[24105] in Perl-Users-Digest
Perl-Users Digest, Issue: 6299 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Mar 24 18:10:34 2004
Date: Wed, 24 Mar 2004 15:10:08 -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, 24 Mar 2004 Volume: 10 Number: 6299
Today's topics:
script reading itself for variables? (hennessy)
Re: script reading itself for variables? (Anno Siegel)
Re: script reading itself for variables? (hennessy)
Re: script reading itself for variables? <tadmc@augustmail.com>
Re: SOAP::Lite <hillmw@ram.lmtas.lmco.com>
The "value" of a block in 'map' <bernie@fantasyfarm.com>
Re: The "value" of a block in 'map' <1usa@llenroc.ude>
Re: The "value" of a block in 'map' <noreply@gunnar.cc>
Re: The "value" of a block in 'map' <vetro@online.no>
Re: The "value" of a block in 'map' <tadmc@augustmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 24 Mar 2004 14:45:03 -0000
From: hennessy@earl-grey.cloud9.net (hennessy)
Subject: script reading itself for variables?
Message-Id: <10637nftlk4pd58@corp.supernews.com>
Hi,
I thought it'd be clever to have a script read itself to find
references to external tools (like ping, traceroute, etc) and then build a
tool hash to contain them.. but it's not working :p The script seems to
be able to read the document and parse the variables but when I attempt to
use those variables I get nada.. script below:
open (IN, $0) || die "$0: can't open [$0]!\n";
while (<IN>) {
next if $_ !~ m#\$tools-\>\{(\S+)\}#;
next if m#\$tools-\>\{\$tool\}#;
next if m#\$tools-\>\{\$1\}#;
chomp;
s#\$tools->\{(\S+)\}#chomp($tools->{$1} = `which $1`)#eg;
}
close (IN);
foreach my $tool (sort keys %$tools) {
print STDOUT "toollist: [$tool]: [$tools->{$tool}]\n";
}
print STDOUT "foo: [$tools->{'traceroute'}]\n";
results;
toollist: ['traceroute']: [/usr/sbin/traceroute]
foo: []
So it actually goes and builds the $tools hashref, but when I specify
values outside of a foreach the values are undefined..
Why would this work within a foreach and not when called directly? Is it
a single-quote thing?
Cheers,
- Matt
--
"When in doubt, use brute force."
- Ken Thompson
------------------------------
Date: 24 Mar 2004 15:30:57 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: script reading itself for variables?
Message-Id: <c3s9jh$9r9$2@mamenchi.zrz.TU-Berlin.DE>
hennessy <hennessy@earl-grey.cloud9.net> wrote in comp.lang.perl.misc:
> Hi,
> I thought it'd be clever to have a script read itself to find
> references to external tools (like ping, traceroute, etc) and then build a
> tool hash to contain them.. but it's not working :p The script seems to
> be able to read the document and parse the variables but when I attempt to
> use those variables I get nada.. script below:
>
> open (IN, $0) || die "$0: can't open [$0]!\n";
> while (<IN>) {
> next if $_ !~ m#\$tools-\>\{(\S+)\}#;
> next if m#\$tools-\>\{\$tool\}#;
> next if m#\$tools-\>\{\$1\}#;
> chomp;
> s#\$tools->\{(\S+)\}#chomp($tools->{$1} = `which $1`)#eg;
> }
> close (IN);
>
> foreach my $tool (sort keys %$tools) {
> print STDOUT "toollist: [$tool]: [$tools->{$tool}]\n";
> }
>
> print STDOUT "foo: [$tools->{'traceroute'}]\n";
>
> results;
> toollist: ['traceroute']: [/usr/sbin/traceroute]
> foo: []
>
> So it actually goes and builds the $tools hashref, but when I specify
> values outside of a foreach the values are undefined..
That's because your hash keys are enclosed in "'...'", as the loop
output shows. In
print STDOUT "foo: [$tools->{'traceroute'}]\n";
the single quotes are used to quote the key, the hash doesn't see them.
Try
print STDOUT "foo: [$tools->{"'traceroute'"}]\n";
> Why would this work within a foreach and not when called directly? Is it
> a single-quote thing?
You got it.
Anno
------------------------------
Date: Wed, 24 Mar 2004 15:48:21 -0000
From: hennessy@earl-grey.cloud9.net (hennessy)
Subject: Re: script reading itself for variables?
Message-Id: <1063be55md4a32b@corp.supernews.com>
In article <c3s9jh$9r9$2@mamenchi.zrz.TU-Berlin.DE>,
Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
> print STDOUT "foo: [$tools->{"'traceroute'"}]\n";
interesting sidenote, leaving either quotes out works quite nicely!
(change)
print STDOUT "foo: [$tools->{traceroute}]\n";
(results)
toollist: [traceroute]: [/usr/sbin/traceroute]
foo: [/usr/sbin/traceroute]
(I tend to quote incessantly)
Thanks for the quick check!!
- Matt
--
"When in doubt, use brute force."
- Ken Thompson
------------------------------
Date: Wed, 24 Mar 2004 15:58:19 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: script reading itself for variables?
Message-Id: <slrnc6413r.jng.tadmc@magna.augustmail.com>
hennessy <hennessy@earl-grey.cloud9.net> wrote:
> In article <c3s9jh$9r9$2@mamenchi.zrz.TU-Berlin.DE>,
> Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
>> print STDOUT "foo: [$tools->{"'traceroute'"}]\n";
>
> interesting sidenote, leaving either quotes out works quite nicely!
Just like perldata.pod says it will:
In fact, an identifier within such curlies is forced to be a string,
as is any simple identifier within a hash subscript. Neither need
quoting. Our earlier example, $days{'Feb'} can be written as
$days{Feb} and the quotes will be assumed automatically. But
anything more complicated in the subscript will be interpreted as
an expression.
So it will only work for tools whose name matches /^[a-z_]\w*$/i.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 24 Mar 2004 16:58:20 -0600
From: Michael Hill <hillmw@ram.lmtas.lmco.com>
To: Simon Taylor <simon@unisolve.com.au>
Subject: Re: SOAP::Lite
Message-Id: <4062128C.291F3B66@ram.lmtas.lmco.com>
>
> I ran the tempobj.pl script above and got reasonable output:
>
> [simon@acacia perl]$ perl clpm.pl
> 37.7777777777778[simon@acacia perl]$
>
I added
use SOAP::Lite +trace => 'debug';
and got an error on line 38 in temper.cgi.
I found that:
my $self = $shift;
should have been:
my $self = shift;
Thanks for looking.
Mike
------------------------------
Date: Wed, 24 Mar 2004 15:13:20 -0500
From: Bernie Cosell <bernie@fantasyfarm.com>
Subject: The "value" of a block in 'map'
Message-Id: <fap360pvsc9vrrbua3361fue9hcffa3omf@library.airnews.net>
Is there any way, other than "the last expression evaluated" to set the
'value' of a BLOCK done in a map {BLOCK} list; ? I note that it doesn't
even say in the docs for map [nor for grep] what the value of BLOCK *IS*,
but it is easy enough to guess that it is at least the value of the last
expression. But I'm finding that a bit limiting. In 'eval' it *DOES*
specify the value:
>>> In both forms, the value returned is the value of
>>> the last expression evaluated inside the mini-pro
>>> gram; a return statement may be also used, just as
>>> with subroutines.
but that doesn't work for map: I tried map {stuff; return something if
BADNESS; more stuff} and for my trouble got
Can't return outside a subroutine at ...
Short of doing:
map { eval { my block with returns} }
is there any other way for map [probably also grep, etc] to provide a value
other than the very last statement in the block?
----------------------------------------------------------------------
Meta question: in researching the above, I ran across this:
>>>... The following all
>>> do the same thing:
>>> if (!open(FOO)) { die "Can't open $FOO: $!"; }
>>> die "Can't open $FOO: $!" unless open(FOO);
>>> open(FOO) or die "Can't open $FOO: $!"; # FOO or bust!
>>> open(FOO) ? 'hi mom' : die "Can't open $FOO: $!";
>>> # a bit exotic, that last one
>>>
Isn't "that last one" not only exotic, but incorrect??? [in fact, won't it
*always* 'die' regardless of whether the open works or not?]
[this all with 5.6.1].
/Bernie\
--
Bernie Cosell Fantasy Farm Fibers
bernie@fantasyfarm.com Pearisburg, VA
--> Too many people, too few sheep <--
------------------------------
Date: 24 Mar 2004 20:49:20 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude>
Subject: Re: The "value" of a block in 'map'
Message-Id: <Xns94B6A0F3EEFABasu1cornelledu@132.236.56.8>
Bernie Cosell <bernie@fantasyfarm.com> wrote in
news:fap360pvsc9vrrbua3361fue9hcffa3omf@library.airnews.net:
> Is there any way, other than "the last expression evaluated" to set
> the 'value' of a BLOCK done in a map {BLOCK} list; ?
> I note that it doesn't even say in the docs for map [nor for grep]
> what the value of BLOCK *IS*, but it is easy enough to guess that
> it is at least the value of the last expression.
perldoc -f map
map BLOCK LIST
map EXPR,LIST
Evaluates the BLOCK or EXPR for each element of LIST (locally
setting $_ to each element) and returns the list value composed
of the results of each such evaluation. In scalar context,
returns the total number of elements so generated. Evaluates
BLOCK or EXPR in list context, so each element of LIST may
produce zero, one, or more elements in the returned value.
If that explanation is not enough,you'll need to post an example of what
you are trying to achieve.
...
> Meta question: in researching the above, I ran across this:
>>>> open(FOO) ? 'hi mom' : die "Can't open $FOO: $!";
>>>> # a bit exotic, that last one
> Isn't "that last one" not only exotic, but incorrect??? [in fact,
> won't it *always* 'die' regardless of whether the open works or not?]
Ah-em ... why omit the name of the file you are trying to open?
perldoc -f open
Open returns nonzero upon success, the undefined value
otherwise.
C:\> perl -e "open(my $foo, '<', 'foo.txt') ? print 'hi mom' : die $!;
No such file or directory at -e line 1.
C:\> touch foo.txt
C:\> perl -e "open(my $foo, '<', 'foo.txt') ? print 'hi mom' : die $!;
hi mom
Hope no one is stopping you from trying things out on your own computer.
Sinan.
--
A. Sinan Unur
1usa@llenroc.ude (reverse each component for email address)
------------------------------
Date: Wed, 24 Mar 2004 21:56:35 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: The "value" of a block in 'map'
Message-Id: <c3ssvj$2b4vli$1@ID-184292.news.uni-berlin.de>
Bernie Cosell wrote:
> I tried map {stuff; return something if BADNESS; more stuff} and
> for my trouble got
>
> Can't return outside a subroutine at ...
>
> Short of doing:
> map { eval { my block with returns} }
>
> is there any other way for map [probably also grep, etc] to provide
> a value other than the very last statement in the block?
Why don't you just do:
map {
# initial processing;
if (BADNESS) {
'something'
} else {
# more processing;
'something else'
}
}
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 24 Mar 2004 22:14:21 +0100
From: Vetle Roeim <vetro@online.no>
Subject: Re: The "value" of a block in 'map'
Message-Id: <m3y8pq0wya.fsf@quimby.dirtyhack.org>
* Bernie Cosell
[...]
> Short of doing:
> map { eval { my block with returns} }
>
> is there any other way for map [probably also grep, etc] to provide a value
> other than the very last statement in the block?
Um... Can't you just put the stuff in the block in a subroutine, or
doesn't this meet your needs? I.e.:
sub my_sub {
stuff;
return something if BADNESS;
more stuff;
return something else;
}
map { my_sub; } @list;
[...]
--
#!/usr/bin/vr
------------------------------
Date: Wed, 24 Mar 2004 16:15:36 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: The "value" of a block in 'map'
Message-Id: <slrnc64248.jng.tadmc@magna.augustmail.com>
Bernie Cosell <bernie@fantasyfarm.com> wrote:
> Is there any way, other than "the last expression evaluated" to set the
^^^^^^^^^
^^^^^^^^^
> 'value' of a BLOCK done in a map {BLOCK} list; ?
> even say in the docs for map [nor for grep] what the value of BLOCK *IS*,
> but it is easy enough to guess that it is at least the value of the last
> expression.
Not "the last expression" but rather "the last expression evaluated".
(subject to your program's flow control logic.)
> is there any other way for map [probably also grep, etc] to provide a value
> other than the very last statement in the block?
Use any control structure you choose to control what will be *evaluated*
(ie. executed) last.
> Meta question: in researching the above, I ran across this:
>>>> open(FOO) ? 'hi mom' : die "Can't open $FOO: $!";
>>>> # a bit exotic, that last one
>>>>
>
> Isn't "that last one" not only exotic, but incorrect???
No.
> [in fact, won't it
> *always* 'die' regardless of whether the open works or not?]
No, but if you don't tell us why you think that, we cannot point
out where your thinking went wrong...
Did you try it with warnings enabled?
Do you know how the ?: operator works?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
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 V10 Issue 6299
***************************************