CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
sql or perl sort

 



daveh71
stranger

Mar 21, 2001, 5:53 PM

Post #1 of 8 (797 views)
sql or perl sort Can't Post

i'm in the beginning stages of setting up a sql database. I will be using perl to access it and make web page. I'm planning on having a table of information that can be sorted by each column header.

question: would it be better to do a sql query each time i click a column header, or would it be better to grab all of the info into a multilevel array in perl and sort it from there?

any thoughts or links to areas that deal with this would be greatly appreciated.



Jasmine
Administrator / Moderator

Mar 21, 2001, 8:00 PM

Post #2 of 8 (796 views)
Re: sql or perl sort [In reply to] Can't Post

Will this be a browser-based program (CGI)? If so, then after the page is delivered, the program terminates the connection to the dbase is closed, and whatever is in your carefully developed multilevel array will go poof. To "click" again is to invoke the program again, so it will need to be requeried anyway.

Am I understanding what you're talking about?



daveh71
stranger

Mar 22, 2001, 9:52 AM

Post #3 of 8 (790 views)
Re: sql or perl sort [In reply to] Can't Post

Jasmine,
thanks for your reply. Yes, this is for a cgi browser program. I'm using cgi-lib.pl. I thought that it was possible to pass an array back through and recapture it.

sorta like::

if ($in{'cmd'} =~ /sort-name/i) {
@myary = $in{'myary'}
}

is this not possible?

thanks
david




Jasmine
Administrator / Moderator

Mar 22, 2001, 2:27 PM

Post #4 of 8 (787 views)
Re: sql or perl sort [In reply to] Can't Post

Sorry, but you can't pass a variable name back and "autorecall" its values.

If your array is moderate in size, you can probably post the entire array back to the program.



daveh71
stranger

Mar 22, 2001, 4:30 PM

Post #5 of 8 (785 views)
Re: sql or perl sort [In reply to] Can't Post

Sorry, you lost me there.
How would i post it back to the program.

It's strange that i can pass a string in a hidden value, but not an array.

Thanks for your help.

d



Jasmine
Administrator / Moderator

Mar 22, 2001, 8:16 PM

Post #6 of 8 (782 views)
Re: sql or perl sort [In reply to] Can't Post

No, not strange. Web pages are HTML, and HTML is not Perl. So it's not likely that one can speak the other language enough to store a data structure that's specific to a different language.

You could post it back to the program from a hidden tag, but you would need to stringify it.

my $string = join( /\t/,@array );

print <<EOF;
<INPUT TYPE="HIDDEN" NAME="stringifiedarray" VALUE="$string">
EOF

and then when the visitor clicks submit again, you can break it back up with something like this

my @array = split( /\t/, $in->param( 'stringifiedarray' ) );

I'll be the first to admit that this method is poor. Requerying is probably be the best (and safest) bet, because someone can fudge with the html source code (by saving the form locally and manually changing the hidden tag data).



daveh71
stranger

Mar 23, 2001, 8:10 AM

Post #7 of 8 (780 views)
Re: sql or perl sort [In reply to] Can't Post

very well explained.
Thank you for spending the time!!!

david



rGeoffrey
User / Moderator

Mar 27, 2001, 2:57 PM

Post #8 of 8 (773 views)
Re: sql or perl sort [In reply to] Can't Post

If you have data that does not change often, but is frequently called from perl, you can save your hash in a *.pl file and require it into your program. Thus you pay the penalty of creating the hash once, and then get to use it for as long as you want, or until you have to replace it with new data.

To do this you need to use Data::Dumper, write a perl file that contains the hash in a form you can use again, and then require that file into your program when you need it.

Here is a piece of code that will take a hash called %user and you will then be able to use $p_user which is a pointer to the hash. To work with a real hash again you can use the hash %{$p_user}.

The last line of a module or required file should be a true value so I have included the 1 to end the file.

You might have to refer to the pointer as $::p_user but I am too lazy to test it right now. If you are using packages it can get more complicated like $somePackage::p_user depending on where you do the requiring. These loose ends are left as an exercise for the reader.


Code
use Data::Dumper; 

my $helperfile = &Dumper (\%user);
$helperfile =~ s/\$VAR1/\$p_user/;
open (HFILE, ">$filename.pl") or die "could not write new required file $!";
print HFILE <<EOF;

use vars qw (\$p_user);
$helperfile

1;
EOF
close HFILE;

This will give a file that looks something like...


Code
use vars qw ($p_user); 
$p_user = {
'-' => 30,
'fred.com' => 10
};


1;

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?

(This post was edited by rGeoffrey on Mar 27, 2001, 2:06 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives