Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: CGI scoping question

by haukex (Bishop)
on May 10, 2019 at 18:26 UTC ( #1233573=note: print w/replies, xml ) Need Help??


in reply to Re^2: CGI scoping question
in thread CGI scoping question

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.

Replies are listed 'Best First'.
Re^4: CGI scoping question
by WoodyWeaver (Monk) on May 10, 2019 at 18:47 UTC
    Good point. It should help with my enlightenment, as well as make it easier for others to see and comment. I've stripped out what is clearly not relevant, and left just bits of the global block and the subroutine. It does still reflect the issue. Error log complains about some undefined $term in a print statement, which is ok. In an example where I see the issue, the source code of the rendered page has
    <!--bless({ ".charset" => "ISO-8859-1", ".fieldnames" => {}, ".parameters" => ["exec", "table", "orderby"], ".r" => bless(do{\(my $o = 94308584056848)}, "Apache2::RequestRec"), "escape" => 1, "param" => { exec => ["list"], orderby => [2], table => ["users"] }, "use_tempfile" => 1,
    yet
    <!-- variables (index, desc) are (10, 1) -->
    which would have been correct from a previous instance. The CGI:
    #!/usr/bin/perl -w # vim:sw=2:et:ic:sm:syn=perl:nu:si use Getopt::Std; use strict; use warnings; use DBI; use CGI; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use Data::Dump qw(dump); my $DEBUG = 1; # SQL fields for table users my @USER_FIELDS = qw(usertypes_idusertypes lastname firstname middlename suffix email +cellPhone authorizingParty lastAccountReview lastReviewRequest accoun +tsOwned); # Human names for user fields my %LABELS = ( 'usertypes_idusertypes' => 'Type of User', 'lastname' => 'Last Name', 'firstname' => 'First Name', 'middlename' => 'Middle Name', 'suffix' => 'Suffix', 'cellPhone' => 'Cell', 'email' => 'Email Address', 'authorizingParty' => 'ID for Authorizing Party', 'lastAccountReview' => 'Date Accounts Last Validated', 'lastReviewRequest' => 'Latest Date for Account Revalidation', 'accountsOwned' => 'Number of Accounts Owned' ); my %opts; my $OPTIONS = 'h'; my $optsStat = getopts( $OPTIONS, \%opts ); # sanity checks go here; provide prompt and set $optsStat = 0; if ( ( $optsStat != 1 ) or ( $opts{'h'} ) ) { HELP_MESSAGE(); exit; } my $q = CGI->new; print '<!--', dump($q), "-->\n"; print $q->header, $q->start_html( -title => 'Update identity user base for identity management store' +, -author => 'woody.weaver@us.ibm.com', ), $q->h1('Manage users and other lists for the OMS master repository') +, $q->p('Web interface for managing users in the OMS centralized back +end repository'), "\n"; if ($DEBUG) { print $q->hr; print $q->p('Debugging: '), $q->Dump, $q->hr; } 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'] ), '<p>', $q->submit, '<p>', $q->hr; my $exec = $q->param('exec'); my $table = $q->param('table'); if ($exec) { # ok, so this should be a jump table. Sue me. if ( $exec eq 'list' ) { if ( $table eq 'users' ) { listUsers(); } else { print "Sorry, I don't know how to list the table $table\n"; } } else { print "Sorry, I don't understand the action $exec\n"; } } ## end if ($exec) print "<p>", $q->submit; print $q->hr, $q->end_form, "\n", $q->end_html; sub listUsers { my $dbh = DBI->connect( 'DBI:mysql:identity', 'idmanagement', + '' ) or die "Cannot connect: $DBI::errstr"; my $preparestr = 'SELECT * FROM usertypes'; my $sth = $dbh->prepare($preparestr) or die "Can't prepare $p +reparestr: $dbh->errstr()"; $sth->execute() or die "Can't execute $preparestr: $dbh->errstr()"; my %usertypes; while ( my $ref = $sth->fetchrow_arrayref() ) { $usertypes{$$ref[0]} = $$ref[1]; } $preparestr = 'SELECT idusers, category'; foreach (my $i=1; $i<=$#USER_FIELDS; $i++) { $preparestr .= ", $USER_FIELDS[$i]"; } $preparestr .= ' FROM users, usertypes WHERE usertypes_idusertypes = usertypes.idusertypes ORDER by '; # what should we order the table by? die "q got killed!" unless defined $q; 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; $preparestr .= $USER_FIELDS[$index]; $preparestr .= ' DESC' if $desc == 1; print "\n<!-- Preparing $preparestr -->\n" if $DEBUG; $sth = $dbh->prepare($preparestr) or die "Can't prepare $preparestr: + " . $dbh->errstr(); $sth->execute() or die "Can't execute $preparestr: ".$dbh->errstr(); print 'Found ', $sth->rows, " users in table\n"; my $subtitle = 'Listing of Users, ordered by '; $subtitle .= $LABELS{$USER_FIELDS[$index]}; $subtitle .= ' (descending)' if $desc == 1; print $q->h2($subtitle), "\n<table border>\n\t<tr><th>ID</th><th>Typ +e"; 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"; while ( my $ref = $sth->fetchrow_arrayref() ) { my $firstone = 1; print "\t<tr>"; foreach my $term (@$ref) { if ($firstone) { print "<td><a href=\"editUser.pl?iduser=$term\">$term</a></td> +"; $firstone = 0; } else { print "<td>$term</td>"; } } print "</tr>\n"; } print "</table>\n"; } ## end sub listUsers sub HELP_MESSAGE { print <<EOH; usage: $0 [-$OPTIONS] -h this message EOH return; }

Log In?
Username:
Password:

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

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



    Results (80 votes). Check out past polls.

    Notices?