Question

I get this error while using cookie_jar method:

Can't call method cookie_jar on an undefined value

Here is my code:

my $cookie_jar= new HTTP::Cookies;
my $ua=new LWP::UserAgent;
my %cookies= fetch CGI::Cookie;
my $encoded=$cookies{'SCred'};
$cookie_jar->set_cookie(1, "SCred", $encoded, "/", $SSO_DOMAIN,  "", 0, 0, 60*60, 0);
$ua->cookie_jar($cookie_jar); # I get error on this line

Any idea why I get this error?

Was it helpful?

Solution

I've tried your code (with strict, warnings and what I think are the required modules, with turning the free variables into strings):

kyle@indigo64 ~[home*]$ cat x.pl
use strict;
use warnings;
use HTTP::Cookies;
use LWP::UserAgent;
use CGI::Cookie;

my $ua         = new LWP::UserAgent;
my %cookies    = fetch CGI::Cookie;
my $encoded    = $cookies{'SCred'};

my $cookie_jar = new HTTP::Cookies;
$cookie_jar->set_cookie(
  1, "SCred", '$encoded',
  "/", '$SSO_DOMAIN',  "",
  0, 0, 60*60, 0
);
$ua->cookie_jar($cookie_jar);

print "ua: ",$ua,"\n";
print "ua->cookie_jar: ",$ua->cookie_jar,"\n";
mortis@indigo64 ~[home*]$ perl x.pl
ua: LWP::UserAgent=HASH(0x82f8cc8)
ua->cookie_jar: HTTP::Cookies=HASH(0x82f8b84)
kyle@indigo64 ~[home*]$ 

and it works. You might want to either post a fuller example, or are there lines between the '$ua = new...' and the '$ua->cookie_jar' lines where $ua is re-assigned or otherwise set to undef? If you print the value of '$ua' just before the call to cookie_jar you should see that it's undef, it must be being reset somewhere between the first assignment and where you are calling that method.

OTHER TIPS

Just to rule out any weird interactions, try the following:

my $cookie_jar = HTTP::Cookies->new;
my $ua = LWP::UserAgent->new;
my %cookies = CGI::Cookie->fetch;
my $encoded = $cookies{'SCred'};
$cookie_jar->set_cookie(
    1, "SCred", $encoded, "/", $SSO_DOMAIN,  "", 0, 0, 60*60, 0
);
$ua->cookie_jar($cookie_jar); # I get error on this line

Now, for some reason, $ua is undefined, which means the constructor call:

my $ua = LWP::UserAgent->new;

failed. I am not too familiar with fastcgi. However, LWP::UserAgent croaks on any failure in the constructor: I am not sure how you are reaching the line in question.

Have you checked the server logs? Total shot in the dark: Does the following add any useful information to the error log?

my $ua = eval { LWP::UserAgent->new }
    or warn "LWP::UserAgent->new failed: $@";

If that is the actual error you get, it's not a problem with cookie_jar. That just happens to be the first method you try to call. Check that you actually get an object when when make the user-agent.

Remove all the cookie stuff and try the agent method:

use strict;
my $ua = eval { LWP::UserAgent->new }
    or die "Could not make user-agent! $@";
$ua->agent("TestAgent");

If anything goes wrong in the constructor, you should be able to catch it. However, if your script isn't already die-ing, I think you have something else wrong. If LWP::UserAgent::new runs into a problem, it already croaks. The only thing it can return is a defined value that it has already called methods on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top