0

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.

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
Hogsmill
  • 1,574
  • 13
  • 21

2 Answers2

5

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"]); -]
-->
DVK
  • 126,886
  • 32
  • 213
  • 327
1

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

Gurunandan Bhat
  • 3,544
  • 3
  • 31
  • 43