Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Dump the key value pairs in a hash

by byrnejb (Initiate)
on Oct 22, 2021 at 20:12 UTC ( #11137898=perlquestion: print w/replies, xml ) Need Help??

byrnejb has asked for the wisdom of the Perl Monks concerning the following question:

I am not a perl programmer. I do have to maintain a legacy perl application. Something has changed in the environment so that it no longer correctly reports the status of public key certificates. I need to discover what that change is and how to accommodate it. The critical piece of code appears to be:
sub genHTMLTableRow { my $self = shift; my $args = shift; my $e = shift; my $ser = $e->{serial}; my $pem = $ser.'.html'; . . .
What I wish to do for debugging purposes is to dump the actual contents of $self, $args, and $e. I tried the naive approach of using print "$self\n"; but that just gave me this:
CSP=HASH(0x800b1e060)
I tried to use the map function
print map { "Key: $_ has Value:$self{$_}\n" } keys $self;
I clearly do not understand the naming conventions of perl variables as this usage of map causes the error:
Global symbol "%self" requires explicit package name (did you forget + to declare "my %self"?) at /root/bin/rcsp/blib/lib/CSP.pm line 861
What is the perl way of displaying the contents of $self, $args, and $e?

Replies are listed 'Best First'.
Re: Dump the key value pairs in a hash
by choroba (Archbishop) on Oct 22, 2021 at 20:21 UTC
    $self is a scalar variable, so it's not a hash, it's a reference to a hash. You need to dereference it to get the hash:
    print map { "Key: $_ has Value:$self->{$_}\n" } keys %$self; # ~~ ~

    Or, use Data::Dumper:

    use Data::Dumper; ... print Dumper($self);

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Dump the key value pairs in a hash
by haj (Priest) on Oct 22, 2021 at 20:27 UTC

    Well, yes, there's a rather easy fix. $self is a reference to the actual hash. You can convert ("dereference") it by writing %$self instead of %self (which is a variable of its own).

    However, a better approach would be to use one of Perl's modules for that. Data::Dumper is in Perl core, Data::Dump or Data::Print need to be installed using your package manager or CPAN and provide "nicer" output. For Data::Dumper, use:

    use Data::Dumper; print Dumper $self; print Dumper $args; print Dumper $e;

    That should go a long way.

Re: Dump the key value pairs in a hash
by Anonymous Monk on Oct 22, 2021 at 20:19 UTC
Re: Dump the key value pairs in a hash
by karlgoethebier (Abbot) on Oct 23, 2021 at 10:17 UTC
    use Data::Dump; sub genHTMLTableRow { my ($self, $args, $e ) = @_; # No triple shift dd for ($self, $args, $e); # It even rhymes! …; }

    Untested as I’m on a mobile device and in a hurry 🤪😎

    BTW, genHTMLTableRow sounds like something that a class does. It cries downright for a role. See Class::Tiny as well as Role::Tiny for further inspiration. Regards, Karl

    Update: Fixed missing $ before self. Thanks choroba.

    «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11137898]
Approved by kcott
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2021-11-28 12:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?