in reply to Re: Using an IO::File object stored in a Hash.
in thread Using an IO::File object stored in a Hash.

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)

Replies are listed 'Best First'.
Re^3: Using an IO::File object stored in a Hash.
by stevieb (Canon) on Jan 20, 2017 at 17:54 UTC

    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>;
Re^3: Using an IO::File object stored in a Hash.
by kcott (Archbishop) on Jan 21, 2017 at 01:43 UTC
    "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