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;

In reply to Re^2: passing variables to a sub by ag4ve
in thread passing variables to a sub by ag4ve

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.