in reply to passing variables to a sub

Sorry, I've looked at your code several times and it looks like nonsense to me. For example, it makes no sense to pass $sortby into sub ownertbl and then assign it to itself.

I suggest you start your script with:

use strict; use warnings;
then define all your subroutines, then your mainline (including any file-scope lexical variable declarations). For example:
use strict; use warnings; # Put your subs here before any file-scope variables. # That makes it clearer that each sub does not depend on # anything other than the parameters passed to it. sub ownertbl { my ( $sortby, $ascdesc, $letter ) = @_; # added leading "my" # ... } # put your other subs here... # Now define your lexical variables after the subs... my ($ownsel, $sortby, $letter, $ascdesc, $t); if ( $cgi->param ) { if ( defined ( $cgi->param( 'sortby' ) ) ) { $sortby = $_; # this looks wrong to me } else { $sortby = "name"; } if (defined ( $cgi->param( 'ascdesc' ) ) ) { $ascdesc = $_; # ditto } else { $ascdesc = "desc"; } if (defined ( $cgi->param( 'letter' ) ) ) { $letter = $_; # ditto } else { $letter = " "; } ownertbl( $sortby, $ascdesc, $letter ); # now these three variables + are properly passed to the sub } else { homepage(); }

Replies are listed 'Best First'.
Re^2: passing variables to a sub
by ag4ve (Monk) on Oct 16, 2010 at 05:13 UTC

    ok, i thought this would be overkill but below is what i have (i have changed some names to protect the innocent but it is all intact). the html::template works fine. and i'd prefer to keep two templates so that i can do some type of js on the results page to popup the search page (though i suppose i could do it with one and use an tmpl_if, doing them separate just seemed logical and would do slightly less processing).

    also, i think you have a point of if not defining $_ so i'll change it (might be my issue). though i still think i'm off on my variable scopes...

    you might have a point in defining my subs with my variables first, i just figured i'd go ahead and post the whole thing to confirm:

    also, there is no need to have the sql connect and disconnect at the beginning and end, but it works for now so until i get this figured out, i figured i'd keep this as is. and there are major security issues with this right now that i'll take care of before i put this up. with those two points aside, i'll take any suggestions you have to make this work / simpler / better.

    #!/usr/bin/perl -w use strict; use DBI; use CGI qw( -no_undef_params :standard); use HTML::Template; my $param = ""; my ($ownsel, $sortby, $letter, $ascdesc, $t); my $dbh = DBI->connect('DBI:mysql:dbname;host=localhost', 'someone', 'something') or die "Database connection: $!"; my $cgi = CGI->new; my $base = $cgi->url; if ( $cgi->param ) { if ( defined ( $cgi->param( 'sortby' ) ) ) { $sortby = $_; } else { $sortby = "name"; } if (defined ( $cgi->param( 'ascdesc' ) ) ) { $ascdesc = $_; } else { $ascdesc = "desc"; } if (defined ( $cgi->param( 'letter' ) ) ) { $letter = $_; } else { $letter = " "; } ownertbl( $sortby, $ascdesc, $letter ); } else { homepage(); } sub homepage { $t = HTML::Template->new( filename => 'home.tmpl', global_vars => '1' ); my @alphabet = (); for ("A" .. "Z") { my %letter; $letter { 'letter' } = $_; push(@alphabet, \%letter); } $t->param( alphabet => \@alphabet, base => $base, sortby => $sortby, ascdesc => $ascdesc, letter => $letter ); print $cgi->header; print $t->output; } sub ownertbl { ( $sortby, $ascdesc, $letter ) = @_; my $sort = ""; $sort = "ORDER BY " . $sortby . $ascdesc; if ( defined ( $letter ) ) { $letter = "WHERE " . $sortby . " LIKE '" . $letter . "%'"; } else { $letter = " "; } my $query = "SELECT field1, field2, field3, field4, field5 FROM table $sort $letter"; my $sth = $dbh->prepare( $query ); $sth->execute( ); my @ownsel = (); push @{$ownsel}, $_ while $_ = $sth->fetchrow_hashref(); $t = HTML::Template->new( filename => 'ownertbl.tmpl' ); $t->param(ownerall => \@ownsel, base => $base, letter => $letter ); print STDERR "base = $base, sort = $sort, sortby = $sortby, ascdes +c = $ascdesc, letter = $letter\n"; print $cgi->header; print $t->output; } $dbh->disconnect;