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

Hi Monks!

I have this code and if a use STRICT it breaks, the problem is at the $input{'username'}, could anyone explain what I am missing?
Thanks!

#!/usr/bin/perl use strict; use warnings; use CGI qw(:cgi-lib); use CGI::Carp qw(fatalsToBrowser); #get input &ReadParse(*input); #assign variables my $loginame= $input{'username'}; my $p_word=$input{'password'}; print "Content-type: text/html\n\n"; print "<HTML><BODY>"; print "***$loginame AND $p_word***"; exit;

Replies are listed 'Best First'.
Re: Hash Problem
by jdporter (Paladin) on Mar 08, 2007 at 15:46 UTC

    The %input hash is a global variable, so you should declare it before you call ReadParse like so:

    our %input;

    Read more about our.

    A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re: Hash Problem
by cLive ;-) (Prior) on Mar 08, 2007 at 16:14 UTC
    Err, is there any reason why you're not using CGI.pm proper? It saves you even more effort.
    #!/usr/bin/perl use strict; use warnings; use CGI ':all'; use CGI::Carp qw(fatalsToBrowser); #assign variables my $loginame= param('username'); my $p_word=param('password'); print header(); print start_html(); print "***$loginame AND $p_word***"; print end_html(); exit;
    I prefer the OO version, but have not used it to keep the code looking as similar to yours as possible.
Re: Hash Problem
by ikegami (Patriarch) on Mar 08, 2007 at 15:47 UTC
    You're using package variable input without fully qualifying it. our %input; will disable the error.
Re: Hash Problem
by JediWizard (Deacon) on Mar 08, 2007 at 15:57 UTC

    The above responses will "fix" the error. However, if I were you, I would reconsider why you are passing a glob into ReadParse, as opposed to reference. From the simple example shown, you may even consider simply having ReadParse return a hash and take no input. As I rule, I try to avoid global variables, except when necessary (which it is not in the example given).

    #get input my(%input) = &ReadParse(); ## OR my(%input) = (some_input => 'for the sub'); &ReadParse(\%input);

    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

      All good advice, but unfortunately the poster is constrained by the fact that ReadParse is part of CGI.pm. And the author of CGI.pm was constrained by the fact that ReadParse was written to be a drop-in replacement for the function of the same name from cgi-lib.pl. If you're interested, the code in CGI.pm looks like this:

      sub ReadParse { local(*in); if (@_) { *in = $_[0]; } else { my $pkg = caller(); *in=*{"${pkg}::in"}; } tie(%in,CGI); return scalar(keys %in); }

      Of course, ReadParse was only included in CGI.pm to ease the transition of moving code from cgi-lib.pl to CGI.pm. There's no reason at all why people should be using it these days. If you want to get a hash containing all the parameters (which is kind of what ReadParse does), then CGI.pm provides the far simpler and more flexible Vars function. And in most cases, you probably just want to be using param anyway as others have mentioned.

      ReadParse wasn't written by the OP. It's part of CGI. It's there for backwards compatibility with cgi-lib, CGI's predecessor. (Needless to say, it shouldn't be used in new scripts.)
        Should I use:
        my $loginame=param("loginame");
        my $p_word=param("p_word");
        instead?
      If a do this way
      #get input
      my(%input) = &ReadParse();
      I am not getting the values of $loginame and $p_word.