Why are you passing typeglobs? There's no reason to use typeglobs here, just pass the lexical handle as a scalar. Plenty of older tutorials say to pass filehandles as globs, but that only applies to global filehandles generated when you do stuff like: open( FOO, ">/bar/baz" ). It's best to use a 3 argument version of open and a lexical handle.
#!/usr/bin/perl open(my $handle1, "<", "/tmp/1") or die "Input file does not exist.\n" +; # Case 2 -- fails GetFileContents($handle1); close($handle1); sub GetFileContents { my $foo = shift; print "Function hash:\n"; { local($/); print <$foo>; } }
On a side note, I like to use the lower precedence or for error detection, becuase it won't mess with my lists if I leave off parens. See perlop section on C-Style Logical Or for more info.
Update: Clarified globs vs typeglobs. Also, it's worth pointing out that using a lexical handle and not a typeglob doesn't resolve the issue with the <$hash{lexical_handle}> construct. Others have covered that issue.
TGI says moo
In reply to Re: Getting file handle glob name instead of contents
by TGI
in thread Getting file handle glob name instead of contents
by stevendel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |