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

Problems with scripts

by damian1301 (Curate)
on Nov 27, 2000 at 02:30 UTC ( #43409=perlquestion: print w/replies, xml ) Need Help??

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

Well I have two scripts that really arent working.

Script (a sort of Perl site) #1. When I run it through ActivePerl, it runs fine except for an "Use of uninitialized value in string eq at line 20." I've tried to fix this and yes, I know whats its from. Well heres the code. Any pointers, suggestions, or whatever will be appreciated. Just make sure to put them in easy, understandable English.

#!/usr/bin/perl -w use CGI qw(param standard); use strict; my $query = new CGI; my $referrer = $query->param("ref"); my $location = $query->param("place"); if ($referrer){ $referrer =~ tr/A-Z/a-z/; open(REF, ">>referrer.txt") or default('Cant open/write, $!'); print REF "$referrer\n"; close(REF); }elsif ($ENV{'HTTP_REFERER'}) { open(REF1, ">>referrer.txt") or default('Cant open/write, $!'); print REF1 "$ENV{'HTTP_REFERER'}\n"; close(REF1); } if ($location eq ""){ &main }elsif ($location eq lc("misc")){ &misc }elsif ($location eq lc("links")){ &links }else{ default('Where are you trying to go?'); } sub links{ print "links\n"; } sub misc{ print "misc\n"; } sub default{ print "Theres an error: @_"; } sub main{ print "this is sub main!"; }
Its an alphabetical sorter that takes input (using and sorts it alphabetically. Now this will print out the first page but when I submit it, I get a 500 error. I don't really know whats wrong with this one, but once again, any suggestions, pointers, etc. will be appreciated and possibly ++'d. Thanks and heres the code that I have so far...

#!/usr/bin/perl use CGI qw(param); my $query = new CGI; if ($query->param("text1")){ my $one = $query->param("text1"); my $two = $query->param("text2"); my $three = $query->param("text3"); my $four = $query->param("text4"); my $five = $query->param("text5"); my @texts; ($one,$two,$three,$four,$five) = @text; foreach (sort @text){ print "$_ \n\n"; } }else{ print qq~ <HTml><head><title>Alphabetical Sorter</title></head> <body><h2 align="center">Alphabetical Sorter.</h2> <form method="post"> <input type="text" name="text1"><Br> <input type="text" name="text2"><Br> <input type="text" name="text3"><Br> <input type="text" name="text4"><Br> <input type="text" name="text5"><Br> <input type="submit" value="submit form"> </form> ~; }

Well thats all I have wrong right now, Im still new to Perl and Im RTFM but its not really helping with what Im trying to write. Thanks, Bye.

Wanna be perl hacker.
Dave AKA damian

Replies are listed 'Best First'.
(Ovid) Re: Problems with scripts
by Ovid (Cardinal) on Nov 27, 2000 at 02:42 UTC
    There are a variety of problems here. First problem: you get a 500 error on the second script because you're not printing a header. Try this:
    print $query->header;
    If you find the "unitilized value" error annoying in the first script, you can use this:
    my $location = $query->param("place") or "";
    For safety's sake, you should always use the -T switch on your command line when writing CGI scripts. See perlsec for details. Also, you can check out my Web programming course for a bit more information about writing CGI scripts. It's not complete, but it is aimed at people new to CGI scripting and Perl.

    Good luck!


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

Re: Problems with scripts
by arturo (Vicar) on Nov 27, 2000 at 02:47 UTC

    First: if you're using the OO interface, you don't need to import anything (which is what your qw(param standard) is supposed to do) (explanation: the list of names you pass the module when you use it tells it that it is to "export" a number of functions into the calling package's namespace; so you can call (e.g.)"param" function without prepending "CGI::" to it. The OO interface makes all those functions methods of the query object, and so there's no need to *import* them).

    the "uninitialized" problem in line 20 occurs because, evidently, no paramter named "place" is being passed to the script. Have you tried it from the command line (add "place=links" or some such when you run it. CTRL-D will end the input of name-value pairs) The solution to a problem like this is usually something like:

    my $val = $query->param('foo') || 'default value';

    Second pointer: can handle arrays, as I found out some time ago by asking on this very site. (link to follow => Passing arrays to a CGI read Fastolfe's post ). The basic idea here is that you put any number of fields with the same name in your HTML form, and CGI will return them as an array if you request it as such, i.e.

    my @texts = $query->param('text');
    will store all the returned fields named 'text' into the array.

    HTH for a start.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Problems with scripts
by Fastolfe (Vicar) on Nov 27, 2000 at 02:41 UTC
    What is wrong with the first script? Are you just trying to get rid of that warning? Are you getting any errors? What behavior is not to your liking? Note that an empty string is considered a false value, so you could probably write if ($location eq "") ... like if (!$location) ..., which would also eliminate that warning.

    With the second script, have you tried running it by hand? Does it compile OK? If so, examine your web server's error logs. They should contain the error messages describing why your script is failing. Off-hand, I would say this line should be reversed: ($one,$two,$three,$four,$five) = @text I might have re-written that like this, eliminating the need for throw-away variables altogether:

    if ($query->param('text1')) { foreach (sort ( $query->param('text1'), $query->param('text2'), $query->param('text3'), $query->param('text4'), $query->param('text5') )) { print "$_\n\n"; } }
    You may also be interested in the <<"EOR" construct instead of using multi-line qq strings. Either is OK though.
Re: Problems with scripts
by quidity (Pilgrim) on Nov 27, 2000 at 03:11 UTC

    You might want to rewrite this section of code so that it is a little clearer:

    if ($location eq ""){ &main }elsif ($location eq lc("misc")){ &misc }elsif ($location eq lc("links")){ &links }else{ default('Where are you trying to go?'); }

    by using a switch construct (which is really an abuse of a looping construct):

    SWITCH: for ($location) { /^$/ && do {&main() ;last SWITCH}; /^misc$/i && do {&misc() ;last SWITCH}; /^links$/i && do {&links() ;last SWITCH}; default('Where are you trying to go'); # catch anything else }

      I like the "switch" idea quite a bit, myself. Here's something you might do right off the bat: those lc "misc" calls are pointless (the string IS in lower case, that's the part you control). So, at the top, something like  my $location = lc $query->param('place) or 'links'; # or a sensible default

      Which converts the value you *can't* control to lowercase . One nice thing about the "switch" idea is that you can fall through to a default if the value of the parameter isn't valid.

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2021-12-01 07:09 GMT
Find Nodes?
    Voting Booth?

    No recent polls found