Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

CGI/JSON and explicit package names.

by flynn7312 (Acolyte)
on Jul 14, 2015 at 14:15 UTC ( [id://1134744]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Guys, The below code throws the well known "Global symbol requires explicit package name", and i have no idea why. Please enlighten me.
#!/usr/bin/perl use strict; use warnings; use JSON; use CGI; my $cgi = CGI->new; print $cgi->header('application/json;charset=UTF-8'); my %errors; my %data; if ( my $lparnmame = $cgi->param('lparname')) { if ( $lparname eq "") { $errors{'lparname'}='Lparname is required'; } } if ( my $from = $cgi->param('from')) { if ( $from eq "") { $errors{'from'}='From is required'; } } if ( my $to = $cgi->param('to')) { if ( $to eq "") { $errors{'to'}='To is required'; } } if ( my $email = $cgi->param('email')) { if ( $email eq "") { $errors{'email'}='Email is required'; } } if (%errors) { $data{'success'}=0; $data{'errors'}=\%errors; } else { $data{'success'}=1; $data{'message'}='Success'; } my $json=JSON->new; print $json->encode(\%data);

Replies are listed 'Best First'.
Re: CGI/JSON and explicit package names.
by marto (Cardinal) on Jul 14, 2015 at 14:20 UTC

    You have:

    if ( my $lparnmame = $cgi->param('lparname')) { if ( $lparname eq "") { $errors{'lparname'}='Lparname is required'; } }

    Note my $lparnmame later used as $lparname

    Update: slightly cleaned up version:

    if ( my $lparname = $cgi->param('lparname') ){ if ( $lparname eq " " ){ $errors{'lparname'}='Lparname is required'; } }

      Exactly. And that’s why the OP’s inclusion of use strict — which the monks are always recommending — is such a good idea:

      Let the compiler find and flag the typo before the script engenders a runtime debug-fest!

      BTW, hello flynn7312, and welcome to the Monastery!

      Update: Improved wording. Thanks marto.

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

On the importance of reading and pasting error messages
by 1nickt (Canon) on Jul 14, 2015 at 14:42 UTC

    Another great example of why it's important to actually paste the actual error message when asking a question.

    Your error message was (when I pasted your code into my boilerplate)

    Global symbol "$lparname" requires explicit package name at 1134744.pl + line 21. Execution of 1134744.pl aborted due to compilation errors.

    When it tells you that, it means you are using a variable that hasn't been declared. To you that doesn't make sense because you can't see the one-character typo no matter how long you stare at your screen. (If it sounds like I am speaking from experience, it is because I am speaking from experience.)

    So you say to yourself "Self, I know I declared my $lparname, dammit! I'm off to PM to ask for help (without pasting the error message) ..."

    NO!! Don't do it!

    Instead, say to yourself, "Self, I know I declared my $lparname, dammit! But if Perl says I didn't, I guess the first thing to do is have look at the statement where I declared it." And then when you get there, you know there's going to be a typo, because Perl just told you, so you can see it!

    HTH :-)

    The way forward always starts with a minimal test.

      Actually seeing it can sometimes be surprisingly hard even if you are told exactly where to look.

      The simplest thing to do might be to copy the variable name from its declaration, and paste it over the complaint. Maybe it was an 'rn' vs 'm' or some arcane magic the cat typed in and you just can't see it, but either way copy-paste will fix it.

      If such things continue to be an issue, I recommend changing the editor and/or console to either increase the font size or use a different font with more distinct characters.

        the editor/ console thing might be the root of all the problems here, since i have to use the standard aix vi an a vt console.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-19 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found