Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Putting an array into a hash gives an unexpected reference to the hash itself.

by AnomalousMonk (Canon)
on Nov 08, 2012 at 03:05 UTC ( #1002796=note: print w/replies, xml ) Need Help??

in reply to Putting an array into a hash gives an unexpected reference to the hash itself.

... I am still puzzled as to why I got a reference to the hash value 'email' => $services{'service1'}{'email'} rather than a reference to say 'email' => $maintainers[0];

The problem is that  Dump() does not 'know' that the reference that is the value of both  $services{service1}{email} and  $services{service2}{email} derives from the  @maintainers array, i.e.,  \@maintainers   (not the  $maintainers[0] array element). All  Dump() 'knows' is that one reference in the data structure it is analyzing (by recursive traversal of a tree of references!) is the same as another, and it tries to let you know (without quotation marks) this because it's probably significant. (Perl has a great deal of introspective ability, so it might be possible (but probably not – see Update below) to make a function like  Dump() smart enough figure out the name of the original referent (if it ever had and still has a name!), but if it is, the author of the module didn't know about the possibility or didn't care to make use of it.)

Update: OTOH, it's easy to imagine situations in which the original name of a variable is gone beyond even the ability of PadWalker to access it. In the example below (don't try this at home: bug: dependence on execution order), the name of the lexical  @ra array is, I believe, absolutely gone upon exit from the scope in which it is defined, but the data lives on as long as there is a reference to it.

>perl -wMstrict -le "use Data::Dumper; ;; { my @ra = qw/ /; sub AR { return \@ra; } } ;; my %services = ( service1 => { email => AR() }, service2 => { email => AR() }, ); ;; print Data::Dumper->Dump( [ \%services ], [qw/*services/] ); " %services = ( 'service1' => { 'email' => [ '', '' ] }, 'service2' => { 'email' => $services{'service1'}{'email' +} } );

Replies are listed 'Best First'.
Re^2: Putting an array into a hash gives an unexpected reference to the hash itself.
by choroba (Chancellor) on Nov 08, 2012 at 15:38 UTC
    I doubt Data::Dumper inspects any variable names. It just uses what you give it - or VAR1, VAR2 if it gets nothing.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      I doubt Data::Dumper inspects any variable names.

      It doesn't (as far as I know), and my code example would have been clearer had I not supplied a variable name to  Dump() in contrast to the OP. I just wanted to make the points that  Data::Dumper tries to emphasize repeated references within the data structure it's dumping (as these are usually important), and that even if one could sometimes recover the name of a reference, it probably wouldn't be worth doing.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1002796]
[Your Mother]: Stick with what you know. Or post a SOPW and if it's not complicated I guarantee someone will answer; me if it's straightforward and no one else gets it better first.
[1nickt]: ++YourMother (Grammar Vigilante of Bristol)
[Lady_Aleena]: Um, how does grep do multiline? print "$file:$_" if grep { /get_(array|hash| data)\(.+\)/ } $_; only returns the same 18 lines the command line grep does.
[Lady_Aleena]: my scratchpad has what I have so far.
[choroba]: what do you mean by multiline?
[Discipulus]: thanks 1nickt!
[choroba]: grep is a filter, it selects elements from a list based on a boolean condition
[moritz]: you need to read the file in one go to do a multiline regex match
[erix]: ha ha, I like the Vigilante
[Lady_Aleena]: Forget it. I'm getting angry with it.

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2017-05-23 19:49 GMT
Find Nodes?
    Voting Booth?