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

strange behavior of indexed list of globs

by jabowery (Beadle)
on Aug 03, 2013 at 21:10 UTC ( #1047742=perlquestion: print w/ replies, xml ) Need Help??
jabowery has asked for the wisdom of the Perl Monks concerning the following question:

$|++; for (1..10){ $pid[$_]=open($fh[$_],"-|"); next if $pid[$_]; print; exit(0); } foreach my $i (1..10){ my $fh=$fh[$i]; while(<$fh[$i]>){ print "'$_'"; } close $fh[$i]; }
outputs:

'GLOB(0x2535298)''GLOB(0x25352e0)''GLOB(0x2535418)''GLOB(0x2541e38)''G +LOB(0x2541ec8)''GLOB(0x25669d8)''GLOB(0x255ead8)''GLOB(0x2542420)''GL +OB(0x25523a8)''GLOB(0x2542dc8)'

but change $fh[$i] after its first occurance to $fh and it outputs:

'1''2''3''4''5''6''7''8''9''10'

Comment on strange behavior of indexed list of globs
Select or Download Code
Re: strange behavior of indexed list of globs
by davido (Archbishop) on Aug 03, 2013 at 21:43 UTC

    Are you asking about the behavior of <$simple_scalar> versus <$more[$complex]>? That's a readline versus a glob. perlop says:

    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. This distinction is determined on syntactic grounds alone. That means <$x> is always a readline() from an indirect handle, but <$hash{key}> is always a glob(). That's because $x is a simple scalar variable, but $hash{key} is not--it's a hash element. Even <$x > (note the extra space) is treated as glob("$x ") , not readline($x).

    The feature is probably going to go down already recorded in history as one of Perl's warts. :) And the confusion that the angle brackets operator leads to is the primary motivation behind Perl Best Practice's advice to "Use glob, not <...>" (Globbing, page 167).


    Dave

      Thanks for the heads up. Perl does what I think it will enough of the time, without reference to documentation, that tolerating its few documented warts is worth it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047742]
Approved by Loops
Front-paged by Loops
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-09-17 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (99 votes), past polls