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:Sample 1 Output:use Path::Class; my $filename = "data.txt"; my $handle = file($filename)->open('<:encoding(UTF-8)'); while (<$handle>) { chomp($_); print $_,"\n"; } close($handle);
However, if I save the same in a hash, it does not. Sample 2:1 2 3 4
Sample 2 Output: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'});
What am I missing? I suspect it is something quite simple that I have not experienced before. Thank you for the assistance.IO::File=GLOB(0x6fdf28)
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:Sample 4 Output: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'});
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 | |
by jjs04 (Novice) on Jan 20, 2017 at 16:36 UTC | |
by BrowserUk (Patriarch) on Jan 20, 2017 at 18:40 UTC | |
by jjs04 (Novice) on Jan 20, 2017 at 19:14 UTC | |
|
Re: Using an IO::File object stored in a Hash.
by kcott (Archbishop) on Jan 20, 2017 at 16:01 UTC | |
by jjs04 (Novice) on Jan 20, 2017 at 17:10 UTC | |
by stevieb (Canon) on Jan 20, 2017 at 17:54 UTC | |
by kcott (Archbishop) on Jan 21, 2017 at 01:43 UTC |