Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have the following code. There is a subroutine named login. It logs in to a webpage with given credentials and grabs a cookie using LWP::Useragent. It would appear that the login subroutine is not storing the cookie in the useragent "cookie jar", since I am providing the script with a valid username and password, but I still get the "bad username or password error" from the script. This is the first time I have worked with subroutines, and I have had to move variables around to accomodate them, but I have screwed something up.

#!/usr/bin/perl use LWP::UserAgent; use Win32::GUI; $tmp = $ENV{"TEMP"}; my $font = Win32::GUI::Font->new( -name => "Comic Sans MS", -size => 12, ); $main=Win32::GUI::Window->new(-name=>"Main",-width => 400, -height => + 400); $main->AddButton(-name=>"BOK",-text=>"submit!",-pos=>[148,335], -onCl +ick=> sub {SUBMIT($single_entry_number, $user, $password)}); my $single_entry_number = $main->AddTextfield( -name => "single_entry_number", -text => "", -left => 10, -top => 10, -width => 200, -height => 25, -prompt => ["URL:", 80], ); my $user = $main->AddTextfield( -name => "user", -text => "", -left => 10, -top => 40, -width => 200, -height => 25, -prompt => ["Username:", 80], ); my $password = $main->AddTextfield( -name => "password", -text => "", -password => 1, -left => 10, -top => 65, -width => 200, -height => 25, -prompt => ["Password:", 80], ); $main->Show(); Win32::GUI::Dialog(); sub Window_Terminate { return -1; } sub SUBMIT { my $login = 1; my $ua = LWP::UserAgent->new(%uaoptions); $ua->cookie_jar( {} ); if ($login) { &login; } my $logintest= get("http://www.site.com/"); if ($logintest =~ m!you are now logged!sim) { } else { print "Bad username or password."; exit; } sub get { my $urls = shift @_; my $result = $ua->get($urls); if (!($result->is_success)) { die "Error: $urls produced " . $result->status_line; } return $result->content; } sub login { my $lpage = get("http://www.site.com/login.html"); my $chal; if ($lpage =~ /name=[\'\"]challenge[\'\"].*?value=[\'\"(.*?)\' +\"]/) { $chal = $1; } else { die "Couldn't find challenge in login.html"; } my %form = ( 'user', $user, 'password', $password, 'challange', $chal, 'action:log-in', "Log in..." ); my $result = $ua->post("http://www.site.com/login.html", \%form); } exit; }
If anyone sees what is wrong, or where I am confused please let me know. I appreciate it.

Replies are listed 'Best First'.
Re: Just between you, me and subroutines
by shemp (Deacon) on Aug 26, 2005 at 21:10 UTC
    One isssue being pointed out is that your subroutines get() and login() are defined within your SUBMIT() subroutine. I can't think of a good reason for that. Your indentation makes it difficult to see that that is whats going on. Try indenting like this: (i copied your SUBMIT function, and just indented it.)
    sub SUBMIT { my $login = 1; my $ua = LWP::UserAgent->new(%uaoptions); $ua->cookie_jar( {} ); if ($login) { &login; } my $logintest= get("http://www.site.com/"); if ($logintest =~ m!you are now logged!sim) { } else { print "Bad username or password."; exit; }
    With that indentation, its easy to see that you are missing the closing '}' in the else, which is why you need the final one at the end of your code.
    Another issue i'd like to bring up is that you are not useing strict or warnings, which will only bite you in the end.

    Also, you could benefit from passing variables into your subroutines, instead of having globals.

    I don't mean to bash your code, for the most part it looks like you're on the right track. As you said, you're pretty new to this, and myself and the other comments are trying to keep you from getting into some bad habits that will hinder you in the end.

    Good luck, hopefully we can be some help to you.


    I use the most powerful debugger available: print!
Re: Just between you, me and subroutines
by gargle (Chaplain) on Aug 26, 2005 at 20:08 UTC

    I'm a bit confused too! Please try to pay attention to the placing of your { and }. Indent clearly, make nice blocks of code. It will help you to debug.

    It appears by the way that your sub's are 'declared' in the SUBMIT subroutine. I don't think you mean to do that. Move the last } a bit up and place it right after the if statement that calls the &login routine.

      Sorry about the sloppy code. I am a bit new at this

      When I move the last } up to where you specified, it errors out and says that "get" on line 76 is an undefined value.