Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

string initialization error

by *alexandre* (Beadle)
on Dec 21, 2018 at 13:46 UTC ( #1227557=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I'm currently rewriting perl script, and I got an error while I'm been running it. Here is my code
sub loadPage { my $page = ""; $page = $query->param("page"); if ($page eq 'main') { loadMainPage(); return 1; } }
My error message is the following : Fri Dec 21 14:32:11 2018 error client Fri Dec 21 14:32:11 2018 helloWorld.cgi: Use of uninitialized value $page in string eq at /home/alexandre/apache/cgi-bin/helloWorld.cgi line 41. Any idea ? Thanks in advance. Alexandre Jaquet

Replies are listed 'Best First'.
Re: string initialization error
by perlancar (Hermit) on Dec 21, 2018 at 14:25 UTC

    It looks like you set $page to "" and then possibly set it to undef in the following line if there is no query parameter named "page". By the time perl compares $page to 'main', it's getting an undef value.

    You probably meant something like: my $page = $query->param("page") // '';

    or: my $page = $query->param("page") || '';

      or: my $page = $query->param("page") || '';

      Caveat: That will also assign '' to $page if $query->param("page") returns 0 or "0" (the same applies to hippo's answer).

      TIMTOWTDI: Another alternative to the code from the OP may be (at least on Perl v5.12 and up):

      sub loadPage { my $page = $query->param("page"); if ( length($page) && $page eq 'main' ) { loadMainPage(); return 1; } }
Re: string initialization error
by hippo (Bishop) on Dec 21, 2018 at 15:15 UTC
    my $page = ""; $page = $query->param("page");

    Don't do that if you want to test $page afterwards. Instead do something like this:

    my $page = $query->param ('page') || '';

    This way $page will always be defined, even if you don't have a param with the name "page".

Re: string initialization error
by pryrt (Abbot) on Dec 21, 2018 at 14:03 UTC

    providing a Short, Self-Contained, Correct Example would help, since there are obviously not 41 lines in your snippet. I am assuming "line 41" is the if ($page eq 'main') { line. In which case, the warning is there because $page is not defined. That would happen if, for example, you were not given a "page" parameter when the CGI script was run (ie, the URL didn't include page=blah, or you didn't pass that on the command-line when debugging your CGI script).

Re: string initialization error
by soonix (Canon) on Dec 21, 2018 at 14:00 UTC
    the message says the error occured in "line 41". Your code has 8 lines. I assume there are about 37 lines above these 8 lines, and probably some below. The cause of the error lies in the part that you didn't post, or in the way you call your script (e.g. not calling it with the needed parameter(s)) - which you didn't post either.
      Hi guys, First thx for the interest you are been taking, here's a full cgi sample I've rewrite to bbe simple
      #!/usr/bin/perl -w use strict; use warnings; use CGI; my $query = CGI->new ; loadPage (); sub loadPage { my $page = ""; $page = $query->param("page"); if ($page eq 'main') { print "Content-Type: text/html\n\n"; print "main page to load"; }else { print "Content-Type: text/html\n\n"; print "error"; } }
      this code work well now, I'm searching on other modules to see what's wrong thx Alexandre Jaquet
        Almost, you pass arguments to subroutines, not work on globals. see cgi101/Re: Object Identifier? (red flags, more subs)
        #!/usr/bin/perl -- use strict; use warnings; use CGI; Main( @ARGV ); exit( 0 ); sub Main { my $q = CGI->new; my( $headers, $body ) = loadPage( $q ); print $headers, $body; } sub loadPage { my( $query ) = @_; if( authenticate( $q ) ){ my $page = $q->page || 'default'; return ThisPage( $pms, $q ) if $page eq 'thisone'; return ThatPage( $pms, $q ) if $page eq 'thatone'; return DefaultPage( $pms, $q ); } else { return UnauthorizedPage( $pqms, $q ); } }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-08-18 13:40 GMT
Find Nodes?
    Voting Booth?

    No recent polls found