[23340] in Perl-Users-Digest
Perl-Users Digest, Issue: 5560 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Sep 25 09:10:40 2003
Date: Thu, 25 Sep 2003 06:10:14 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 25 Sep 2003 Volume: 10 Number: 5560
Today's topics:
Re: Newbie: form checkboxes & param <REMOVEsdnCAPS@comcast.net>
packages <david-del@del-nonspiritual.com>
Re: packages (Jay Tilton)
Pattern Matching .... (MJS)
Re: Perl 5.6.1 on RedHatLinux 9.0 <grazz@pobox.com>
Re: Perl 5.6.1 on RedHatLinux 9.0 <donny.kwan@comcast.net>
Perl tricks <ak-n@agk.nnov.ru>
Regex backref returns extra data <daniel.rawson.take!this!out!@asml.nl>
Re: Regex backref returns extra data <thepoet_nospam@arcor.de>
Re: Regex backref returns extra data <stefan@unfunked.org>
Re: Regex backref returns extra data <thepoet_nospam@arcor.de>
Re: Regex backref returns extra data <daniel.rawson.take!this!out!@asml.nl>
Re: Student needs help with CGI.pm (I think) <gwschenk"remove this"@socal.rr.com>
Re: Student needs help with CGI.pm (I think) <REMOVEsdnCAPS@comcast.net>
Re: testing news server--ignore please (Anno Siegel)
Re: testing news server--ignore please <spikey_wan@bigfoot.com>
Re: <bwalton@rochester.rr.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 25 Sep 2003 06:20:05 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Newbie: form checkboxes & param
Message-Id: <Xns94014A90599F0sdn.comcast@206.127.4.25>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
Nadine <nadine@mail.sdsu.edu> wrote in news:bksk5j$to3$1@gondor.sdsu.edu:
> I have a script to print out the values of a form's fields to a file.
> That works fine. However I wanted to print out the values of all the
> checkboxes, not just the ones checked, to make analysis easier. I
> shortened my script to deal with just the checkboxes until I got this
> matter settled. I found several ideas on the web about using param
> with checkboxes. I tried two but, being a newbie, I do not know how to
> fix the script. Debug says I have compilation errors near line 24 and
> "0". I tried a couple things but got nowhere.
>
> My checkboxes are called "c1", "c2", "c3", "c4".
>
> #!/usr/bin/perl -w
> use CGI qw(:standard);
> print "Content-type:text/html\n\n";
>
> read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
> @pairs = split(/&/, $buffer);
> foreach $pair (@pairs) {
> ($name, $value) = split(/=/, $pair);
> $value =~ tr/+/ /;
> $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
> $FORM{$name} = $value;
> }
Gah!! This is the third or fourth time in the past two weeks that same
code for parsing CGI parameters! IT IS VERY VERY BAD CODE!! Where are
you people *getting* this crap??
You are already using the excellent CGI module. It does the CGI
parameter parsing for you, and it does not suffer from the many bugs that
the parsing code you're actually using has:
1. It fails if CGI parameters are separated by semicolons.
2. It fails if a CGI value has an unencoded equals sign in it.
3. It fails if a CGI parameter has multiple values (eg checkboxes).
4. It will not work for GET method, only POST method.
5. It does not limit the quantity of data uploaded, leaving you open
to possible DOS attacks.
6. It does not unencode escaped characters in the parameter name.
Seven lines of code, six bugs.
Delete these seven lines, and start writing "param($name)" instead of
"$FORM{$name}". Also, start writing
print header();
instead of
print "Content-type: yadda yadda yadda\n\n";
One, it's less typing; two, you're less likely to make a typo that will
give you confusing 500 errors.
Now, on to your actual question:
> $box=param($FORM{$key})||'0'; # print value or zero
I don't see any syntax error on this line. What specific error message
did you get? Is this your actual code, or did you make a transcription
error when re-typing it? (if so, please copy/paste code instead of re-
typing it).
There is a bad logic error in the above line. You have manually parsed
out the CGI parameters into the %FORM variable. So $FORM{$key} is the
value of the $key CGI variable. However, you mysteriously then pass that
to the CGI.pm param() function, which does the same thing. So if your
checkbox is named "c1", and its value is "yes", then you're calling
param("yes"), which attempts to look up the value of the "yes" variable.
Either use %FORM or use param(), not both. Since I highly recommend that
you ditch your %FORM variable, read that as "use param($key) instead"!
- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
iQA/AwUBP3LPUGPeouIeTNHoEQKx2ACeMdMroTBTyTeB23pC+G2dVbClUJkAoOkD
fKQCDcDT3D8vD+eeBrELUqiH
=JBwi
-----END PGP SIGNATURE-----
------------------------------
Date: Thu, 25 Sep 2003 00:04:42 -0500
From: David McDivitt <david-del@del-nonspiritual.com>
Subject: packages
Message-Id: <48t4nvs30u9u8417o1dd65tteltalra4bj@4ax.com>
I am having difficulty understanding packages. I made a package, and
figured out how to make variables visible to modules using it without
qualifying with the package name, but exposing package subroutines the
same way is a bit confusing. I read several things. I do not understand
export syntax and how to set that up. Would someone please post an
example package having one routine called "init", and make the init
routine visible to any module using it without having to say
PACKAGENAME::init() in that module. Thanks
--
http://nonspiritual.com
http://freeconservative.org
------------------------------
Date: Thu, 25 Sep 2003 06:20:44 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: packages
Message-Id: <3f72861e.133797184@news.erols.com>
David McDivitt <david-del@del-nonspiritual.com> wrote:
: I am having difficulty understanding packages. I made a package, and
: figured out how to make variables visible to modules using it without
: qualifying with the package name, but exposing package subroutines the
: same way is a bit confusing.
It shouldn't be. For purposes of poking around in the symbol table, the
only difference between a variable and a subroutine is the funny sigil
in front of the name.
If you show how you're exporting variables from one namespace to
another, you can see how the same technique can export a subroutine.
------------------------------
Date: 25 Sep 2003 00:56:55 -0700
From: tabletdesktop@yahoo.com (MJS)
Subject: Pattern Matching ....
Message-Id: <f0171209.0309242356.6b6b785e@posting.google.com>
How can the following pattern be matched and replaced by something
similar to it. After the pattern has been replaced, the replacing
pattern is repeating itself for certain number of times.
"The puzzle is :
go to your left=> continue
go to your right <= [ 20: 909]
continue[7:0] "
move.
This is how exactly how the text is written(including the spaces).
I can't figure out how to do it. Please help.
I tried something like this and didn't work:
===========================================================
# tie @array to filename using Tie::File
tie my @array, 'Tie::File', 'result.txt' or die "Cannot open
result.txt:$!";
while(<reading the text file>){
for($firstindex; $firstindex >= 0; $firstindex--){
$n++;
for ( 0 .. $#array ) {
my $string ="the above mentioned pattern"
if ( $array[ $_ ] =~ /string/ ) {
my $new_string= some similar pattern.
splice @array, $_ +1, 0, /the_string/;
last;
}
}
}
untie @array;
=============================================
------------------------------
Date: Thu, 25 Sep 2003 04:26:08 GMT
From: Steve Grazzini <grazz@pobox.com>
Subject: Re: Perl 5.6.1 on RedHatLinux 9.0
Message-Id: <A7ucb.4167$541.777@nwrdny02.gnilink.net>
Donny Kwan, P.Eng. <donny.kwan@comcast.net> wrote:
> thanks Steve,
>
> after deleting <built-in> and <command> from makefile
> make gives this msg.
>
> make[1]: Entering directory`/home/name/perl-5.6.1/utils`
You have to do the same thing again in the subdirectories,
since they have makefiles as well, and those makefiles also
contain the "<built-in>" and "<command line>" entries.
(Think.)
--
Steve
------------------------------
Date: Thu, 25 Sep 2003 05:54:05 GMT
From: "Donny Kwan, P.Eng." <donny.kwan@comcast.net>
Subject: Re: Perl 5.6.1 on RedHatLinux 9.0
Message-Id: <1qvcb.426824$Oz4.225261@rwcrnsc54>
yeah, it works...thanks steve
recap:
just remove <built-in>, <command> from all the makefile in both current and
subdirectory.
"Steve Grazzini" <grazz@pobox.com> wrote in message
news:A7ucb.4167$541.777@nwrdny02.gnilink.net...
> Donny Kwan, P.Eng. <donny.kwan@comcast.net> wrote:
> > thanks Steve,
> >
> > after deleting <built-in> and <command> from makefile
> > make gives this msg.
> >
> > make[1]: Entering directory`/home/name/perl-5.6.1/utils`
>
> You have to do the same thing again in the subdirectories,
> since they have makefiles as well, and those makefiles also
> contain the "<built-in>" and "<command line>" entries.
>
> (Think.)
>
> --
> Steve
------------------------------
Date: Thu, 25 Sep 2003 12:27:11 +0400
From: Andrei Koulik <ak-n@agk.nnov.ru>
Subject: Perl tricks
Message-Id: <bku8sv$63cvq$1@ID-187854.news.uni-berlin.de>
Can anybody explain me how this command deletes files:
perl -e '$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|`{;;y; -/:-@[-`{-};`-{/"
-;;s;;$_;see'
------------------------------
Date: Thu, 25 Sep 2003 07:08:20 -0400
From: Dan Rawson <daniel.rawson.take!this!out!@asml.nl>
Subject: Regex backref returns extra data
Message-Id: <bkuibb$62t7t$1@ID-122008.news.uni-berlin.de>
I'm attempting to extract a single word from a line which looks like:
TARGET = file1 file2 file3 .....
I need 'file1' from this line. Using:
s/.*?\=\s*([a-zA-Z_\.0-9]+).*/$1/ (Note the "+" following the char. class)
works fine as long as file1 is present. However, if I have a line which ends after the "=" sign, it returns the entire
line, instead of an empty string.
If I change the pattern to:
s/.*?\=\s*([a-zA-Z_\.0-9]*).*/$1/ (Note the "*" following the char. class)
it works; I get 'file1' back if it's there, and an empty string if not.
Questions:
1. My understanding was that the "+" would cause it to match one or more of the char. class (which would make it a
legal file name in this case). Why does it return the entire line if there's no match??
2. Why does the second pattern work differently than the first one if there's nothing after the "=" sign?
TIA . . . .
Dan
------------------------------
Date: Thu, 25 Sep 2003 13:35:08 +0200
From: "Christian Winter" <thepoet_nospam@arcor.de>
Subject: Re: Regex backref returns extra data
Message-Id: <3f72d2ec$0$23087$9b4e6d93@newsread2.arcor-online.net>
"Dan Rawson" <daniel.rawson.take!this!out!@asml.nl> wrote:
> I'm attempting to extract a single word from a line which looks like:
>
> TARGET = file1 file2 file3 .....
>
> I need 'file1' from this line. Using:
>
> s/.*?\=\s*([a-zA-Z_\.0-9]+).*/$1/
> (Note the "+" following the char. class)
>
> works fine as long as file1 is present. However, if I have a line
> which ends after the "=" sign, it returns the entire
> line, instead of an empty string.
No, it doesn't "return" the entire line.
It doesn't match at all, thus the content
of your variable isn't changed.
You might want to try catching the returned match
of the m//-Operator into a separate variable.
This little script should illustrate that:
------------------------------------------------
#!perl -w
use strict;
my $a = "TARGET = file1 file2 file3 file4";
(my $hit) = $a =~ /.*?\=\s*([a-zA-Z_\.0-9]+).*/;
print $hit?"HIT: $hit\n":"No HIT\n";
my $b = "TARGET =";
($hit) = $b =~ /.*?\=\s*([a-zA-Z_\.0-9]+).*/;
print $hit?"HIT: $hit\n":"No HIT\n";
-------------------------------------------------
HTH
-Christian
------------------------------
Date: Thu, 25 Sep 2003 13:00:42 +0100
From: Stefan <stefan@unfunked.org>
Subject: Re: Regex backref returns extra data
Message-Id: <bkuldb$2h7$1@news6.svr.pol.co.uk>
Hi Dan
I think you are using a substitution (s///) where you actually want a
match (m//). If your substitution regex doesn't match, nothing will be
replaced and you will get the whole line back. If you want to extract
the first file name, you would be better off writing...
'TARGET = file1 file2 file3' => m/.*?=\s*(\w.+)/;
...after which $1 will contain 'file1'.
Hope this helps.
Stefan
Dan Rawson wrote:
> I'm attempting to extract a single word from a line which looks like:
>
> TARGET = file1 file2 file3 .....
>
> I need 'file1' from this line. Using:
>
> s/.*?\=\s*([a-zA-Z_\.0-9]+).*/$1/ (Note the "+" following the char. class)
>
> works fine as long as file1 is present. However, if I have a line which ends after the "=" sign, it returns the entire
> line, instead of an empty string.
>
> If I change the pattern to:
>
> s/.*?\=\s*([a-zA-Z_\.0-9]*).*/$1/ (Note the "*" following the char. class)
>
> it works; I get 'file1' back if it's there, and an empty string if not.
>
> Questions:
> 1. My understanding was that the "+" would cause it to match one or more of the char. class (which would make it a
> legal file name in this case). Why does it return the entire line if there's no match??
> 2. Why does the second pattern work differently than the first one if there's nothing after the "=" sign?
>
> TIA . . . .
>
> Dan
>
------------------------------
Date: Thu, 25 Sep 2003 14:11:14 +0200
From: "Christian Winter" <thepoet_nospam@arcor.de>
Subject: Re: Regex backref returns extra data
Message-Id: <3f72db63$0$23095$9b4e6d93@newsread2.arcor-online.net>
"Stefan" <stefan@unfunked.org> wrote:
> Hi Dan
>
> I think you are using a substitution (s///) where you actually want a
> match (m//). If your substitution regex doesn't match, nothing will be
> replaced and you will get the whole line back. If you want to extract
> the first file name, you would be better off writing...
>
> 'TARGET = file1 file2 file3' => m/.*?=\s*(\w.+)/;
>
> ...after which $1 will contain 'file1'.
But be careful with $1, as it will not
be reset after an unsuccessful match.
e.g.:
my $xx = "a b c";
$xx =~ /^(a)/; # matches
print "\$1 = $1\n";
$xx =~ /^(b)/; # does not match
print "\$1 = $1\n";
which prints out:
$1 = a
$1 = a
and may not be what one expects.
So one should either use capturing (see my reply
to the OP) or add a check like
if( /.*?=\s*(\w.+)/ ) { do something with $1... }
-Christian
------------------------------
Date: Thu, 25 Sep 2003 08:24:39 -0400
From: Dan Rawson <daniel.rawson.take!this!out!@asml.nl>
Subject: Re: Regex backref returns extra data
Message-Id: <bkumq7$67gqd$1@ID-122008.news.uni-berlin.de>
Christian Winter wrote:
> "Stefan" <stefan@unfunked.org> wrote:
>
>>Hi Dan
>>
>>I think you are using a substitution (s///) where you actually want a
>>match (m//). If your substitution regex doesn't match, nothing will be
>>replaced and you will get the whole line back. If you want to extract
>>the first file name, you would be better off writing...
>>
>>'TARGET = file1 file2 file3' => m/.*?=\s*(\w.+)/;
>>
>>...after which $1 will contain 'file1'.
>
>
> But be careful with $1, as it will not
> be reset after an unsuccessful match.
> e.g.:
>
> my $xx = "a b c";
> $xx =~ /^(a)/; # matches
> print "\$1 = $1\n";
> $xx =~ /^(b)/; # does not match
> print "\$1 = $1\n";
>
> which prints out:
> $1 = a
> $1 = a
> and may not be what one expects.
>
> So one should either use capturing (see my reply
> to the OP) or add a check like
> if( /.*?=\s*(\w.+)/ ) { do something with $1... }
>
> -Christian
>
Thanks to everyone for helping me shake out the cobwebs; I guess I should have had another cup of coffee before posting
this morning!!
I modified it so it checks for the match, then does the substitution if the match succeeds (duh!).
Thanks again . . . .
Dan
------------------------------
Date: Thu, 25 Sep 2003 04:45:39 GMT
From: Gary Schenk <gwschenk"remove this"@socal.rr.com>
Subject: Re: Student needs help with CGI.pm (I think)
Message-Id: <Tpucb.1368$5z.179@twister.socal.rr.com>
Sam Holden wrote:
> On Thu, 25 Sep 2003 03:36:18 GMT, Gary Schenk <gwschenk@socal.rr.com>
> wrote:
>> Sam Holden wrote:
> [snip CGI script calling dump()]
>>>
>>> So why are you calling it? Case matters.
>>
>> Long story, but I'm having to teach myself Perl and CGI. I'm taking an
>> extended education class starting next week. In the meantime I've been
>> reading a book on CGI and Perl. This is an example script from the book.
>> It is supposed to output a webpage with information taken from a form.
>> This script is to show how easy CGI.pm makes this task. Basically, I'm
>> quite ingnorant, and want to know if it is the script or a configuration
>> problem.
>
> Are you *sure* the book doesn't say:
>
> print Dump();
It definitely says print dump();
>
> Since that would do what you describe.
>
> As I said, case matters.
Of course, you are absolutely correct! That was it. It runs as it should
now.
>
> If it really says dump(), then I'd be looking for an errata and making
> the corrections it specifies. If there isn't one I'd be looking for a
> new book.
>
I checked for an errata and nothing there. I'll send an email to the
authors.
>>>
>>> Also, run the script at the command line to get it working before
>>> running it via CGI, any errors will be much easier to see.
>>>
>>
>> Yes, I ran perl -c and got the "too late for -T option" message. Maybe i
>> am being impatient, but it's bugging me.
>
> So use perl -c -T.
>
> See 'perldoc perldiag' if you don't understand a perl diagnostic.
Thanks for the tip.
>
> Though what I meant was:
>
> ./test.cgi
>
> Or whatever the CGI was named, to run it on the command line.
I see.
Thanks for the help. Lots of information was packed into this exchange!
--
Gary Schenk
------------------------------
Date: Thu, 25 Sep 2003 06:24:11 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Student needs help with CGI.pm (I think)
Message-Id: <Xns94014B41AB1E4sdn.comcast@206.127.4.25>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
Gary Schenk <gwschenk"remove this"@socal.rr.com> wrote in
news:Tpucb.1368$5z.179@twister.socal.rr.com:
> Sam Holden wrote:
>> Are you *sure* the book doesn't say:
>>
>> print Dump();
>
> It definitely says print dump();
Which book are you using? Lord knows there are enough bad Perl books out
there. :-/
- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
iQA/AwUBP3LQRmPeouIeTNHoEQJXKQCeMn39bUHfpq+1/FrZmoDuL2l00iEAn1NQ
sLMa8MBisQi2yYzekhuHVHL4
=nFYb
-----END PGP SIGNATURE-----
------------------------------
Date: 25 Sep 2003 09:51:52 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: testing news server--ignore please
Message-Id: <bkudro$7do$1@mamenchi.zrz.TU-Berlin.DE>
test <jack@datastar.com> wrote in comp.lang.perl.misc:
> test
Don't use active newsgroups for tests. That's what test groups are for.
Anno
------------------------------
Date: Thu, 25 Sep 2003 11:27:39 +0100
From: "Richard S Beckett" <spikey_wan@bigfoot.com>
Subject: Re: testing news server--ignore please
Message-Id: <bkug1e$n0v$1@newshost.mot.com>
> > test
>
> Don't use active newsgroups for tests. That's what test groups are for.
Yeah!
------------------------------
Date: Sat, 19 Jul 2003 01:59:56 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re:
Message-Id: <3F18A600.3040306@rochester.rr.com>
Ron wrote:
> Tried this code get a server 500 error.
>
> Anyone know what's wrong with it?
>
> if $DayName eq "Select a Day" or $RouteName eq "Select A Route") {
(---^
> dienice("Please use the back button on your browser to fill out the Day
> & Route fields.");
> }
...
> Ron
...
--
Bob Walton
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 5560
***************************************