Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Splitting form input

by C-Keen (Monk)
on Sep 24, 2001 at 15:09 UTC ( [id://114282]=perlquestion: print w/replies, xml ) Need Help??

C-Keen has asked for the wisdom of the Perl Monks concerning the following question:

I am writing on a little script that checks on user input from an html-form. The parameters are send to the script through STDIN. The perl code I am currently using:
@items = split(/&/,$teststring); foreach $line (@items){ ($dummy1,$dummy2) = split(/=/,$line); $hash{$dummy1} = $dummy2; }

This works fine as long as the user does not enter an ampersand,which works as a delimiter in this case, in one of the input fields. Since in the html form a commentary field is included this might likely happen. So if the user writes something like this: "blabla et & cetera" I will end up with wrong hash keys an parts of the input are lost. How can I split the string correctly?
Thank You!
Your's

C-Keen

Replies are listed 'Best First'.
Re (tilly) 1: Splitting form input
by tilly (Archbishop) on Sep 24, 2001 at 16:45 UTC
    Multiple people have already given you your answer, but nobody has actually told you how to use CGI. So here is a brief example of how you accept CGI input: use strict; Not really needed, but a good idea. See strict.pm for details. use CGI qw/:standard/; Loads CGI and imports most of the functions you are likely to want. The only one I am going to show you is param(). my @parameter_list = param(); With no arguments it hands you back a list of all parameter names you were called with. my $text = param('textbox_name'); As for a single parameter in scalar context, and it gives you back that parameter. If there were multiple entries, it gives you just one. my @selection = param('select_list_name'); Ask for a single parameter in list context, and it gives you back all of the entries with that name. If you are not familiar with the difference between list and scalar context, then I would suggest calling the following function in various places to see when you have list and scalar context:
    sub show_context { if (wantarray()) { print "List context\n"; } elsif (defined(wantarray())) { print "Scalar context\n"; } else { print "Boolean (scalar) context\n"; } }
    (It is explained in perlsyn, but a little trial and error generally helps to figure it out.)
Re: Splitting form input
by Caillte (Friar) on Sep 24, 2001 at 15:28 UTC

    For my first 6 months as a perl programmer I often wrote line splitters like yours. Then I realised that they are the kiss of death as it is very hard to wirte a block of code that takes all possible input conditions into account. Then, while trying to do such a thing, I discovered CGI.pm

    After crying and banging my head against the wall for several hours at the thought of allthe hours I had spent writing code that imitated - badly - some of the things that CGI did I then vowed to always search CPAN before starting a project!

    People always say in this situation 'use this' or 'use that' but it is worth reiterating why. With the CGI module, for example, several year's worth of development, by a large number of people has gone into making sure that that module is the best way of reading CGI data possible. Because of that you will find it impossible to duplicate that work or even write something that is nearly as good.

    So always do a module search, it will save pain! :)

    $japh->{'Caillte'} = $me;

Re: Splitting form input
by tommyw (Hermit) on Sep 24, 2001 at 15:13 UTC
    Repeat after me:
    I will use CGI;
    I will use CGI;
    I will use CGI;
    It's there to make your life easier...
Re: Splitting form input
by synapse0 (Pilgrim) on Sep 24, 2001 at 15:13 UTC
    Use CGI.pm .. it comes with Perl, there's no reason not to use it if you're working with web based scripts.
    It will solve your problems quite effectively.
    -Syn0
Re: Splitting form input
by George_Sherston (Vicar) on Sep 24, 2001 at 18:31 UTC
    In case some of these responses seem harsh, I thought I should mention that I received a use CGI or die; mark of doom for one of my early posts. I'd never bothered to figure out how to in the past, but I was convinced by the weight of informed opinion, made the effort, and behold, the system works! It's well worth learning. And if people sound angry about this, I'm confident it's not anger at you: it's anger the Demon of Wasted Effort, who has found another victim. Don't let him get away with it any longer!

    May I also say, since you mention you are not just receiving but also checking form input, use CGI::Validate.

    § George Sherston

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://114282]
Approved by root
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