Perl question

Ryan Hennig rhennig at cs.washington.edu
Thu Jun 22 00:57:52 PDT 2000


> I can't get my program to print anything to a file.
How are you trying to do it? Here's an example:

open(OUTFILE, ">outfile.txt") or die "Can't open outfile: $!\n";
print OUTFILE "foobar\n";

common errors:
did you put the > in front of the filename?
In the print statement, did you put a comma after the filehandle?  There
shouldn't be one.

>It was complaining about line earlier. Is this correct?
>
>  if  ($string1 =~ /\*\s*\$string2|\$string2/)

I think you want:
if  ($string1 =~ /\*\s*$string2|$string2/o)
You don't need to escape the $.  It is only interpreted as an EOL character
if it is followed by a pipe, closing paren, or end of string in the regex.
Also, if the value of string2 won't change during the pattern match, use the
/o option.  This tells perl to only compile the pattern once... otherwise,
it will interpolate the variable every time the pattern search is evaluated
(in case it changed since the last search) and this makes it much slower if
used as a loop conditional or something.  If the pattern is only used once,
it won't make a difference, but it's good to remember.
Also, if you do something like:

while( /$var/o) { $var .= "a"; }

Then on each iteration of the loop, $var will change, but perl won't notice
because of the /o option.  This would be a bad place to use /o.

Hope that helps.

Ryan Hennig
ACM Student Webmaster
Dept. of Computer Science and Engineering
University of Washington




>
>
>
>
>



More information about the Linux mailing list