开发者

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

开发者 https://www.devze.com 2023-02-02 06:55 出处:网络
How do I do something like \'print Dumper $var\' in Embperl - I did this: [- $var = (some hash) ; use Data::Dumper

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

[+ Du开发者_开发百科mper $var +]

...but couldn't get any output.


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"]); -]
-->


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

0

精彩评论

暂无评论...
验证码 换一张
取 消