Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Completely lost - 'Use of uninitialized value in pattern match...' error :(

by n00dles (Novice)
on Sep 01, 2006 at 19:46 UTC ( [id://570816]=perlquestion: print w/replies, xml ) Need Help??

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

I have written this little app, really just for somthing to. but I keep getting the following error.
Use of uninitialized value in pattern match (m//) at ./usercheck.pl li +ne 35. Use of uninitialized value in pattern match (m//) at ./usercheck.pl li +ne 35.
Repeated a rather large amount of times. I think that @source is not beening assigned any values? and using perl -d did not reap much help but im still learning my way round the debuger.
#!/usr/bin/perl -w # by n00dles use IO::Socket; use strict; my $target = 'localhost'; my $userlist = 'uselist.txt'; my $timeout = 40; print "Content-type: text/html\n\n"; print "<BODY BGCOLOR=#000000 Text=#008000 link=#00c000 vlink=lightblue +>"; print "<PRE>\n"; print "<H2>Active Apache user accounts</H2>\n\n"; print "<B>Loading from file $userlist</B>\n"; open(USERS, "< $userlist") or die "cannot open file: $!\n"; my @users =<USERS>; close(USERS); print "<B>User list $userlist loaded!</B>\n"; print "<B>Starting Scan please wait...</B>\n"; for (my $i = 0; $i < scalar(@users); $i++) { my $socket = new IO::Socket::INET ( peerAddr => $target, peerPort => '80', Proto => 'tcp', Timeout => $timeout,); die "Socket failed at $target\n" unless $socket; print $socket "GET http://$target/~$users[$i] HTTP/1.0\r\n\r\n +"; my @source = <$socket>; foreach my $line ($source[2]) { if ($line =~/302 Found/ || $line =~ /200 OK/ || $line +=~ /403 Forbidden/ || $line =~ /301 Moved Permanetly/) { print "Found: $users[$i]"; } } close($socket); } print "</BODY></HTML>";
Any wisdom you can offer would be greatly recived. I have made a command-line version of this app that uses @ARGV to take the target and userlist from the prompt witch works fine, but this version does not.

2006-09-03 Retitled by g0n, as per Monastery guidelines
Original title: 'Completely lost :('

Replies are listed 'Best First'.
Re: Completely lost - 'Use of uninitialized value in pattern match...' error :(
by merlyn (Sage) on Sep 02, 2006 at 14:25 UTC
    Please replace:
    my $socket = new IO::Socket::INET ( peerAddr => $target, peerPort => '80', Proto => 'tcp', Timeout => $timeout,); die "Socket failed at $target\n" unless $socket; print $socket "GET http://$target/~$users[$i] HTTP/1.0\r\n\r\n +"; my @source = <$socket>;
    with:
    use LWP::Simple; my $source = get "http://$target/~$users[$i]";
    No sense in trying to reinvent code that has been done, done again, done over, redone, and finally made fast.

    Oh, and then you won't need to check return statuses... you'll either have the content, or undef. So the rest of your code also becomes simpler.

    Right tool for the job.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Completely lost - 'Use of uninitialized value in pattern match...' error :(
by japhy (Canon) on Sep 01, 2006 at 19:49 UTC
    It's not an error, it's a warning. But yes, it's saying that $line (and thus, $source[2]) is uninitialized. You should probably check to see if there's anything in @source before you use it.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Completely lost - 'Use of uninitialized value in pattern match...' error :(
by GrandFather (Saint) on Sep 01, 2006 at 20:12 UTC

    Try adding

    next if ! defined $line;

    to the foreach loop before the regexen.


    DWIM is Perl's answer to Gödel
Re: Completely lost - 'Use of uninitialized value in pattern match...' error :(
by chromatic (Archbishop) on Sep 01, 2006 at 23:42 UTC

    I blame the line:

    foreach my $line ($source[2]) {

    It doesn't make any sense to loop over it, for starters. As well, if you're reading from a socket (as japhy implies), how do you know how many lines you'll get? I don't see any reason to assume you'll get at least three, so you're probably skipping over the real data in the first couple of elements (and is $/ on your platform the same as the end-of-line of your network transfer protocol?). Try:

    for my $line ( @source ) {

    ... as a better approach.

Re: Completely lost - 'Use of uninitialized value in pattern match...' error :(
by McDarren (Abbot) on Sep 01, 2006 at 22:19 UTC
    Other monks have addressed the source of your warning messages, but here are a couple of other tips:
    • If you are creating dynamic HTML, then you really should use the CGI module.
    • You are checking the return value of open, which is good. However, the three argument form is generally recommended over the two argument form. So instead of:
      open(USERS, "< $userlist") or die "cannot open file: $!\n";
      you should write something like:
      open USERS, '<', $userlist or die "Cannot open $userlist:$!\n";
    • Your first for loop:
      for (my $i = 0; $i < scalar(@users); $i++) {
      could be written more "perlishly" (and more cleanly) as:
      for my $user ( @users ) {

    Hope this helps,
    Darren :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://570816]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-29 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found