Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Pushed references getting stringified somehow?

by charnos (Friar)
on Oct 01, 2002 at 16:40 UTC ( [id://202046]=perlquestion: print w/replies, xml ) Need Help??

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

I've been staring at this (originally one line) segment of code, and I cannot figure out what is going wrong, exactly. I have a snippet of code here (for external reference, $digest and $entry are simple scalars containing strings, not that they're used strangely here) that I have broken down into as many steps as I could to pinpoint what was going wrong, inserting print statements along the way (to no avail), the code also runs with strict and warnings on:
my %hash = ('digest'=>$digest, 'entry'=>$entry); my $hashref = \%hash; print ref $hashref, "|"; push @out, $hashref; print ref $out[$#out], "|$out[$#out]";
This prints: HASH||HASH(0x3e00258). The first ref() call suggests that $hashref is a valid hash reference, the second call returns false, and the third segment prints out a string representation of a hash reference. This suggests to me that since the second ref() call fails, yet $out[$#out] contains the string representation of a hash ref, that the last element in @out is a stringified version of a valid hash ref.
At this point, my brain taps me on the shoulder and says quite matter-of-factly that push() does not do anything to the list portion of its parameters, and that's the only operation performed on it between tests. This is verified by testing a smaller script containing just this code.

So I suppose my question is really: what could I possibly be doing outside of this code to get this outcome? The only variables not initialized in this snippet are two strings only used in the assignment of the hash, and an array which is only used to retrieve the last item, which was pushed onto it in the scope of this snippet.

Replies are listed 'Best First'.
Re: Pushed references getting stringified somehow?
by chromatic (Archbishop) on Oct 01, 2002 at 16:43 UTC

    The third segment stringifies the hash reference. That's why it prints a stringified hash reference. Also beware of precedence. I don't think it's hurting you, but if you're confused, be explicit.

    use Data::Dumper; print ref($out[-1]), '|', Dumper($out[-1]);
Re: Pushed references getting stringified somehow?
by broquaint (Abbot) on Oct 01, 2002 at 16:45 UTC
    Er, I get this output
    HASH|HASH|HASH(0x80fd4d0)
    Grasping at straws have you tried sticking parens around the second ref() call to disambugate it's argument list? Also the 'third segment' is going to print out a stringified hash ref because you're accessing a hash ref which is then stringified (because it's being interpolated in a string). Where's the surprise?
    HTH

    _________
    broquaint

Re: Pushed references getting stringified somehow?
by Zaxo (Archbishop) on Oct 01, 2002 at 16:55 UTC

    It works for me. I can't reproduce that with:

    perl -e'$foo={};print ref $foo,"|";push @bar,$foo; print ref $bar[$#ba +r], "|$bar[$#bar]$/"' HASH|HASH|HASH(0x8106164)

    After Compline,
    Zaxo

Re: Pushed references getting stringified somehow?
by charnos (Friar) on Oct 01, 2002 at 17:08 UTC
    Thank you all for your amazingly fast responses. :)
    As it turned out, the following modified code worked correctly (the overhead is going out the window in a second):
    #print ref($hashref), "|"; push @out, $hashref; print keys %{$hashref}; #print ref($out[-1]), "ass|$out[$#out]|";
    Which makes sense, I suppose, though it worked for Zaxo and Broquaint, as well as my other test script. Odd..

Log In?
Username:
Password:

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

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

    No recent polls found