Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Passing Params

by Anonymous Monk
on Jan 17, 2001 at 00:50 UTC ( [id://52349]=perlquestion: print w/replies, xml ) Need Help??

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

I'm calling subroutines that are in a package from a main script. The subroutines use values that are submitted through a form. I can either read the form values in, in the main script and then pass them to the subroutines via a hash or simply instaniate the CGI module in each subroutine and read in the values in there. aka..
###Method 1 use CGI; my $query=new CGI; my $value=$query->('value'); my %param=(value=>$value); test(%param); package Test; sub test { my %param=@_; #do stuff } ##Method 2 ##main script test(); package Test; sub test { use CGI; my $query=new CGI; my $value=$query->('value'); ##do stuff; }
Basically, my question is what takes up less processing power and is faster?
1.instantiating the CGI module repeatedly or
2. create hashes everytime I want to pass values to my subroutine?

Obviously, I want my script to be able execute as quickly as possible. Note: I don't want to just pass the values in as regular vars. I'd rather pass a hash.
Thoughts?
Thanks.

Replies are listed 'Best First'.
(Ovid) Re: Passing Params
by Ovid (Cardinal) on Jan 17, 2001 at 01:20 UTC
    instantiating the CGI module repeatedly or
    Rarely, if ever, do you wish to instantiate a CGI object more than once. If you are using a POST request, the first instantiation of the CGI object reads the data from STDIN (unless you specify an alternate source of the data). Thus, CGI objects instantiated at a later date cannot access the data from STDIN.

    One simple way to create a hash is to do the following (untested):

    use CGI; my $query = CGI->new; my @names = $query->param; my %hash = map { $_, $query->param( $_ ) } @names;
    In newer versions of CGI.pm, there is a direct method of creating a hash of CGI name/value pairs, but I can't recall it offhand. Also, note that the above method only works with single values for each param. If you have multiple values, you could try the following (also untested):
    my %hash = map { my @vals = $query->param( $_ ); $_, \@vals } @names;
    Each hash value should then be a reference to an array of the values associated with each name.

    Cheers,
    Ovid

    Update: Three things:

    1. chipmunk has pointed out some problems with my untested code and improved upon it.
    2. I would not use %hash = $query->Vars; because of the null byte. That's bad juju that can get you in serious security trouble if you screw up!.
    3. chromatic made the point that he wasn't sure if CGI.pm caches the results of POST requests. Because of some problems that I have had with this issue before, I erroneously assumed that it didn't. I ran a test and instantiated two CGI objects and used Data::Dumper to output them to a browser. Apparently (as of CGI.pm version 2.74), CGI.pm does cache the output and it's available on subsequent requests. Of course, that really doesn't justify the overhead of instantiating multiple CGI objects.
    If you'd like to check the results, create a form with textfields, checkboxes, and whatever other widgets you would like. Use the POST method and point it at the following hack:
    #!c:/perl/bin/perl.exe -wT use strict; use Data::Dumper; use CGI; my $query = CGI->new; my @names = $query->param; my %hash = map { $_, $query->param( $_ ) } @names; # Clearly broken my %hash2 = map { $_, [$query->param( $_ )] } @names; # works great my %chipmunk = $query->Vars; # Lincoln should have known better! my $q2 = CGI->new; print $query->header, $query->start_html, $query->pre( $query->h1('First Query'), Dumper( $query )), $query->pre( $query->h1('Second Query'), Dumper( $q2 )), $query->pre( $query->h1('Hash'), Dumper( \%hash )), $query->pre( $query->h1('Hash2'), Dumper( \%hash2 )), $query->pre( $query->h1('Chipmunk'), Dumper( \%chipmunk )), $query->end_html;

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      The first code snippet will also fail if there is a parameter without a value, because param() returns an empty list in that case. The list will end up like ('param1' => 'value1', 'param2' => 'param3', 'value3'). That code really just won't work well, unfortunately.

      In the second snippet, it's simpler to use an anonymous array: my %hash = map {$_, [$query->param($_)]} @names; The new CGI method for getting a hash of the parameters is Vars(): my %hash = $query->Vars(); However, that uses the cgi-lib approach of joining multiple values with "\0". :/

Re: Passing Params
by sutch (Curate) on Jan 17, 2001 at 00:54 UTC
    Try passing $query to your subroutine.
      Would passing $query to the subroutine have the fastest execution of the above methods?
        From where I sit, yes. The overhead of creating a new CGI object is not trivial. It has to read the query string or STDIN (if it can, I don't remember offhand if it caches it) to get parameters, setting things up to be retrieved at will.

        Creating a hash means allocating memory and hashing keys you may or may not use.

        Passing in $query means dereferencing the object to call a method on it.

        Depending on how many parameters you have and how many things you need to call and if you can get by with sticking things in a hash just once and you don't want sticky fields behavior that CGI.pm does so nicely, I'd give the edge to passing in $query. Sometimes I pass a hash.

        This is unlikely to be the largest bottleneck in your program, however.

        I would tend to think that passing $query (which is a reference to an existing CGI query object) is much faster than creating a hash or another new CGI query object.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-26 04:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found