Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

A note regarding CGI::Vars(): I never use it. It was created to ease the transition from the Perl4 cgi-lib.pl and the Perl5 CGI.pm. As a result, it uses the NUL byte (ASCII zero) to delimit multiple values. Since the NUL byte hack is so dangerous, I prefer not not to deliberately introduce them.

One quick and easy way of getting all params is the following:

my %param = map { $_ => [$cgi->param($_)] } $cgi->param;

Some might prefer the following:

#!/usr/bin/perl -wT use strict; use CGI qw/:standard/; my %params = map { $_ => get_data( $_ ) } param; sub get_data { my $name = shift; my @values = param( $name ); return @values > 1 ? \@values : $values[0]; }

With the last snippet, if you only have one param value, the hash value is a scalar value. If you have multiple param values, the hash value is a reference to an anonymous array.

I used to think the last method was better, but as tilly alluded to above, and as I have seen before, most code that uses the ref function has problems. Most of the time, such code can be rethought to more naturally model the problem without having to insert a lot of extra logic to see what type of reference you are getting at any given time. Of course, that comes with the caveat that the only hard and fast rule is that there are no hard and fast rules.

Cheers,
Ovid

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


In reply to (Ovid) Re: Best way to parse CGI params by Ovid
in thread Best way to parse CGI params by hakkr

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found