jjs04 has asked for the wisdom of the Perl Monks concerning the following question:

I have found something that I think should work, but does not work as expected. When I use Path::Class to open a file and save its handle to a variable, it works as expected.

Sample 1:
use Path::Class; my $filename = "data.txt"; my $handle = file($filename)->open('<:encoding(UTF-8)'); while (<$handle>) { chomp($_); print $_,"\n"; } close($handle);
Sample 1 Output:
1 2 3 4
However, if I save the same in a hash, it does not. Sample 2:
use Path::Class; my $filename = "data.txt"; my %hash = ( 'handle' => file($filename)->open('<:encoding(UTF-8)') ); while(<$hash{'handle'}>) { chomp($_); print $_,"\n"; } close($hash{'handle'});
Sample 2 Output:
IO::File=GLOB(0x6fdf28)
What am I missing? I suspect it is something quite simple that I have not experienced before. Thank you for the assistance.

 

Edit: As first indicated by BrowserUk, readline should be used. Because the diamond operator is used with readline and glob, there may be some ambiguity as to how the parser should proceed. When a hash element is used within the diamond operator (<$hash{$key}>), it is a glob, causing my difficulties.

This following sample works as expected. Sample 4:
use Path::Class; my $filename = "data.txt"; my %hash = ( 'handle' => file($filename)->open('<:encoding(UTF-8)') ); while(readline($hash{'handle'})) { chomp($_); print $_,"\n"; } close($hash{'handle'});
Sample 4 Output:
1 2 3 4

Replies are listed 'Best First'.
Re: Using an IO::File object stored in a Hash.
by BrowserUk (Patriarch) on Jan 20, 2017 at 15:55 UTC

    It's a parsing thing. Use: while( readline( $hash{'handle' ) ) { instead.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thank you, readline works as expected.

      However, I still do not understand how <$handle> is not equivalent to <$hash{'handle'}> when they both contain IO::File objects.

        Simply stated, its because the diamond operator does dual service as both readline and glob. If the value or variable inside looks like a file handle at compile time, then it is interpreted and compiled as readline, if it doesn't, then it is interpreted and compiled as glob. At compile time a hash element is deemed more likely to contain a glob pattern than a filehandle.

        As for why, you'd have to ask the author(s), but it is probably a throwback to when perl didn't have lexical filehandles; and it's never been seen as high enough priority to attempt a fix.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Using an IO::File object stored in a Hash.
by kcott (Archbishop) on Jan 20, 2017 at 16:01 UTC

    G'day jjs04,

    Welcome to the Monastery.

    "What am I missing? I suspect it is something quite simple that I have not experienced before."

    Yes, it's fairly simple (once you know about it). You just need to put the filehandle in a block, e.g.

    close { $hash{'handle'} };

    Update (correction): Firstly, I saw the close and thought that was the problem: ++BrowserUk has correctly identified the issue and provided a fix. Secondly, while I was certain your close (as written) was a problem, I can't reproduce it: perhaps it was a problem in older versions of Perl (I tried in 5.24.0 and 5.14.0). Thirdly, the close statement I wrote won't work as is: you'll get an "Odd number of elements in anonymous hash" message. If you do need a block (for your version of Perl), you'll need to prefix it with a '*'. Apologies for the misinformation and any confusion I may have caused. Here's how to write both a print and a close (using a block) without any warning or error messages:

    print { $hash{'handle'} } "...\n"; close *{ $hash{'handle'} };

    That issue doesn't appear to be mentioned in the close doco: it probably should be! However, the print doco does mention the same issue:

    "If you're storing handles in an array or hash, or in general whenever you're using any expression more complex than a bareword handle or a plain, unsubscripted scalar variable to retrieve it, you will have to use a block returning the filehandle value instead, ..."

    Update (additional information): The close block issue was niggling me, so I did some further research. As already stated, I couldn't reproduce any problem with 5.24.0 or 5.14.0 (the newest and oldest versions, respectively, that I have installed locally). I looked in the earliest (5.8.8) online perldoc, http://perldoc.perl.org/5.8.8/functions/close.html: no mention there. I then looked in the earliest (5.004) I could find on CPAN, http://search.cpan.org/~chips/perl5.004/pod/perlfunc.pod#close_FILEHANDLE (dated 15 May 1997): no mention there either. So, it seems there's no reference to this in the past two decades and it appears that I was simply wrong about that. I was probably thinking of the print block requirement, as per the documentation link, and quote, above. Again, my apologies.

    — Ken

      Thank you for the assistance.

      I am using Perl 5.24.0.

      The problem is with reading from the IO::File object stored in the hash. All other portions of the example code work correctly.

      Using readline, as suggested by BrowserUK fixes the problem; however, I do not understand why storing the IO::File object in a hash vs storing it in a scalar should have any affect. There must be some syntatic trick I am missing.

      Sample 3:
      use Path::Class; my $filename = "data.txt"; my $object = file($filename)->open('<:encoding(UTF-8)'); my %hash = ( 'handle' => $object); my $line1 = <$object>; my $line2 = readline($hash{'handle'}); my $line3 = <$hash{'handle'}>; my $line4 = <{$hash{'handle'}}>; print $line1, "\n"; print $line2, "\n"; print $line3, "\n"; print $line4, "\n"; close($object);
      Sample 3 Output:
      1 2 IO::File=GLOB(0x4de178) IO::File=GLOB(0x4de178)

        You said:

        my $line3 = <$hash{'handle'}>;

        Perl interprets <$hash{handle}> as this: glob(qq<$hash{handle}>), which is not what you want, and it's why you're getting the glob ref itself printing.

        As stated, you can use readline(), or extract out the handle before you use it:

        my $fh = $hash{handle}; my $text = <$fh>;
        "I am using Perl 5.24.0."

        There isn't any version issue: that was my mistake. See my "Update (additional information)", which I posted after you replied.

        I note your other issue has been resolved[1, 2 & 3].

        — Ken