Loginskip to content

November 2nd, 2006

and the rest of the program will be

and the rest of the program will be ignored. (Actually, it becomes available for your program to read through the filehandle DATA if you wish.) Perl’s quoting rules will be a welcome relief no more fighting to determine the combination of ‘, “, and to get what you want. The simple rule is: Interpolation (the Perl term for what you call parameter substitution) is determined only by the type of the outermost quotes. For instance, if you want to pass a variable that may contain spaces to the grepprogram, just say system “grep -s ‘$quote’ @files”; and the double quoting around the whole command ensures that $quote and @filesare interpolated. The former is decorated with single quotes, and the elements of the latter are space-separated by default, exactly what we want. What if you want to embed $quotebetween double quotes instead? Perl allows you to put a double quote in a double-quoted string by preceding it with a backslash, but there is a cooler solution using Perl’s qq quoting operator, which allows you to choose a different delimiter and thereby eliminate the need to backwhack quotation marks: system qq(grep -s “$quote” @files); If $quote can contain absolutely any characters, however, and you don’t want any of them to be interpreted as metacharacters either by the shell or by the grepcommand, you need to make sure that all of the potential troublemakers are escaped, and Perl has a special escape sequence pair just for the job: system “grep -s Q$quoteE @files” Extending this to handle the possibility of members of @files containing white space or metacharacters is left as an exercise for the reader. Speaking of operators, Perl uses eq and nefor strings, and ==and != for numbers, which you probably think is the wrong way around. You’ll be pleased to learn that the return code for the backticks and system command is stored in the familiar $?. However, that return code is not the one you’re used to; it’s the one that C programmers get from their system call, so shift it right by eight bits to get the code you want. (The bottom eight bits contain information most people aren’t interested in, like whether the program core dumped or what signal interrupted it, at least on Unix.) 12.5 Tips for the C++ or Java Programmer The object-oriented features of Perl may appear frighteningly sparse; in fact, they are the consequence of a careful design to avoid unnecessarily bloating the language. For instance, there is no thiskeyword for referring to the current object in a method; instead, it’s passed as the first parameter, and you can pick your own name. Most people pick $selfbecause the declaration my $self has a nice ring to it (or because they are former Smalltalk programmers).
Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services

Comments are closed.