Today I came across what seems to this newbie a quite perplexing puzzle. It regards the behaviour of references to filehandles:
#!/usr/bin/perl -w use strict; open(OUTFILE, ">out.txt") or die "cannot open out.txt: $!"; my $fred = {}; $fred->{outfile} = \*OUTFILE; my $barney = $fred->{outfile}; print $barney "This is a test\n"; print $fred->{outfile} "This is another test\n";
The code above produces the following syntax error:
String found where operator expected at ./test.pl line 11, near "} "Th +is is another test\n"" (Missing operator before "This is another test\n"?) syntax error at ./monks.pl line 11, near "} "This is another test\n"" Execution of ./test.pl aborted due to compilation errors.
My question is why does it work for $barney, but not for $fred->{outfile}, when both should contain the same reference?
Thanks in advance for any light you may be able shed upon this matter.
In reply to Using a reference to a Filehandle by mildside
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |