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
In reply to Using an IO::File object stored in a Hash. by jjs04
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |