[30900] in Perl-Users-Digest
Perl-Users Digest, Issue: 2145 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jan 21 11:09:52 2009
Date: Wed, 21 Jan 2009 08:09:18 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 21 Jan 2009 Volume: 11 Number: 2145
Today's topics:
CGI.pm and Use of uninitialized value in pattern match <Alexander.Farber@gmail.com>
Re: CGI.pm and Use of uninitialized value in pattern ma <peter@makholm.net>
Re: CGI.pm and Use of uninitialized value in pattern ma <Alexander.Farber@gmail.com>
Re: CGI.pm and Use of uninitialized value in pattern ma <tadmc@seesig.invalid>
Re: CGI.pm and Use of uninitialized value in pattern ma <tadmc@seesig.invalid>
Re: CGI.pm and Use of uninitialized value in pattern ma <Alexander.Farber@gmail.com>
Re: CGI.pm and Use of uninitialized value in pattern ma (Randal L. Schwartz)
Re: Concatenating regular exprs in a 'grep' call <tadmc@seesig.invalid>
on Code references sharma__r@hotmail.com
Re: on Code references <ramesh.thangamani@gmail.com>
Re: on Code references <ramesh.thangamani@gmail.com>
Re: on Code references sharma__r@hotmail.com
Re: on Code references <1usa@llenroc.ude.invalid>
Re: on Code references sharma__r@hotmail.com
Re: on Code references <tadmc@seesig.invalid>
returning calculation results in perl scripts spanish26@gmail.com
Re: returning calculation results in perl scripts <1usa@llenroc.ude.invalid>
Re: What do you need to have to be considered a Master (Vicky Conlan)
Re: What do you need to have to be considered a Master <hjp-usenet2@hjp.at>
Re: What do you need to have to be considered a Master <cartercc@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 21 Jan 2009 06:27:39 -0800 (PST)
From: "A. Farber" <Alexander.Farber@gmail.com>
Subject: CGI.pm and Use of uninitialized value in pattern match
Message-Id: <f03dcfdb-b9b8-44dd-8d4a-f44d0c0ebb89@e1g2000pra.googlegroups.com>
Hello Perl users,
I have a habit of untainting input data this way in my
CGI scripts (example: http://pastebin.com/m46057a70):
$user = $1 if $query->param('user') =~ /(\w{3,12})/;
$pass = $1 if $query->param('pass') =~ /(\w{8})/;
.....
unless ($user and $pass and ...) {
print $query->start_form(), ...
} else {
# do the real work with untainted data
}
and wonder, how to get rid of the warnings
"Use of uninitialized value in pattern match (m//)"
without (ab)using the no warnings qw(uninitialized)?
Thank you
Alex
------------------------------
Date: Wed, 21 Jan 2009 15:48:37 +0100
From: Peter Makholm <peter@makholm.net>
Subject: Re: CGI.pm and Use of uninitialized value in pattern match
Message-Id: <87k58oiuka.fsf@vps1.hacking.dk>
"A. Farber" <Alexander.Farber@gmail.com> writes:
> I have a habit of untainting input data this way in my
> CGI scripts (example: http://pastebin.com/m46057a70):
>
> $user = $1 if $query->param('user') =~ /(\w{3,12})/;
> $pass = $1 if $query->param('pass') =~ /(\w{8})/;
I wouldn't call
{
no warnings qw(uninitialized);
$user = $1 if $query->param('user') =~ /(\w{3,12})/;
$pass = $1 if $query->param('pass') =~ /(\w{8})/;
}
abuse of the functionality. It clearly shows that you are aware of the
posibility of the parameters to be uninitialized and explicitly
decided that it doesn't matter.
If you wont accept that solution you have to test for definedness in
some way:
$user = $1 if defined( $query->param('user') )
&& $query->param('user') =~ /(\w{3,12})/;
$user = $1 if ($query->param('user') // '') =~ /(\w{3,12})/;
$user = $1 if
(defined $query->param('user') ? $query->param('user') : '') =~ /(\w{3,12})/;
I wouldn't rule out the 'no warnings' solution. But most of the time I
would use the first of the above three without really considering any
alternatives.
//Makholm
------------------------------
Date: Wed, 21 Jan 2009 07:09:07 -0800 (PST)
From: "A. Farber" <Alexander.Farber@gmail.com>
Subject: Re: CGI.pm and Use of uninitialized value in pattern match
Message-Id: <0d247353-7edf-41d3-aa77-973714a40250@r37g2000prr.googlegroups.com>
On Jan 21, 3:48=A0pm, Peter Makholm <pe...@makholm.net> wrote:
> {
> =A0 =A0 no warnings qw(uninitialized);
>
> =A0 =A0 $user =3D $1 if $query->param('user') =3D~ /(\w{3,12})/;
> =A0 =A0 $pass =3D $1 if $query->param('pass') =3D~ /(\w{8})/;
>
> }
This looks better to me, thanks
------------------------------
Date: Wed, 21 Jan 2009 08:58:09 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: CGI.pm and Use of uninitialized value in pattern match
Message-Id: <slrngnee01.b11.tadmc@tadmc30.sbcglobal.net>
A. Farber <Alexander.Farber@gmail.com> wrote:
> Hello Perl users,
>
> I have a habit of untainting input data this way in my
> CGI scripts (example: http://pastebin.com/m46057a70):
>
> $user = $1 if $query->param('user') =~ /(\w{3,12})/;
If a 20-character username is entered, you want to silently
truncate it to 12 chars?
Validation should match against the *entire input*.
Put some anchors in there.
> and wonder, how to get rid of the warnings
> "Use of uninitialized value in pattern match (m//)"
> without (ab)using the no warnings qw(uninitialized)?
You currently have a value of undef to indicate invalid data.
Use some other false value, such as the empty string.
Using m// in list context, and adding anchors:
my($user) = $query->param('user') =~ /^(\w{3,12})$/ || '';
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Wed, 21 Jan 2009 09:05:01 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: CGI.pm and Use of uninitialized value in pattern match
Message-Id: <slrngneect.bgo.tadmc@tadmc30.sbcglobal.net>
A. Farber <Alexander.Farber@gmail.com> wrote:
> $pass = $1 if $query->param('pass') =~ /(\w{8})/;
You don't even _allow_ punctuation characters in passwords?
Good security generally *requires* punctuation characters in passwords.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Wed, 21 Jan 2009 07:33:13 -0800 (PST)
From: "A. Farber" <Alexander.Farber@gmail.com>
Subject: Re: CGI.pm and Use of uninitialized value in pattern match
Message-Id: <2ce78258-3d30-402a-b347-48321d5f8f75@d36g2000prf.googlegroups.com>
Hello Tad,
On Jan 21, 4:05=A0pm, Tad J McClellan <ta...@seesig.invalid> wrote:
> A. Farber <Alexander.Far...@gmail.com> wrote:
> > $pass =3D $1 if $query->param('pass') =3D~ /(\w{8})/;
>
> You don't even _allow_ punctuation characters in passwords?
>
> Good security generally *requires* punctuation characters in passwords.
your previous comment about anchors etc. has been good, thanks.
No, I don't want punctuation :-) Here in Germany
the keyboard layout is different from the english.
And the latter layout pop-ups from time to time on
different occasions, but I don't want the users
to call me in those cases (work as sysadmin here).
Here is my current password-generating function
(I also removed chars like 8 and B, because some
users really manage to mix them up...):
use constant POSSIBLE =3D>
'abcdefghijkmnpqrstuvwxACDEFGHJKMNPQRSTUVWX234579';
# generate a random password (also used for 2-chars salt)
sub genpass($) {
my ($len, $pass);
$len =3D shift;
$pass =3D '';
$pass .=3D substr(POSSIBLE, (int(rand(length(POSSIBLE)))), 1)
while length($pass) < $len;
# make sure the password contains 2 digit
# and 2 letters for the Windows policy
return genpass($len) if $len >=3D8 &&
($pass =3D~ tr/0-9// < 2 ||
$pass =3D~ tr/a-z// < 2 ||
$pass =3D~ tr/A-Z// < 2);
return $pass;
}
It generates strings like:
C3Uns7te
gRc4mVA4
5sF4DC5m
sx5N4eSm
WV7e3bTJ
3UbfM7g5
ni4TrqS3
5NArf3Pb
I wish there were some clever module
for better-to-memorize passwords though.
Maybe by alternate vowels and consonants?
Regards
Alex
------------------------------
Date: Wed, 21 Jan 2009 07:50:04 -0800
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: CGI.pm and Use of uninitialized value in pattern match
Message-Id: <86wscoodzn.fsf@blue.stonehenge.com>
>>>>> "A" == A Farber <Alexander.Farber@gmail.com> writes:
A> return genpass($len) if $len >=8 &&
A> ($pass =~ tr/0-9// < 2 ||
A> $pass =~ tr/a-z// < 2 ||
A> $pass =~ tr/A-Z// < 2);
Apparently, you believe that Perl has tail-recursion elimination. It doesn't.
You should probably have written this as a loop instead. Something like:
{
$pass = [your calculation here];
return $pass if $len < 8; # short passwords skip checks
# if any quality check fails, try again:
redo if $pass =~ tr/0-9// < 2;
redo if $pass =~ tr/a-z// < 2;
redo if $pass =~ tr/A-Z// < 2;
return $pass; # passed all checks, return it
}
print "Just another Perl hacker,"; # the original
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
------------------------------
Date: Wed, 21 Jan 2009 08:32:39 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Concatenating regular exprs in a 'grep' call
Message-Id: <slrngnecg7.aln.tadmc@tadmc30.sbcglobal.net>
Larry Gates <larry@example.invalid> wrote:
> I think this program blurs this line:
>
> my $filename = 'faulk15.pl';
> open(my $fh, '<', $filename) or die "cannot open $filename: $!";
> while (<$fh>) {
> print $_;
> }
> close($fh)
>
> # perl faulk15.pl
>
> C:\MinGW\source>perl faulk15.pl
> my $filename = 'faulk15.pl';
> open(my $fh, '<', $filename) or die "cannot open $filename: $!";
> while (<$fh>) {
> print $_;
> }
> close($fh)
>
> # perl faulk15.pl
http://en.wikipedia.org/wiki/Quine_(computing)
http://www.nyx.net/~gthompso/quine.htm
Some quines in Perl:
http://www.nyx.net/~gthompso/self_perl.txt
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Wed, 21 Jan 2009 00:13:17 -0800 (PST)
From: sharma__r@hotmail.com
Subject: on Code references
Message-Id: <0a2d735d-066f-4f45-98b8-66fc1b95720f@n33g2000pri.googlegroups.com>
Hi Perl gurus,
I am having trouble with calling subroutines using a reference
variable.
#!/usr/local/bin/perl
use 5.8.0;
use strict;
use warnings;
# anonymous subroutine stored in a reference variable
my $reduce_ref = sub (&@) {
my $code = shift;
no strict 'refs';
return shift unless @_ > 1;
use vars qw($a $b);
my $caller = caller;
local(*{$caller."::a"}) = \my $a;
local(*{$caller."::b"}) = \my $b;
$a = shift;
foreach (@_) {
$b = $_;
$a = &{$code}();
}
$a;
};
my @A = qw( 3 54 22);
## to compute the max of array
print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
__END__
## this gives the following error:
# Use of uninitialized value in numeric gt (>) at ...
# Odd number of elements in anonymous hash at ...
# Use of uninitialized value in anonymous hash ({}) at ...
# Not a CODE reference at ...
Had I used a named subroutine then the things work fine.
What could be the problem here?
with thanks,
------------------------------
Date: Wed, 21 Jan 2009 01:53:56 -0800 (PST)
From: rthangam <ramesh.thangamani@gmail.com>
Subject: Re: on Code references
Message-Id: <00f23a3a-873d-469b-a81e-cfab5bf4f5c0@a12g2000pro.googlegroups.com>
On Jan 21, 1:13=A0pm, sharma...@hotmail.com wrote:
> Hi Perl gurus,
>
> I am having trouble with calling subroutines using a reference
> variable.
>
> #!/usr/local/bin/perl
> use 5.8.0;
> use strict;
> use warnings;
>
> # anonymous subroutine stored in a reference variable
> my $reduce_ref =3D sub (&@) {
> =A0 =A0 =A0 =A0 my $code =3D shift;
> =A0 =A0 =A0 =A0 no strict 'refs';
>
> =A0 =A0 =A0 =A0 return shift unless @_ > 1;
>
> =A0 =A0 =A0 =A0 use vars qw($a $b);
>
> =A0 =A0 =A0 =A0 my $caller =3D caller;
> =A0 =A0 =A0 =A0 local(*{$caller."::a"}) =3D \my $a;
> =A0 =A0 =A0 =A0 local(*{$caller."::b"}) =3D \my $b;
>
> =A0 =A0 =A0 =A0 $a =3D shift;
> =A0 =A0 =A0 =A0 foreach (@_) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $b =3D $_;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $a =3D &{$code}();
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 $a;
>
> };
>
> my @A =3D qw( 3 54 22);
>
> ## to compute the max of array
> print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
> __END__
>
> ## this gives the following error:
> # Use of uninitialized value in numeric gt (>) at ...
> # Odd number of elements in anonymous hash at ...
> # Use of uninitialized value in anonymous hash ({}) at ...
> # Not a CODE reference at ...
>
> Had I used a named subroutine then the things work fine.
> What could be the problem here?
>
> with thanks,
you can use eval !! that should solve the problem:
#!/usr/local/bin/perl
use 5.8.0;
use strict;
use warnings;
use Data::Dumper;
# anonymous subroutine stored in a reference variable
my $reduce_ref =3D sub (&@) {
my $code =3D shift;
no strict 'refs';
return shift unless @_ > 1;
use vars qw($a $b);
my $caller =3D caller;
local(*{$caller."::a"}) =3D \my $a;
local(*{$caller."::b"}) =3D \my $b;
$a =3D shift;
foreach (@_) {
$b =3D $_;
$a =3D eval $code;
}
$a;
};
my @A =3D qw( 3 54 22);
## to compute the max of array
print $reduce_ref->( '($a > $b) ? $a : $b', @A);
__END__
Please check it
------------------------------
Date: Wed, 21 Jan 2009 01:56:31 -0800 (PST)
From: rthangam <ramesh.thangamani@gmail.com>
Subject: Re: on Code references
Message-Id: <242af5c0-7099-4c44-9ec3-932fc4c56f29@a39g2000prl.googlegroups.com>
On Jan 21, 1:13=A0pm, sharma...@hotmail.com wrote:
> Hi Perl gurus,
>
> I am having trouble with calling subroutines using a reference
> variable.
>
> #!/usr/local/bin/perl
> use 5.8.0;
> use strict;
> use warnings;
>
> # anonymous subroutine stored in a reference variable
> my $reduce_ref =3D sub (&@) {
> =A0 =A0 =A0 =A0 my $code =3D shift;
> =A0 =A0 =A0 =A0 no strict 'refs';
>
> =A0 =A0 =A0 =A0 return shift unless @_ > 1;
>
> =A0 =A0 =A0 =A0 use vars qw($a $b);
>
> =A0 =A0 =A0 =A0 my $caller =3D caller;
> =A0 =A0 =A0 =A0 local(*{$caller."::a"}) =3D \my $a;
> =A0 =A0 =A0 =A0 local(*{$caller."::b"}) =3D \my $b;
>
> =A0 =A0 =A0 =A0 $a =3D shift;
> =A0 =A0 =A0 =A0 foreach (@_) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $b =3D $_;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $a =3D &{$code}();
> =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 $a;
>
> };
>
> my @A =3D qw( 3 54 22);
>
> ## to compute the max of array
> print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
> __END__
>
> ## this gives the following error:
> # Use of uninitialized value in numeric gt (>) at ...
> # Odd number of elements in anonymous hash at ...
> # Use of uninitialized value in anonymous hash ({}) at ...
> # Not a CODE reference at ...
>
> Had I used a named subroutine then the things work fine.
> What could be the problem here?
>
> with thanks,
You can use eval
#!/usr/local/bin/perl
use 5.8.0;
use strict;
use warnings;
use Data::Dumper;
# anonymous subroutine stored in a reference variable
my $reduce_ref =3D sub (&@) {
my $code =3D shift;
no strict 'refs';
return shift unless @_ > 1;
use vars qw($a $b);
my $caller =3D caller;
local(*{$caller."::a"}) =3D \my $a;
local(*{$caller."::b"}) =3D \my $b;
$a =3D shift;
foreach (@_) {
$b =3D $_;
$a =3D eval $code;
}
$a;
};
my @A =3D qw( 3 54 22 12 100);
## to compute the max of array
print $reduce_ref->( '($a > $b) ? $a : $b', @A);
__END__
------------------------------
Date: Wed, 21 Jan 2009 02:23:27 -0800 (PST)
From: sharma__r@hotmail.com
Subject: Re: on Code references
Message-Id: <edcc40ee-252f-4876-80d2-7f5f222cf7ca@r36g2000prf.googlegroups.com>
On Jan 21, 2:56 pm, rthangam <ramesh.thangam...@gmail.com> wrote:
> On Jan 21, 1:13 pm, sharma...@hotmail.com wrote:
>
>
>
> > Hi Perl gurus,
>
> > I am having trouble with calling subroutines using a reference
> > variable.
>
> > #!/usr/local/bin/perl
> > use 5.8.0;
> > use strict;
> > use warnings;
>
> > # anonymous subroutine stored in a reference variable
> > my $reduce_ref = sub (&@) {
> > my $code = shift;
> > no strict 'refs';
>
> > return shift unless @_ > 1;
>
> > use vars qw($a $b);
>
> > my $caller = caller;
> > local(*{$caller."::a"}) = \my $a;
> > local(*{$caller."::b"}) = \my $b;
>
> > $a = shift;
> > foreach (@_) {
> > $b = $_;
> > $a = &{$code}();
> > }
>
> > $a;
>
> > };
>
> > my @A = qw( 3 54 22);
>
> > ## to compute the max of array
> > print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
> > __END__
>
> > ## this gives the following error:
> > # Use of uninitialized value in numeric gt (>) at ...
> > # Odd number of elements in anonymous hash at ...
> > # Use of uninitialized value in anonymous hash ({}) at ...
> > # Not a CODE reference at ...
>
> > Had I used a named subroutine then the things work fine.
> > What could be the problem here?
>
> > with thanks,
>
> You can use eval
>
> #!/usr/local/bin/perl
> use 5.8.0;
> use strict;
> use warnings;
>
> use Data::Dumper;
>
> # anonymous subroutine stored in a reference variable
> my $reduce_ref = sub (&@) {
> my $code = shift;
> no strict 'refs';
>
> return shift unless @_ > 1;
>
> use vars qw($a $b);
>
> my $caller = caller;
> local(*{$caller."::a"}) = \my $a;
> local(*{$caller."::b"}) = \my $b;
>
> $a = shift;
> foreach (@_) {
> $b = $_;
> $a = eval $code;
> }
>
> $a;
>
> };
>
> my @A = qw( 3 54 22 12 100);
>
> ## to compute the max of array
> print $reduce_ref->( '($a > $b) ? $a : $b', @A);
>
> __END__
It works! But is possible to make it work without 'eval'?
------------------------------
Date: Wed, 21 Jan 2009 13:22:42 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: on Code references
Message-Id: <Xns9B9A55399C0F1asu1cornelledu@127.0.0.1>
sharma__r@hotmail.com wrote in news:0a2d735d-066f-4f45-98b8-
66fc1b95720f@n33g2000pri.googlegroups.com:
> I am having trouble with calling subroutines using a reference
> variable.
>
> #!/usr/local/bin/perl
> use 5.8.0;
> use strict;
> use warnings;
>
> # anonymous subroutine stored in a reference variable
> my $reduce_ref = sub (&@) {
> my $code = shift;
> no strict 'refs';
>
> return shift unless @_ > 1;
>
> use vars qw($a $b);
>
> my $caller = caller;
> local(*{$caller."::a"}) = \my $a;
> local(*{$caller."::b"}) = \my $b;
>
> $a = shift;
> foreach (@_) {
> $b = $_;
> $a = &{$code}();
> }
>
> $a;
> };
>
> my @A = qw( 3 54 22);
>
> ## to compute the max of array
> print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
> __END__
>
> ## this gives the following error:
> # Use of uninitialized value in numeric gt (>) at ...
> # Odd number of elements in anonymous hash at ...
> # Use of uninitialized value in anonymous hash ({}) at ...
> # Not a CODE reference at ...
Funny, it gives me:
C:\Temp> ttt
Array found where operator expected at C:\Temp\ttt.pl line 31, near "} "
(Missing operator before ?)
syntax error at C:\Temp\ttt.pl line 31, near "} @A" Execution of C:\Temp
\ttt.pl aborted due to compilation errors.
If I correct the last line:
print $reduce_ref->( sub { ($a > $b) ? $a : $b }, @A);
Then the script outputs 54.
The simple resolution of the problem aside, I am not sure why you want
to do this when you can use:
http://search.cpan.org/~gbarr/Scalar-List-Utils-1.19/lib/List/Util.pm
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Wed, 21 Jan 2009 06:30:11 -0800 (PST)
From: sharma__r@hotmail.com
Subject: Re: on Code references
Message-Id: <87dc667d-1916-4df1-9a77-5a7fe33d5bd0@v39g2000pro.googlegroups.com>
On Jan 21, 6:22=A0pm, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
> sharma...@hotmail.com wrote in news:0a2d735d-066f-4f45-98b8-
> 66fc1b957...@n33g2000pri.googlegroups.com:
>
>
>
> > I am having trouble with calling subroutines using a reference
> > variable.
>
> > #!/usr/local/bin/perl
> > use 5.8.0;
> > use strict;
> > use warnings;
>
> > # anonymous subroutine stored in a reference variable
> > my $reduce_ref =3D sub (&@) {
> > =A0 =A0 =A0my $code =3D shift;
> > =A0 =A0 =A0no strict 'refs';
>
> > =A0 =A0 =A0return shift unless @_ > 1;
>
> > =A0 =A0 =A0use vars qw($a $b);
>
> > =A0 =A0 =A0my $caller =3D caller;
> > =A0 =A0 =A0local(*{$caller."::a"}) =3D \my $a;
> > =A0 =A0 =A0local(*{$caller."::b"}) =3D \my $b;
>
> > =A0 =A0 =A0$a =3D shift;
> > =A0 =A0 =A0foreach (@_) {
> > =A0 =A0 =A0 =A0 =A0 $b =3D $_;
> > =A0 =A0 =A0 =A0 =A0 $a =3D &{$code}();
> > =A0 =A0 =A0}
>
> > =A0 =A0 =A0$a;
> > };
>
> > my @A =3D qw( 3 54 22);
>
> > ## to compute the max of array
> > print $reduce_ref->( { ($a > $b) ? $a : $b } @A);
> > __END__
>
> > ## this gives the following error:
> > # Use of uninitialized value in numeric gt (>) at ...
> > # Odd number of elements in anonymous hash at ...
> > # Use of uninitialized value in anonymous hash ({}) at ...
> > # Not a CODE reference at ...
>
> Funny, it gives me:
>
> C:\Temp> ttt
> Array found where operator expected at C:\Temp\ttt.pl line 31, near "} "
> =A0 =A0 =A0 =A0 (Missing operator before =A0?)
> syntax error at C:\Temp\ttt.pl line 31, near "} @A" Execution of C:\Temp
> \ttt.pl aborted due to compilation errors.
>
> If I correct the last line:
>
> print $reduce_ref->( sub { ($a > $b) ? $a : $b }, @A);
>
> Then the script outputs 54.
>
> The simple resolution of the problem aside, I am not sure why you want
> to do this when you can use:
>
> http://search.cpan.org/~gbarr/Scalar-List-Utils-1.19/lib/List/Util.pm
>
> Sinan
> --
> A. Sinan Unur <1...@llenroc.ude.invalid>
> (remove .invalid and reverse each component for email address)
>
> comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clp=
misc/
Thanks for resolving that!
The error messages are differing maybe because of the OS differences.
I ran on Sun-Solaris Unix.
I am using the code from List::MoreUtils module, but was trying to use
it
as a code reference rather than a named subroutine.
Regards,
------------------------------
Date: Wed, 21 Jan 2009 09:00:38 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: on Code references
Message-Id: <slrngnee4m.b11.tadmc@tadmc30.sbcglobal.net>
sharma__r@hotmail.com <sharma__r@hotmail.com> wrote:
> On Jan 21, 6:22 pm, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
>> Array found where operator expected at C:\Temp\ttt.pl line 31, near "} "
>> (Missing operator before ?)
>> syntax error at C:\Temp\ttt.pl line 31, near "} @A" Execution of C:\Temp
>> \ttt.pl aborted due to compilation errors.
> The error messages are differing maybe because of the OS differences.
It is a syntax error.
The syntax of Perl does not depend on the OS.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Wed, 21 Jan 2009 00:45:06 -0800 (PST)
From: spanish26@gmail.com
Subject: returning calculation results in perl scripts
Message-Id: <5b1c0202-fbb0-4928-80fc-5bab2aae0891@l33g2000pri.googlegroups.com>
Hello,
I am running a program using perl scripts to calculate the proximity
between two search terms when they are entered in the form box, i can
successfully view the form boxes, but when i click on the calculate
button after entering the term, i do not get any result, the values do
not show any result
E.g lets say i enter two terms "egg" term 1 and "fish" term 2 and i
click on the calculate button, i am supposed to see the following
results:
NGD(x,y) = some value
Term1: +:"egg"
f(x) = some value
log f(x) = some value
Term 2: +"fish"
f(y) = some value
log f (y) = some value
Intersection: +"egg" + "fish"
f(x,y) = some value
i dont get any result when click the calculate.
The source code is below :
#!"C:\Program Files\xampp\perl\bin\perl.exe"
# ngd-calculator.cgi
#
# wjt
# http://history.uwo.ca/faculty/turkel/
#
# 5 aug 2006
use SOAP::Lite;
use CGI;
use POSIX qw(log10);
use List::Util qw(max min);
# Google API developer's key
my $google_key = '<Insert Key Here>';
# Google WSDL
my $google_wsdl = "http://api.google.com/GoogleSearch.wsdl";
# my $google_wsdl = "./GoogleSearch.wsdl";
# start, maxResults, filter, restrict, safeSearch, lr, ie, oe
my @params = (0, 10, 0, '', 0, '', 'utf-8', 'utf-8');
# Do Google search and return count
sub do_search {
unshift (@params, ($google_key, $_[0]));
my $result =
SOAP::Lite
-> service($google_wsdl)
-> doGoogleSearch(@params);
shift @params;
shift @params;
return $result->{estimatedTotalResultsCount};
}
# Create the search page
$query = new CGI;
print $query->header;
print $query->start_html('NGD Calculator');
print "<H1>Normalized Google Distance (NGD) Calculator</H1>";
print '<p>';
print 'For information about NGD see Rudi Cilibrasi and Paul Vitanyi,
"';
print '<a href="http://www.arxiv.org/PS_cache/cs/pdf/
0412/0412098.pdf">';
print 'Automatic Meaning Discovery Using Google</a>."';
print '</p>';
# Print the search box form
print $query->startform;
print '<strong>Enter term 1</strong> ',$query->textfield('term1');
print '<br />';
print '<strong>Enter term 2</strong> ',$query->textfield('term2');
print '<br />';
print $query->submit('form_1','Calculate');
print $query->endform;
print '<br />';
$x = ''; $y = ''; $xy = '';
$x = '+"' . $query->param('term1') . '"';
$y = '+"' . $query->param('term2') . '"';
$xy = $x . " " . $y;
$fx = 1; $fy = 1; $fxy = 1;
$logfx = 0; $logfy = 0; $logfxy = 0; $logm = 0;
$maxlogfxy = 0; $minlogfxy = 0;
$ngd = 0;
# Best guess as of Jan 2006
$m = 11828505634;
if ($x && $y) {
# Determine frequencies
$fx = do_search( $x );
$fy = do_search( $y );
$fxy = do_search( $xy );
# Determine logarithms
$logm = log10( $m );
$logfx = log10( $fx );
$logfy = log10( $fy );
$logfxy = log10( $fxy );
# Determine max and min
@fxy = ($logfx, $logfy);
$maxlogfxy = max @fxy;
$minlogfxy = min @fxy;
# Calculate NGD
$ngd = ($maxlogfxy - $logfxy) / ($logm - $minlogfxy);
print 'NGD(x,y) = ' . $ngd . '<br /><br />';
print 'Term 1: ' . $x . '<br />';
print 'f(x) = ' . $fx . '<br />';
print 'log f(x) = ' . $logfx . '<br /><br />';
print 'Term 2: ' . $y . '<br />';
print 'f(y) = ' . $fy . '<br />';
print 'log f(y) = ' . $logfy . '<br /><br />';
# print 'max(log f(x),log f(y)) = ' . $maxlogfxy . '<br />';
# print 'min(log f(x),log f(y)) = ' . $minlogfxy . '<br /><br />';
print 'Intersection: ' . $xy . '<br />';
print 'f(x,y) = ' . $fxy . '<br />';
print 'log f(x,y) = ' . $logfxy . '<br /><br />';
print 'M: ' . $m . '<br />';
print 'log M: ' . $logm . '<br />';
}
print qq{<P><A HREF="http://history.uwo.ca/faculty/turkel">Digital
History at Western</A>};
print $query->end_html;
Thank you
------------------------------
Date: Wed, 21 Jan 2009 13:11:01 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: returning calculation results in perl scripts
Message-Id: <Xns9B9A533E479B3asu1cornelledu@127.0.0.1>
spanish26@gmail.com wrote in news:5b1c0202-fbb0-4928-80fc-5bab2aae0891@l33g2000pri.googlegroups.com:
> I am running a program using perl scripts to calculate the proximity
> between two search terms when they are entered in the form box, i can
> successfully view the form boxes, but when i click on the calculate
> button after entering the term, i do not get any result, the values do
> not show any result
I do not have a Google API key so I can't try out your code. AFAIK,
Google no longer hands out keys for that service:
http://google-code-updates.blogspot.com/2006/12/beyond-soap-search-api.html
I will point out that you need to divide the problem into two separate
questions:
1) Are you getting back results?
2) Are those results being output properly?
As I said, I cannot look into (1) above. (2) is a stealth CGI question,
not a Perl question, but let's look at the code below.
> #!"C:\Program Files\xampp\perl\bin\perl.exe"
> # ngd-calculator.cgi
> #
> # wjt
> # http://history.uwo.ca/faculty/turkel/
> #
> # 5 aug 2006
Here, you are missing:
use strict;
use warnings;
Please see the posting guidelines for why you should include
those pragmas so that you can help yourself and help others help
you more effectively.
> use SOAP::Lite;
> use CGI;
> use POSIX qw(log10);
> use List::Util qw(max min);
<snip>
Code accessing Google Search service. You should test this part
separately from the command line.
</snip>
> # Create the search page
> $query = new CGI;
my $query = CGI->new;
Avoid indirect object notation.
> print $query->header;
> print $query->start_html('NGD Calculator');
> print "<H1>Normalized Google Distance (NGD) Calculator</H1>";
Here, you are mixing CGI.pm's output generation routines with plain HTML.
I would recommend using HTML::Template or some other similar solution
to separate presentation from logic.
Also, by default, CGI.pm outputs XHTML in which upper case tags
are not valid AFAIK.
In any case, you should try the script from the command line to see
what happens there. See
http://search.cpan.org/~lds/CGI.pm-3.42/CGI.pm#DEBUGGING
Please provide a small, self-contained example others can run from
the command line that still exhibits the problem.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Wed, 21 Jan 2009 10:26:47 +0000 (UTC)
From: comps@riffraff.plig.net (Vicky Conlan)
Subject: Re: What do you need to have to be considered a Master at Perl?
Message-Id: <gl6t97$93j$1@magenta.plig.net>
According to <cwilbur@chromatico.net>:
>Well, we don't grade things objectively -- we've found, for instance,
>that it's often better to hire a smart person who doesn't know Perl
>details (and then teach him or her Perl) than to hire a dumb person
>who's mastered Perl.
I agree. Which is why I like to make sure /I/ mark Perl tests for people
I am expected to work with, so that I can be lenient with people who get
the right idea but not the exact syntax on the programming tasks. Which
I know that most other people wouldn't bother with, they'd just mark
against the stock answer (hopefully testing things that weren't exactly
as written!). Unfortunately part of the test we currently use does seem
to focus on "do you know the precise syntax of this Perl function", which
seems a little pointless, imho.
--
------------------------------
Date: Wed, 21 Jan 2009 16:43:00 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: What do you need to have to be considered a Master at Perl?
Message-Id: <slrngnegk5.djo.hjp-usenet2@hrunkner.hjp.at>
On 2009-01-20 22:13, Tad J McClellan <tadmc@seesig.invalid> wrote:
> Charlton Wilbur <cwilbur@chromatico.net> wrote:
>> One of my favorites:
>>
>> You have a string that comes from a freeform text field where the user
>> is expected to enter a telephone number.
>
>
> I sure hope that is restricted to "a US phone number"...
And I hope it isn't. Local conventions for phone numbers, street
addresses, etc. differ a lot, and software tends to be used outside that
local context. Forcing a non-US customer to guess US conventions, or
worse, rejecting a phone number because it has too many or too few
digits is a major annoyance.
As a real-world example, some time ago I decided to subscribe to the
German Perl magazine $foo. I couldn't enter my street address because
the input validation insisted on a street name and a purely numeric
house number - "Nordwestbahnstraße 93-95/30" simply couldn't match
unless I mangled it so much that I doubted that the postman would
recognize it. Fortunately Renée Bäcker reads his email and fixed the
problem within a day. A larger and less flexible firm might have lost me
as a customer.
hp
------------------------------
Date: Wed, 21 Jan 2009 08:06:46 -0800 (PST)
From: cartercc <cartercc@gmail.com>
Subject: Re: What do you need to have to be considered a Master at Perl?
Message-Id: <8b062303-0574-459f-aaf4-3c43201cc28c@b38g2000prf.googlegroups.com>
On Jan 21, 2:13=A0am, Charlton Wilbur <cwil...@chromatico.net> wrote:
> Well, we don't grade things objectively -- we've found, for instance,
> that it's often better to hire a smart person who doesn't know Perl
> details (and then teach him or her Perl) than to hire a dumb person
> who's mastered Perl. =A0
I've had a number of different kinds of jobs, but never a programming
job, even though I've interviewed for quite a few of them.
We can all tell our stories of HR and management types interviewing
developers, so I won't share any of mine (unless we get into telling
war stories.)
However, I will relate the ASTONISHING FACT (!!!) that HR and
management types CANNOT UNDERSTAND (!!!) this simple truth:
Competent programmers can program in any language whether or not they
know it; incompetent programmers cannot program in any language
whether or not they know it.
(I'm not an especially competent programmer, but I've had the
opportunity to write projects in about a dozen different languages.
It's much better to use a language that you know pretty well, but it's
possible to use any language -- particularly true if you are in a MS
environment where you can pretty much figure it out whether you use
VB, C#, J#, C++, etc. and rely on the VS IDE to do your drags and
drops.)
CC
------------------------------
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 V11 Issue 2145
***************************************