Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Indirect Referencing

by periapt (Hermit)
on Jun 17, 2004 at 13:43 UTC ( [id://367615]=perlquestion: print w/replies, xml ) Need Help??

periapt has asked for the wisdom of the Perl Monks concerning the following question:

OK, I'm stumped. I'm trying to read from a file handle in a while loop. The reference to the file handle is in a hash which itself begins with a hash reference like this ...
my $cfg = {}; open (INFILE,"<testfile.txt") || die "Could not open file\n$!\n"; $$cfg{infile} = \*INFILE; while(<$$cfg{infile}>){ print "$_\n"; } __END__ GLOB(0x20eab18)
However, when I do it this way, it works as expected.
my $cfg = {}; open (INFILE,"<testfile.txt") || die "Could not open file\n$!\n"; $$cfg{infile} = \*INFILE; my $infh = $$cfg{infile}; while(<$infh}>){ print "$_\n"; }
I figure that the angle operator is not dereferencing $$cfg{infile} as I want but I cannot come up with a combinations of parens, curly braces and arrows that will make this work. What am I missing?

Update: I knew I was missing the obvious. Thanks Roy. :o)

PJ
Unspoken but ever present -- use strict; use warnings; use diagnostics; (as needed)

Replies are listed 'Best First'.
Re: Indirect Referencing
by Roy Johnson (Monsignor) on Jun 17, 2004 at 14:02 UTC
    From perldoc perlop:
    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.
    You can try the readline function rather than the <> operator. It is not clear to me whether you get all the default behaviors when using readline.

    We're not really tightening our belts, it just feels that way because we're getting fatter.
      Darn, I missed that. I'm not hung up on using the hash element, I just got running down the wrong track. Thanks.

      PJ
      We are drowning in information and starving for knowledge - Rutherford D. Rogers
      What good is knowledge if you have to pull teeth to get it - anonymous
      Update: Argh! Nevermind, I misunderstood the question.
      If what's within the angle brackets is neither a filehandle nor a simple scalar variable containing a filehandle name, typeglob, or typeglob reference,
      $infh is a typeglob reference, so it should work. Actually, it does, if the while(<$infh}>){ is replaced by while(<$infh>){.

      Small change, big results ;-)

      Arjen

Re: Indirect Referencing
by demerphq (Chancellor) on Jun 18, 2004 at 18:35 UTC

    Can I just say that you code just screams for a rewrite?

    my $cfg = {}; open my $INFILE,"<","testfile.txt" or die "Could not open file:$!"; $cfg->{infile} = $INFILE; my $infh = $cfg->{infile}; while(<$infh>){ print "$_\n"; }

    Sorry, but if i ever worked on your code before i did anything else i would make conversions like that $$hash{key} is an evil meme that is confusing (it doesnt scale up) easy to misunderstand (what the hell is $$hash {key}, a dereferenced ref followed by a block containing a sub call, a syntax error or a hashref dereference and lookup?) $hash->{key} is hard to mistake for anything but what it is.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


      Hmmm, I like the open my $INFILE ... construct. I'll have to remember it since I use the
      open INFILE,"<","testfile.txt" or die "Could not open file:$!"; $fh = \*INFILE;
      code a lot.

      On a personal note, I have never felt comfortable with the arrow op. It may just be lack of use although I have been using it more lately. I do agree with you about scalability. I generally end up using the arrow op on more complex structures - although I do have a $$$cfg{infile} line in this same program ;o) - Still, it does look cleaner here...

      Thanks for the suggestions.

      PJ
      unspoken but ever present -- use strict; use warnings; use diagnostics; (if needed)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 02:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found