Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Data::Dumper and Cookies

by PerlSufi (Friar)
on Jul 08, 2013 at 17:59 UTC ( #1043154=perlquestion: print w/replies, xml ) Need Help??
PerlSufi has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I am using WWW::Mechanize::Firefox to crawl a site that (unfortunately) uses flash to give me a file I need. I have narrowed down that I need a certain cookie value in order to retrieve this file. Since it is my understanding that I cannot save cookies with Mechanize::Firefox, I am trying to use Data::Dumper in order to fetch the cookie value. Here is what I have so far:
my $mech = WWW::Mechanize::Firefox->new(launch => 'C:\Program Files (x +86)\Mozilla Firefox\firefox.exe', autoclose => 0, autodie => 1 ); $mech->get(''); my $jar = $mech->cookies(); print Dumper($jar);
I am still new to using Data::Dumper, so I am not sure how to go about getting the value I need. My output looks something like this:
'COOKIES' => { '' => { '/' => { 'NSC_fqg-qspe_443' => [ 1, 'ffffffff3b08dc1e45525d5f4f58455e445a4a4212d3' +, undef, undef, bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' ) +, 1373308908 ], 'JSESSIONID' => [ 1, '8430caba0ba26ecea900783b4f4b15378246', undef, undef, $VAR1->{'COOKIES'}{''}{'/'}{'css'}[4] +, 1373308908 ]
I want the value of the JSESSIONID (8430..) I guess my question is- is there a parameter I can pass to Data::Dumper to retrieve this automatically? If not, I may be able to retrieve it with a regex because I have noticed this cookie always starts with 8430 for me. Any insight is greatly appreciated.

Replies are listed 'Best First'.
Re: Data::Dumper and Cookies
by Perlbotics (Chancellor) on Jul 08, 2013 at 18:32 UTC

    Data::Dumper is a good tool to visualise complex data structures, not to access them. In this case, you could access the JSESSIONID directly (since domain and path seem to be constant):

    print $jar->{COOKIES}{''}{'/'}{'JSESSIONID'}[1], "\n";

    But there is a better way. Since $mech->cookies() returns an HTTP::Cookies object, you should use its methods to access the cookie. The scan() method should accomplish what you want.

    # ... as before, set $jar = ... sub callback { # 0 version # 1 key # 2 val # 3 path # 4 domain # 5 port # 6 path_spec # 7 secure # 8 expires # 9 discard # 10 hash #-- add more comparisons to narrow the search result print "Cookie: $_[2]\n" if $_[2] =~ /^8430/; } $jar->scan( \&callback );

    See also perldsc, HTTP::Cookies, and maybe Data::Diver.

      That’s a key point.   (Up-voted!)   When the perldoc's tell you that a particular call returns an object, then that’s what you should work from:   treat it as an object, ask it questions, and dump the answers.   If you simply dump the object, it’s kinda like looking at an X-ray picture:   “yeah, that’s what the guts of the thing look like (ugh!), but that’s not really what I want to know.”   (You’d have to understand the deep-guts of the thing to understand what you are looking at, and who wants to stick their hands in guts when the thing is alive and can answer questions?)   Instead, call its methods, and dump what they tell you:

      print STDERR Data::Dumper->Dump( [ $myobject->foo(), $myobject->bar(), $myobject->bletch() ], [ 'foo says', 'bar says', 'bletch says' ] );

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1043154]
Approved by Perlbotics
Front-paged by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2017-10-21 23:44 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (271 votes). Check out past polls.