How do I do something like 'print Dumper $var' in Embperl

1.8k Views Asked by At

How do I do something like 'print Dumper $var' in Embperl - I did this:

[-
$var = (some hash) ;
use Data::Dumper
print Dumper $var
-]

and this

[+ Dumper $var +]

...but couldn't get any output.

2

There are 2 best solutions below

0
On BEST ANSWER

I usually use Data::Dumper's Dump method, which produces a string I can do anything I want with.

[! use Data::Dumper; # Only need to do once !]
[-
$var = (some hashref);
print Data::Dumper->Dump([$var, \@var2, \%var3]);
# Note that Dump takes an arrayref of SCALARs, therefore
# if you have arrays/hashes, you need to pass references to those
-]

HOWEVER, please remember that in EmbPerl, you need to be careful where you print:

To print to Apache's error log, print to STDERR:

[-
print STDERR Data::Dumper->Dump([$var, \@var2, \%var3]);
-]

To print to your web page, print to OUT handle, or use [+ +] includes. If it's a debug print, I usually just stick it inside an HTML comment:

<!-- DEBUG
[+ Data::Dumper->Dump([$var, \@var2, \%var3]); +]
[- print OUT Data::Dumper->Dump([$x1, $x2], ["VarName1", "VarName2"]); -]
-->
0
On

Did you mean Embperl?

If you want to dump a variable with Data::Dumper, you need to pass its reference. In your case:

use Data::Dumper;
my %hashvar = (a => 1, b => 2);
print Dumper(\%hashvar);

or

use Data::Dumper;
my $hashref= {a => 1, b => 2};
print Dumper($hashref);

In the first case the variable is a hash so you must take its reference; in the second you have a reference to a hash and is therefore passed as-is to Data::Dumper