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

Hi All,

I am confused as to how to use data dumper. In the following code, how would I use it to see the data and the structure of $ua and $res?

my $ua; my $res; $ua = LWP::UserAgent->new; $ua->timeout ( MaxTime1 ); $ua->show_progress; $ua->requests_redirectable ([]); $res = $ua->get($Url);

Also a point of confusion: is

     $ua->show_progress
the same thing as
     $$ua{show_progress}
?

Many thanks,
-T

Replies are listed 'Best First'.
Re: data dumper question
by LanX (Saint) on Sep 13, 2016 at 01:47 UTC
    > Also a point of confusion: is $ua->show_progress the same thing as $$ua{show_progress}?

    No the first is a method call identical to $ua->show_progress() , the second a dereferenced hash look up identical to $ua->{show_progress}

    Regarding Data::Dumper , what's wrong with the examples in the docs?

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      "$ua->show_progress" = "$ua->show_progress()"

      Now I understand. Thank you!

      > Regarding Data::Dumper , what's wrong with the examples in the docs?

      Probably nothing. I just don't understand it. (I did look at it before posting this.) Would you mind weaving it into my code so I can see for myself?

        Would you mind weaving it into my code so I can see for myself?

        (Note: The following provides answers already given in essence by LanX, but it at least also provides an example of sorts and so may be helpful.)

        ... how would I use [data dumper] to see the data and the structure of $ua and $res?

        Untested:

        use Data::Dumper; ... my $ua = LWP::UserAgent->new; print Dumper $ua; ... my $res = $ua->get(...); print Dumper $res; ...
        Please see Data::Dumper. (Note: Be prepared for a lot of data to be dumped from some object references!)

        Also a point of confusion: is
            $ua->show_progress
        the same thing as
            $$ua{show_progress}
        ?

        It is not. The expression  $ua->show_progress invokes (without any arguments) the method  show_progress() in the class (or a superclass) of which  $ua is an object reference.

        The expression  $$ua{show_progress} attempts to access the value of the  'show_progress' key in the anonymous hash to which  $ua is presumed to be a reference; it is an error if  $ua is not a hash reference. This expression is the same as the form  $ua->{show_progress} which is preferred (properly, IMHO).

        c:\@Work\Perl>perl -wMstrict -le "{ package Foo; use Data::Dump qw(pp); ;; sub new { my $class = shift; return bless { show_progress => 'not so fast', @_, } => $class; } sub show_progress { my $self = shift; print 'Real Thing: ', pp $self; } } ;; my $object_reference = Foo->new(foo => 'bar', hi => 'lo'); ;; $object_reference->show_progress; print 'A: ', $$object_reference {show_progress}; print 'B: ', $object_reference->{show_progress}; " Real Thing: bless({ foo => "bar", hi => "lo", show_progress => "not so + fast" }, "Foo") A: not so fast B: not so fast
        I'm using Data::Dump in this example instead of Data::Dumper because I like its output formatting better; the latter is core, the former is not.


        Give a man a fish:  <%-{-{-{-<

        Hi Todd,

        I see from Need help with loop syntax errors, that you like to experiment. I suggest that you do more of the same with Data::Dumper to empirically see what it does with various data structures.

        In general you give Dumper a reference to any arbitrary structure and it figures out how to display it.

        Here is some "play" code to get you started:

        #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @data = qw( 7 8 9); my $dataref = \@data; #create reference to data array my $string = 'this is string'; print Dumper \@data; #dump using reference to @data =PRINTS $VAR1 = [ '7', '8', '9' ]; =cut print Dumper $dataref; #dump using scalar reference =PRINTS $VAR1 = [ '7', '8', '9' ]; =cut print Dumper @data; =PRINTS $VAR1 = '7'; $VAR2 = '8'; $VAR3 = '9'; =cut print Dumper $string; =PRINTS $VAR1 = 'this is string'; =cut print Dumper \$string; =PRINTS $VAR1 = \'this is string'; =cut __END__ I used a feature of Perl called perlpod - the "Plain Old Documentation +" commands to interleave multi-line printouts into the code. That is not what this is normally used for, but you will sometimes see this techni +que on PM as a way to make the code and the printout all "one Perl file" t +hat can be executed.
        There is more than one module that can dump data, but Data::Dumper is "core" meaning that it is included in all Perl distributions and you can just assume that it is there without having to install anything. Have fun experimenting.
Re: data dumper question
by Anonymous Monk on Sep 13, 2016 at 01:39 UTC

    I am confused as to how to use data dumper. In the following code, how would I use it to see the data and the structure of $ua and $res?

    What is "data dumper", where can I read about it?