user1428649
user1428649

Reputation: 93

Data::Dumper returned hash with a slash

So I have a line of perl code that reads like this:

my $stored_value = $foo->some_function($argument);

When I do a Dumper on it:

warn Dumper $stored_value

I receive this as a result.

$VAR1 = \{
             'foo' => 'abc',
             'bar' => '123'
};

Now, I've seen results like this:

warn Dumper $another_hash;
$VAR1 = {
      'foo' => 'bar',
      'baz' => 'quux'
    };

And if I wanted to get say, foo's value, I'd type in something like this:

warn Dumper $another_hash->{'foo'};

To get this as a result.

$VAR1 = 'bar';

Originally, I couldn't find anything through my Google searches, but just now, I made a little test script to play around with what I saw, and I found this out

#!/usr/bin/perl
#
use strict;
use warnings;
use Data::Dumper;

sub test {
my $brother = {'Ted'};
$brother->{'Ted'} = 'brother';
return \$brother;
}

my $blah= test();
my $blah = ${$blah};
print Dumper $blah->{'Ted'};
print "\n";

Here are my results:

$VAR1 = 'brother';

I wanted to share what I had found incase someone else ran into the same thing, but what exactly did I see?

I saw how to do this in http://perldoc.perl.org/perlref.html#Using-References, but I just wanted some clarification on it.

Upvotes: 2

Views: 817

Answers (1)

Borodin
Borodin

Reputation: 126742

I'm not sure what your question is, but your output shows that $stored_value is a reference to a scalar, which, in turn, is a reference to a hash.

It is rarely useful to keep references to scalars so this may indicate a bug.

This short program shows how the value could have been created

use strict;
use warnings;

use Data::Dumper;

my $href = {
  foo => 'abc',
  bar => '123',
};

my $href_ref = \$href;

print Dumper $href_ref;

output

$VAR1 = \{
            'bar' => '123',
            'foo' => 'abc'
          };

And, by the way, it is usually more useful to use Data::Dump in preference to Data::Dumper

Upvotes: 4

Related Questions