perl - Why does ${fh} behave differentially than $fh when reading line by line? -


this script:

#!/usr/bin/perl use strict; use warnings;  ${fh};  open($fh, '<', "some_file.txt") or die "fail\n";  while(my $line = <${fh}> ) {     print $line; } close $fh; 

outputs:

glob(0x5bcc2a0) 

why output?

if change <${fh}> <$fh>, print some_file.txt line line expected. thought braces used delimit variable names, , my ${var} same my $var.

are there other scenarios adding {} around variable name causes problems?

i tried on perl 5.8.8 on red hat , cygwin perl 5.14.

from section on <> in perlop:

if angle brackets contain simple scalar variable (for example, $foo), variable contains name of filehandle input from, or typeglob, or reference same. example:

$fh = \*stdin; $line = <$fh>; 

if what's within angle brackets neither filehandle nor simple scalar variable containing filehandle name, typeglob, or typeglob reference, interpreted filename pattern globbed, , either list of filenames or next filename in list returned, depending on context. distinction determined on syntactic grounds alone. means <$x> readline() indirect handle, <$hash{key}> glob(). that's because $x simple scalar variable, $hash{key} not--it's hash element. <$x > (note space) treated glob("$x "), not readline($x).

you can see b::concise:

$ perl -mo=concise -e'<$fh>' 5  <@> leave[1 ref] vkp/refc ->(end) 1     <0> enter ->2 2     <;> nextstate(main 1 -e:1) v:{ ->3 4     <1> readline[t1] vk*/1 ->5 -        <1> ex-rv2sv sk/1 ->4 3           <$> gvsv(*fh) s ->4 -e syntax ok  $ perl -mo=concise -e'<${fh}>' 6  <@> leave[1 ref] vkp/refc ->(end) 1     <0> enter ->2 2     <;> nextstate(main 70 -e:1) v:{ ->3 5     <@> glob[t1] vk/1 ->6 -        <0> ex-pushmark s ->3 -        <1> ex-rv2sv sk/1 ->4 3           <$> gvsv(*fh) s ->4 4        <$> gv(*_gen_0) s ->5 -e syntax ok 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -