hsmyers has asked for the wisdom of the Perl Monks concerning the following question:
doesn't work worth a damn. You get an error message like:#!/perl/bin/perl # # test.pl -- IO:: test code... use strict; use warnings; use diagnostics; use IO::ScalarArray; use IO::File; use IO::Scalar; my @list = qw(one.tmp two.tmp three.tmp); my @handles; foreach (@list) { push @handles,new IO::File ">$_"; } for (my $i = 0;$i < $#handles ;$i++) { print $handles[$i] ,"Hi how the hell are ya!"; close $handles[$i]; }
It doesn't work any better on similar code using old style typeglobs:String found where operator expected at test.pl line 31, near "] "Hi h +ow the hell are ya!"" (#1) (S) The Perl lexer knows whether to expect a term or an operator. + If it sees what it knows to be a term when it was expecting to see an operator, it gives you this warning. Usually it indicates that an operator or delimiter was omitted, such as a semicolon. (Missing operator before "Hi how the hell are ya!"?) syntax error at test.pl line 31, near "] "Hi how the hell are ya!"" Execution of test.pl aborted due to compilation errors (#2) Uncaught exception from user code: syntax error at test.pl line 31, near "] "Hi how the hell are +ya!"" Execution of test.pl aborted due to compilation errors. C:\Perl\Perl_Dev\regex>perl test.pl Undefined subroutine &main::mprint called at test.pl line 31 (#1) (F) The subroutine indicated hasn't been defined, or if it was, it + has since been undefined. Uncaught exception from user code: Undefined subroutine &main::mprint called at test.pl line 31.
open ONE, ">one.tmp"; push @handles, *ONE; open TWO, ">two.tmp"; push @handles, *TWO; open THREE, ">three.tmp"; push @handles, *THREE;
Dies in the same place and manner. The code works just fine if you create a temporary simple scalar with a copy of the contents of the array position. Like '$fh = $handles[$i]; print $fh yadda, yadda, yadda'. So somehow perl doesn't evaluate 'array position containing scalar' to 'scalar contained'. Which last, is certainly what I had expected. Note that any reduction to simple scalar works fine.
Another peculiarity, lies in the difference between filehandles created by IO::File versus IO::Scalar and IO::ScalarArray. Simply put, in the first case $fh = IO::File->new whatever, <$fh> works fine, pretty much keeping up the aforementioned transparency. But, if $fh comes from either IO::Scalar or IO::ScalarArray, the shorthand, <$fh>, dies the usual nasty death.
The work-around is to use either $fh->getline() or $fh->getlines() as needed (wantarray versus scalar context.) Since both inherit from IO::Handles and since the source for handle.pm suggests that:
"The getline method returns a single line from the filehandle, just like the <$fh> operator when used in a scalar context. the getlines method returns a list of lines in a manner identical to the <$fh> operator in a list context. The getlines method will croak if called in a scalar context."Note that it doesn't say <$fh> for IO::Scalar and IO::ScalarArray dies...just says this stuff about 'just like the <$fh>...'.
It occurs to me that these two things are just different sides of the same coin. For whatever reason, perl does not evaluate the thing in question as a filehandle and complains accordingly.
So having said all of this, would someone point out to me what I've gotten wrong, misunderstood or the like. I am entirely too aware of my own blind spots, particularly when things don't appear to work as I had expected (that shouldn't be too surprising, great expectations are usually replaced by reality sooner or later…).
hsm
|
---|