I've lately been playing around with the IO:: modules, File, Scalar and ScalarArray. I've noticed some behavior that leaves me a little puzzled- the old feature versus bug thing. Here's what I'm talking about, starting with filehandles. No matter which style you use, open FILE, name or $fh = IO::File->new(name), old and new styles are pretty much transparent, i.e. code written one way can be changed to the other with minimal bother. There is one thing though, this:
#!/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]; }
doesn't work worth a damn. You get an error message like:
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.
It doesn't work any better on similar code using old style typeglobs:
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

In reply to Real World 1, Great Expectations 0 by hsmyers

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.