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

hello, i wrote a rudimentary log on script that pretty much just checks to see if a file is there, and if it is it pulls the username and pw from the file and compares them to the ones entered on an html form. for some reason it doesnt work. here is the code:
#!/usr/local/bin/perl -wT use strict; use CGI qw(:all); # used to store username my $USERNAME = param('name'); print header; # the submit parameter is only passed if this program was executed by +the submit button if (defined param('submit')) # if its called by the form then save use +r info and display add inventory page { my $loginPath = $USERNAME . ".txt"; if (open(USER, "<$loginPath")) { while (my $lineData = <USER>) { chomp $lineData; my ($name, $password) = split /:/, $lineData; if ($name eq $USERNAME && $password eq param('pw')) { saveInfo(); writeAddInv(); } else # if the user is not in the list then display the log +in page { $printErrMsg = 1; # set to true (user is not in list) writeLogin(); } } } else { $printErrMsg = 1; # set to true (user is not in list) writeLogin(); } close(USER); } else # if not called by form { writeLogin(); # display login page } and here are the errors im getting: Global symbol "$loginPath" requires explicit package name at /opt2/gri +ffass/httpd/cgi-bin/tapeAndLabelLogin.cgi line 488. Global symbol "$loginPath" requires explicit package name at /opt2/gri +ffass/httpd/cgi-bin/tapeAndLabelLogin.cgi line 488. Global symbol "$lineData" requires explicit package name at /opt2/grif +fass/httpd/cgi-bin/tapeAndLabelLogin.cgi line 488. Global symbol "$lineData" requires explicit package name at /opt2/grif +fass/httpd/cgi-bin/tapeAndLabelLogin.cgi line 488. Global symbol "$name" requires explicit package name at /opt2/griffass +/httpd/cgi-bin/tapeAndLabelLogin.cgi line 488. Global symbol "$password" requires explicit package name at /opt2/grif +fass/httpd/cgi-bin/tapeAndLabelLogin.cgi line 488. syntax error at /opt2/griffass/httpd/cgi-bin/tapeAndLabelLogin.cgi lin +e 539, near "my ($name, $password) = split /:" (Might be a runaway multi-line // string starting on line 488) Search pattern not terminated at /opt2/griffass/httpd/cgi-bin/tapeAndL +abelLogin.cgi line 539. [Fri Aug 19 10:46:35 2005] [error] [client 64.80.205.90] Premature end + of script headers: /opt2/griffass/httpd/cgi-bin/tapeAndLabelLogin.cg +i
i cant figure out why this isnt working. ive declared all the variables using 'my', and the search pattern works on another server that uses this script.

Replies are listed 'Best First'.
Re: problem with explicit package name
by davorg (Chancellor) on Aug 19, 2005 at 15:12 UTC

    The point with use strict 'vars' (which is what you seem to be falling over here) is not that you have to declare variables, but that you have to declare them in the right scope.

    I can see declarations of the variables that you are getting errors on, but the errors are far away in different part of the program (on line 488). It looks like your variable names have gone out of scope by that point.

    You either need to define your variables in a wider scope which covers all of their usage, or (far better) you need to pass the data into subroutines using parameters.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: problem with explicit package name
by Transient (Hermit) on Aug 19, 2005 at 15:15 UTC
    After adding a
    my $printErrMsg;
    and
    sub writeLogin { }
    That code compiled fine for me. It's probably something above that section of code.
Re: problem with explicit package name
by Ven'Tatsu (Deacon) on Aug 19, 2005 at 16:25 UTC
    I think the real cause of the errors is this:
    syntax error at /opt2/griffass/httpd/cgi-bin/tapeAndLabelLogin.cgi lin +e 539, near "my ($name, $password) = split /:" (Might be a runaway multi-line // string starting on line 488)
    Check like 488 for a syntax error like an extra or mising /, or something similar. If it's not there try looking a few lines before or after that.
    The gist of the error is that there is a / that is being interpreted as starting a string (or posibly a regular expression) and so every thing between that and the first / on line 539 is being treated as contained in a string. All the my declarations are then not being interpreted as code, but when perl sees the variable it tries to interpret it and does not find that it's been declaried and throws a strict error. Basicly perl sees something like this
    print "some text\n; my $foo; ";
    it interprets it as
    print "some text\n;\nmy " . $foo . ";\n";
    and complains that $foo is not declaired.

    perl however then doesn't find another missing string delemiter and tries to be as helpful as posibal by telling you where it had the error parsing and where it thinks the error originated.