Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Is $formdata present?

by ecuguru (Monk)
on Jun 07, 2005 at 08:26 UTC ( #464183=perlquestion: print w/replies, xml ) Need Help??

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

I've got code below (copy/pasted) that works for grabbing data from a post/get request.
Can someone tell me what function I can use to check to see if a value was submitted or not? I have played with exists and defined, but couldn't get either to work right.
A user can post and id and another number. If the number isn't provided, I have a default number that I replace it with later. But I can't figure out how to tell perl that if the value isn't submitted, tell me.

Any ideas? And Thanks in advance!

if ($ENV{'REQUEST_METHOD'} eq 'GET') { @pairs = split(/&/, $ENV{'QUERY_STRING'}); } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') { read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); } else { print "Content-type: text/html\n\n"; print "<P>Use Post or Get"; } foreach $pair (@pairs) { ($key, $value) = split (/=/, $pair); $key =~ tr/+/ /; $key =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg; $value =~s/<!--(.|\n)*-->//g; if ($formdata{$key}) { $formdata{$key} .= ", $value"; } else { $formdata{$key} = $value; } } $id = $formdata{'id'}; $num = $formdata{'num'}; #If there great, if not how do i know? $num = sprintf("%3.3f",$num); #if num is not present, this makes num +0.

Replies are listed 'Best First'.
Re: Is $formdata present?
by blazar (Canon) on Jun 07, 2005 at 08:46 UTC
    Possibly OT wrt your actual question - I must admit I didn't even read it carefully.

    I have only extremely limited experience myself. But they all recommend to avoid reinventing the wheel with the risk of doing it wrong and

    use CGI;
    instead. And I mean experienced hackers that could well reinvent the wheel...
Re: Is $formdata present?
by stonecolddevin (Parson) on Jun 07, 2005 at 09:59 UTC
    Ewwwwwiiie! use CGI, and read Ovid's CGI course on how to use it and why.
    You'll come to find that manually trying to parse form data gets tiresome and leads to broken code 9/10 times1.
    1 Statistics not validated by the FDA or any other official affiliation.
Re: Is $formdata present?
by tchatzi (Acolyte) on Jun 07, 2005 at 08:49 UTC
    I think you should consider reading the documentation of CGI. You can find it here CGI .
    It will be easier for you, if you use CGI to work with forms.

    ``The wise man doesn't give the right answers, he poses the right questions.'' TIMTOWTDI
      Correct. will work for both POST and GET. For existence u couldd use something like..
      use CGI qw(:standard); my %formdata = (); map { $formdata{$_} = param($_); } param; if(exists $formdata{'parametername'}) {do something;}

      20050606 Janitored by Corion: Added formatting, put code in between <code>...</code> tags

        While using is a good thing, you still made it more complicated than it should be, at least if you ask me ...
        use strict; use CGI; my $q = new CGI; my $form = $q->Vars(); if(exists $form->{parameter_name}){ #do whatever }
        Of course, you can also use Data::FormValidator. It's suggested if you need more complicated form validation.
        And multipart-formdata encoding also, which you simply ignored.
        So what's the difference functionally between Defined and Exists? I thought Exists was used for subroutines and hashes? (clearly wrong)..
        Sorry.. I'm new. Will soon register myself and format my code. But my answer should work for you.
        Happy Coding
Re: Is $formdata present?
by jpeg (Chaplain) on Jun 07, 2005 at 09:11 UTC
    would either of these work?

    if (length($formdata{'num'}) == 0) { #... }

    if ($formdata{'num'} eq '' ) { #... }

    obligatory 'use CGI' suggestion goes here

      Yes and no..
      Yes it works and it will execute correctly.
      No because you get runtime warnings about trying to do operations with a nonexistant variable
      If you have your code online and hit enough times, it just jams up the error log something fierce.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://464183]
Approved by Corion
Front-paged by diotalevi
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2022-05-28 00:43 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (98 votes). Check out past polls.