Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: FileHandles in a Hash (<> ambiguity)

by LanX (Saint)
on May 28, 2014 at 16:07 UTC ( [id://1087677]=note: print w/replies, xml ) Need Help??


in reply to FileHandles in a Hash

Without testing I think you fell into a special trap of '<>'...

The <> operator has DWIM magic to act either as glob or readline.

So it tries to use your hash like a list of filename patterns to be expanded like glob does.

Like others indicated just use readline to avoid ambiguity or copy the hash element to a simple scalar.

HTH! :)

Cheers Rolf

( addicted to the Perl Programming Language)

update

see I/O Operators:

If what's within the angle brackets is neither a filehandle nor a simple scalar variable containing a filehandle name, typeglob, or typeglob reference, it is interpreted as a filename pattern to be globbed, and either a list of filenames or the next filename in the list is returned, depending on context.

Replies are listed 'Best First'.
Re^2: FileHandles in a Hash (<> ambiguity)
by morgon (Priest) on May 28, 2014 at 22:10 UTC
    For my own understanding I try to reformulate what is happening:

    When you do open $teamAccts{$key}.. the expression $teamAccts{$key} refers to a reference to a GLOB.

    When you put that into angles it should (from my point of view) be interpreted as a filehandle but for whatever reason it is not - rather it is treated as a glob-pattern that is first stringified (that gives the GLOB(...) value which is then "globbed". Because no file in the current directory matches that pattern, the pattern is returned.

    This behaviour of glob is probably to mimik the broken glob-behaviour of shells:

    perl -e print glob("hubba"); # prints "hubba" even if you don't have +a file "hubba"
    whereas:
    perl -e print glob("hubba*"); # prints nothing, provided no file matc +hes
    So this is why <$teamAccts{$key}> returns GLOB(...) For my taste this is not simply a wart but a hunchback with a wart on top of perl...

    You have tried to combine in a natural way two of perl's features (hashes and the angle-operator) and and were bitten by a total lack of orthogonality. Really nothing to boast about...

      Well Perl started as a script language, that's why it mimics glob.

      I rarely use <> for globbing, and I wouldn't mind if this feature would be disabled. And I agree that this DWIM logic is somehow surprising.

      But everybody is free to overload <> to do so.

      Your second critic about glob() is somehow cryptic for me, if you say touch hubba you don't want to touch "nothing" because hubba doesn't already exist.

      Perl's success in its early years came from its compatibility with established tools.

      Otherwise we wouldn't be here discussing Perl...

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1087677]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-25 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found