Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: More efficient reading of forms in CGI with CGI::Lite

by troy (Initiate)
on Sep 30, 2003 at 17:31 UTC ( [id://295362]=note: print w/replies, xml ) Need Help??


in reply to More efficient reading of forms in CGI

This is one of the reasons why I like CGI::Lite. Besides CGI::Lite being very speedy, it puts all of the variables into a hashref (or hash if you like) so that you can use it in any context that you would use your variables in. Start with this:
use CGI::Lite; my $cgi = new CGI::Lite;
Then, you can do either one of the following, depending on your preference:
$form = $cgi->parse_form_data; %form = $cgi->parse_form_data; $form = $cgi->parse_form_data ('GET', 'HEAD' or 'POST');
Quick, easy, and all in a neat little hash, ready for you to use in statements like thise:
print "blabla bla bla $form->{entry} blabla<br>\n";
Yes, I know that you could write something to put your CGI.pm params into a hash, but then I couldn't write a plug for CGI::Lite, could I? If you're not using some of the big features of CGI.pm, like html generation, you're better off with CGI::Lite. Either way, like others have said here, get your incoming variables out of the main namespace.

Replies are listed 'Best First'.
Re: Re: More efficient reading of forms in CGI with CGI::Lite
by bradcathey (Prior) on Sep 30, 2003 at 18:10 UTC
    In 25 words or less, what exactly is "namespace?" From what I can tell, it is the area inside my script where I assign the variable names and what is in them. Anyone care to expound?
      In 25 words or less, what exactly is "namespace?"
      Check out the perlfunc entry for package.

      Hmm... that's 7 words. I'll just give you a final example:

      #!/usr/bin,/perl -w $x = "MAIN"; # in default package, "main" package Foo; $x = 123; # in package "Foo", so this is $Foo::x package Bar; $x = 456; # in package "Bar", so this is $Bar::x package main; # back to normal print "\$$x = '$x'\n"; print "\$main::$x = '$main::x'\n"; print "\$Foo::$x = '$Foo::x'\n"; print "\$Bar::$x = '$Bar::x'\n";
      This prints:
      $MAIN = 'MAIN'
      $main::MAIN = 'MAIN'
      $Foo::MAIN = '123'
      $Bar::MAIN = '456'
      
      In summary: it's a way to have global variables of the same name which nevertheless don't trample on each other. That way you don't have to worry if you happen to use the same name for a variable as used in some obscure module.

      Commonly modules have their own package, and the standard convention is that it's the same as the module name. use relies entirely on that convention for it's import mechanism — which is a way to make variables and subs visible unqualified across package boundaries, by making aliases to the same variables in the different packages. Thus, if you import $Foo::x into Bar, then $Bar::x will point to the same variable as $Foo::x does. Any modification of one will be reflected in a change in the other.

      You're probably familiar with that effect, but you likely didn't realise how simple the underlying implementation really is. Because it is.

Log In?
Username:
Password:

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

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

    No recent polls found