kamila
kamila

Reputation: 74

Perl attempts to use wrong package

i am very new to perl. I'm leraning how to use Mechanize package. I have example page. I don't know if it is good for learning cause it works in a strange way but still, i want to understand what's happening right now.

First, i have written script which is getting directly content under page1:

my $mech = WWW::Mechanize::Frames-> new();
my $mechFr = WWW::Mechanize::Frames-> new();
my $url="page1 url";

$mech->get($url);

my @frames = $mech->get_frames();
$mechFr= $frames[0];
print $mechFr->content();

And this works fine. Then i was trying to get to this page from page0 and i have written this:

my $mech = WWW::Mechanize::Frames-> new();
my $mechFr = WWW::Mechanize::Frames-> new();
my $url="page0url";

$mech->get($url);
$mech=$mech->follow_link(text_regex => qr/page1like/);
print $mech->content();

my @frames = $mech->get_frames();
$mechFr= $frames[0];
print $mechFr->content();

Content under mech afeter following link looks ok (i really am on page1), but then i get error Can't locate object method "get_frames" via package HTTP:Headers" at (...)/Message.pm line 694". My package list is the same in both scripts:

use strict;
use warnings;
use WWW::Mechanize::Frames;

So my question is, what am i doing wrong?

Upvotes: 0

Views: 206

Answers (1)

Hunter McMillen
Hunter McMillen

Reputation: 61512

According to the WWW::Mechanize documentation $mech->follow_link(...) returns an HTTP::Response object, you assign this result into your variable $mech. This effectively replaces the existing value of $mech with an HTTP::Response object.

The HTTP::Response module has no method get_frames() as Perl correctly tells you.

You need to store your HTTP response in a separate variable:

my $mech = WWW::Mechanize::Frames-> new();
my $mechFr = WWW::Mechanize::Frames-> new();
my $url="page0url";

$mech->get($url);
my $http_response = $mech->follow_link(text_regex => qr/page1like/);
print $http_response->content();

my @frames = $mech->get_frames();
$mechFr= $frames[0];
print $mechFr->content(); 

Upvotes: 2

Related Questions