in reply to Getting file handle glob name instead of contents

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

Replies are listed 'Best First'.
Re: Getting file handle glob name instead of contents
by stevendel (Novice) on Sep 10, 2008 at 18:36 UTC
    My apologies to all for not RTFM (or more precisely, not finding the relevant section to read). TGI, thanks for your additional remarks concerning passing scalars. I don't use global file handles any more, so passing just the scalar will work perfectly.