Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

CGI scoping question

by WoodyWeaver (Monk)
on May 10, 2019 at 16:57 UTC ( #1233566=perlquestion: print w/replies, xml ) Need Help??

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

Perl script has
use CGI; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use Data::Dump qw(dump);
In a global block
my $q = CGI->new; print '<!--', dump($q), "-->\n";
and a little later
if ($DEBUG) { print $q->hr; print $q->p('Debugging: '), $q->Dump, $q->hr; }
and in particular there is a parameter being passed, "orderby", which is set to 10, e.g
<li><strong>orderby</strong></li> <ul> <li>10</li>
A couple of the parameters direct execution of a subroutine, and in that subroutine
my $index = $q->param('orderby'); my $desc = $q->param('desc'); $index = 1 if not defined $index; print "\n<!-- variables (index, desc) are ($index, $desc) -->\n" if +$DEBUG;
but <!-- variables (index, desc) are (1, ) --> Why would index not be 10? What am I missing?

Replies are listed 'Best First'.
Re: CGI scoping question
by huck (Prior) on May 10, 2019 at 17:35 UTC

    You have seemed to have left quite a bit out of your description.

    but based on what you did say let me propose an explanation. If i expected that

    my $index = $q->param('orderby');
    was to give me the value of a cgi parameter called orderby i would expect to see a html section something like
    <input type="text" name="orderby" value="10" />
    But i do not see you posting such a section.

      I did try to omit pieces that seemed irrelevant, so I didn't include how the parameters were being set. Several parameters are being set through classical forms, e.g.
      print $q->start_form(), 'Please choose an action: &nbsp;', $q->popup_menu( -name => 'exec', -values => ['add', 'list', 'search' +] ), $q->popup_menu( -name => 'table', -values => ['users', 'VPN', 'VMacc +ounts', 'supporting tables'] );
      This orderby and desc fields are not -- they are being manually set via
      print $q->h2($subtitle), "\n<table border>\n\t<tr><th>ID</th><th>Type +"; foreach (my $i=1; $i<=$#USER_FIELDS; $i++) { my $flags = '?exec=list&amp;table=users&amp;orderby='.$i; if ($i == $index) { $flags .= '&amp;desc=1'; } print "</th><th><a href=\"$flags\">$LABELS{$USER_FIELDS[$i]}</a>"; } print "</th></tr>\n";
      (The titles on the table lets you sort by that field, and if you click on an already-sorted field it reverses it.)

      I didn't think that relevant, because its all upstream: at the invocation I'm looking at, the value of orderby is being shown in the $q->Dump output; yet its vanished by the time the first subroutine is called. That is, I've got a print statement, a couple of local assignments, two if's that call listUsers(), a little DB work at the top of sub listusers, and then orderby has vanished. I am assuming there is some sort of global variable passing between CGI instances that I don't know about? ... well, I'm confused.

        I also think you haven't yet provided enough information.

        I didn't think that relevant

        To determine what is relevant and what isn't, remove a piece of code. If the problem remains, that piece of code wasn't relevant, if the problem goes away or changes, that piece of code probably was relevant, so keep it in. One of the important aspects of a Short, Self-Contained, Correct Example is that it runs, so that we can download and run the code ourselves to reproduce the problem on our end - you'll get help much quicker this way, and with less guessing :-)

        Also, in case you're not, Use strict and warnings.

        Several parameters are being set through classical forms
        ...
        This orderby and desc fields are not
        If you are sending a form to your script then the parameters in the URL are not available to CGI's param(). You need to use url_param()
        my $index = $q->url_param('orderby');
Re: CGI scoping question
by poj (Abbot) on May 10, 2019 at 18:05 UTC
    Why would index not be 10

    Perhaps $q is not defined, add this line to check

    die "\$q not defined" unless defined $q; my $index = $q->param('orderby');
    poj
      I'm using $q in the global, so having it vanish in the subroutine would be interesting indeed. Added that check line, but no joy. Just now I had a case where
      <li><strong>orderby</strong></li> <ul> <li>10</li> </ul> <li><strong>desc</strong></li> <ul> <li>1</li>
      yet
      <!-- variables (index, desc) are (10, ) -->
      The previous instance of the CGI variable would have correctly had the desc parameter undefined, but *this* one seems to be set.
Re: CGI scoping question
by WoodyWeaver (Monk) on May 16, 2019 at 13:52 UTC
    One last point that might help a reader -- while the difference between $q->param() and $q->url_param() turned out to be interesting, I think the real issue with the CGI scoping came from the web server itself. I had
    <Directory /var/www/html/id/bin> SetHandler perl-script PerlResponseHandler ModPerl::Registry PerlOptions +ParseHeaders Options +ExecCGI </Directory>
    and that doesn't seem to like subroutines well. Switching to
    <Directory /var/www/html/id/bin> SetHandler perl-script PerlResponseHandler ModPerl::PerlRun PerlOptions +ParseHeaders Options +ExecCGI </Directory>
    and I haven't seen the problem since.

      See here regarding mod_perl and using variables in subroutines.

      poj

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2022-05-17 16:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (68 votes). Check out past polls.

    Notices?