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

Hi All,

First, I am really, really new to Perl, so please don't address me like I know what I am doing. You need to directly correct me. I don't mind you calling me a Dumb A** as long as you answer the question.

I am trying to read and set my cookies. I get the following error:

Can't call method "extract_cookies" on an undefined value at ./GetJavaTest.pl line 53 (#1)

If I remove the comment from line 51, a whole web page prints out. I don't get why I am trying to work on an "undefined value". There is tons of stuff in it. This is so frustrating!

The exact cookie I am trying to use is:

http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1880261.html

Lines 1024 to 1029:

function acceptAgreement(windowRef, part){ var doc = windowRef.document; disableDownloadAnchors(doc, false, part); hideAgreementDiv(doc, part); writeSessionCookie( 'oraclelicense', 'accept-securebackup-cookie' ); }

What the heck (not my actual word) am I doing wrong???

Is there any way I can just write the cookie in like I can with wget? (See the code below for how to use wget.)

Many thanks,

-T

#!/usr/bin/perl # Test java wget style download of Java # wget --header "Cookie: # oraclelicense=accept-securebackup-cookie" # http://download.oracle.com/otn-pub/java/jdk/7u80-b15/jre-7u80-window +s-i586.exe # Notes: # This is going in a sub. Do not use "die" # The "echo" of this will be read by a Bash script. All debug # statements need to use STDERR # Known mistakes: the multimeg download need to go to a file # not a variable # cookies are not being transfered correctly # http://www.perlmonks.org/?node_id=456612 # http://lwp.interglacial.com/ch11_01.htm use strict; use warnings; use diagnostics; # use diagnostics "-verbose"; use Term::ANSIColor qw( BOLD RED RESET ); use LWP::UserAgent; use HTTP::Cookies qw ( extract_cookies ); use LWP::Protocol qw ( https ); use HTTP::Request qw ( request ); use constant MaxTime3 => scalar 3300; # Seconds # Indent here is so I don't have to put it back when it goes back into + a sub my $ua; my $response; my $content; my $request; my $cookie_jar; my $ClickHere = "http://download.oracle.com/otn-pub/java/jdk/7u80-b +15/jre-7u80-windows-i586.exe"; my $RevAddr = "http://www.oracle.com/technetwork/java/javase/down +loads/jre7-downloads-1880261.html"; $ua = LWP::UserAgent->new; $ua->cookie_jar ( {} ); $request = HTTP::Request->new('GET', "$RevAddr" ); print "\$request = ", $request -> as_string, "\n"; $response = $ua->simple_request($request); # print "\$response = ", $response -> as_string, "\n"; $cookie_jar->extract_cookies($response); $ua->timeout ( MaxTime3 ); $ua->show_progress ( 1 ); $response = $ua->get ( $ClickHere, ':content_file' => './eraseme.bi +n' ); print "\$ClickHere response = ", $response -> as_string, "\n";

Replies are listed 'Best First'.
Re: Can't call method "extract_cookies" on an undefined value
by NetWallah (Canon) on Oct 17, 2015 at 05:27 UTC
    In your code, $cookie_jar is declared, but never assigned.

    So, it croaks when you try to call a method on it.

    I think you need:

    $ua->cookie_jar ($cookie_jar={}); # instead of $ua->cookie_jar ( {} );
    (untested)

            The best defense against logic is ignorance.

      That helped. Now I am getting an even sillier error:

      Can't call method "extract_cookies" on unblessed reference at ./GetJavaTest.pl line 53 (#1)

      changing line 53 to:

      $cookie_jar->HTTP::Cookies::extract_cookies($response);

      give the same error:

       Can't call method "HTTP::Cookies::extract_cookies" on unblessed reference at ./GetJavaTest.pl line 53.

      What is an "unblessed reference" anyway?

        When I look at the web page it downloads, I find

        writeSessionCookie( 'oraclelicense', 'accept-securebackup-cookie' );

        in it

        Be nice if I could just add the cookie directly, like I can do with wget.

Re: Can't call method "extract_cookies" on an undefined value
by tangent (Parson) on Oct 17, 2015 at 11:57 UTC
    Normally, you shouldn't need to worry about extracting or setting the cookie at all. If $RevAddr sends a cookie it will be in cookie jar, and it will be returned automatically:
    my $ua = LWP::UserAgent->new; $ua->cookie_jar( {} ); # auto calls HTTP::Cookies->new my $response = $ua->get( $RevAddr ); $ua->timeout( 10 ); $ua->show_progress ( 1 ); $response = $ua->get( $ClickHere, ':content_file' => './eraseme.bin' ) +;
    However in this case the cookie is set by a javascript function, so the above will not work. But as you already know the cookie you can set it directly:
    my $ua = LWP::UserAgent->new; $ua->timeout( 10 ); $ua->show_progress ( 1 ); my $response = $ua->get( $ClickHere, ':content_file' => './eraseme.bin', 'Cookie' => 'oraclelicense=accept-securebackup-cookie', );
Re: Can't call method "extract_cookies" on an undefined value
by stevieb (Canon) on Oct 17, 2015 at 05:07 UTC

    Been many years since I've dealt with anything web-based, but can you show us the implementation (code) of extract_cookies()?

    I may be missing something here, but if not, knowing the details of that subroutine will allow us to know what it's expecting. It's apparent that your $response doesn't contain what extract_cookies() needs.

    You get expected output if you print ">$response<";?

      "extract_cookies" comes from:

      http://search.cpan.org/~gaas/HTTP-Cookies-6.01/lib/HTTP/Cookies.pm
      $cookie_jar->extract_cookies( $response ) The extract_cookies() method will look for Set-Cookie: and Set-Coo +kie2: headers in the HTTP::Response object passed as argument. Any of + these headers that are found are used to update the state of the $co +okie_jar.

      If I uncomment the print line, I get tons of html code.

        "extract_cookies" comes from: http://search.cpan.org/~gaas/HTTP-Cookies-6.01/lib/HTTP/Cookies.pm

        You will see that the synopsis on that page says:

        use HTTP::Cookies; $cookie_jar = HTTP::Cookies->new( file => "$ENV{'HOME'}/lwp_cookies.dat", autosave => 1, ); use LWP; my $browser = LWP::UserAgent->new; $browser->cookie_jar($cookie_jar);

        But you have not done that in your code. In fact nowhere have you assigned anything at all to the variable $cookie_jar as in the second line above. That is the cause of your error message. You should instantiate the jar as shown in the synopsis and then everything should be fine.

Re: Can't call method "extract_cookies" on an undefined value
by Anonymous Monk on Oct 17, 2015 at 09:31 UTC