FaceBro
FaceBro

Reputation: 859

How to understand the output of Data::Dumper?

#!/usr/bin/perl
use Data::Dumper;
use Tk;
use strict;
use warnings;
my $name='test';
my $s_ref=\$name;
bless $s_ref, 'Tk';
print Dumper \$s_ref;

The output info is:

$VAR1 = \bless( do{\(my $o = 'test')}, 'Tk' );

How to understand this info? What do we get from the output?

Upvotes: 2

Views: 1873

Answers (2)

Chandre Gowda
Chandre Gowda

Reputation: 930

Usually Data::Dumper is used to check the content of the variable like Hash or Array. In your case you are trying to Dump the class object reference.

For Example look at the below code and output, $VAR1 shows the content of $var which is associate array (HASH)

Code:

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


my $var = {a => 'One', b => 'Two', c => [1,2,3]};

print Dumper $var;

Output:

$VAR1 = {
  'c' => [
           1,
           2,
           3
         ],
  'a' => 'One',
  'b' => 'Two'
};

Upvotes: 0

Steve
Steve

Reputation: 1243

Data::Dumper needs to get a reference to a string without creating a new variable in the current scope, this is presumably how it does it. Working from the middle outwards we have my $o = 'test' which declares $o, sets it's value to 'test' and also returns $o. The do{} block in this case provides a scope for the my binding to exist in, when the block exits $o ceases to exist but the value it references continues to, which is good as the \ at the start of the do block takes a reference to it's returned value. The reference to the string 'test' is then blessed with 'Tk'.

Upvotes: 3

Related Questions