Skip to content

Instantly share code, notes, and snippets.

@willwillis
Created December 20, 2009 19:33
Show Gist options
  • Save willwillis/260607 to your computer and use it in GitHub Desktop.
Save willwillis/260607 to your computer and use it in GitHub Desktop.
package Website;
use Moose;
use WWW::Mechanize;
use Data::Dumper; # for debugging
our $VERSION = '0.01';
has 'login_url' => (is => 'rw', default => 'https://secure.loginz.com');
has 'user' => (is => 'rw', isa => 'Str', required => 1);
has 'pass' => (is => 'rw', isa => 'Str', required => 1);
has '_mech' => (is => 'rw');
#
# The problem I'm having is keeping state in Mech
# while bouncing between different methods. One method
# modifies $self->_mech, the Mechanized object
# but when the next method calls Mech object, it
# has forgotton that it is already logged in.
#
sub connect {
my $self = shift->new(@_);
my $mech = $self->_login();
my $page = $self->_get_home_page();
}
sub _login {
my ( $self, $user, $pass ) = @_;
my $login_url = $self->login_url();
print "Login URL is $login_url \n";
$self->_mech(WWW::Mechanize->new());
$self->_mech->get($login_url);
$self->_mech->submit_form(
form_number => 1,
fields => {
username => $user,
password => $pass
}
) || die $!;
# after login I'm taking to different page
# call it page2. Still OK up to here
return $self->_mech;
}
sub _get_home_page {
my $self = shift;
print $self->_current_uri; # what I expect
# this is when the error comes in.
# I loose my Mech state. instead of getting page2
# content, I get login screen.
# how do I keep persistance in $self->_mech ?????
print $self->_mech->content; # back to login page, not page2
my $mech_link = $self->_mech->find_link(
class => 'channeltitle' );
}
sub _current_uri {
my $self = shift;
return $self->_mech->uri;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment