[22062] in Perl-Users-Digest
Perl-Users Digest, Issue: 4284 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Dec 18 18:10:40 2002
Date: Wed, 18 Dec 2002 15:10:13 -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, 18 Dec 2002 Volume: 10 Number: 4284
Today's topics:
Why does Perl handle "return undef;" differently then " <asimmons@mitre.org>
Re: Why does Perl handle "return undef;" differently th <uri@stemsystems.com>
Re: Why does Perl handle "return undef;" differently th <asimmons@mitre.org>
Re: Why does Perl handle "return undef;" differently th <krahnj@acm.org>
Re: Why does Perl handle "return undef;" differently th (Jay Tilton)
Re: Why is system call exit code factor of 256? <bart.lateur@pandora.be>
Wierd loop/chdir behavior (Steve Walker)
Re: Wierd loop/chdir behavior (Anno Siegel)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 18 Dec 2002 16:24:13 -0500
From: "Aaron Simmons" <asimmons@mitre.org>
Subject: Why does Perl handle "return undef;" differently then "return;"
Message-Id: <atqp0b$246$1@newslocal.mitre.org>
What actually gets returned when a "return;" is done? Why doesn't it do an
implicit "return undef"?
Example:
#####################
printArgs('1st', test(), '2nd');
printArgs('1st', test2(), '2nd');
sub test { return }
sub test2 { return undef }
sub printArgs { print join(',', @_) . "\n" }
#####################
This REALLY presents a problem with code like this:
#####################
subName($var1, subName2(), $var2);
sub subName
{
my ($arg1, $arg2, $arg3) = @_;
# ...
}
#####################
$arg2 and $arg3 won't always be defined -- it depends if subName2() returns
a value or just does "return;"
My opinion: either (a) "return;" should be illegal or (b) "return;" should
do the same thing as "return undef;"
Thoughts? Can anyone explain why "return;" doesn't do an implicit "return
undef;"?
--
Aaron Simmons
G066-Software Systems Eng, Lead
(703) 883-3394
AaronSimm1 (AIM)
------------------------------
Date: Wed, 18 Dec 2002 21:27:39 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Why does Perl handle "return undef;" differently then "return;"
Message-Id: <x7y96nqaph.fsf@mail.sysarch.com>
>>>>> "AS" == Aaron Simmons <asimmons@mitre.org> writes:
AS> What actually gets returned when a "return;" is done? Why doesn't
AS> it do an implicit "return undef"?
AS> printArgs('1st', test(), '2nd');
AS> printArgs('1st', test2(), '2nd');
AS> sub test { return }
AS> sub test2 { return undef }
AS> sub printArgs { print join(',', @_) . "\n" }
AS> My opinion: either (a) "return;" should be illegal or (b) "return;" should
AS> do the same thing as "return undef;"
AS> Thoughts? Can anyone explain why "return;" doesn't do an implicit "return
AS> undef;"?
read perlsub carefully. and check your calling context.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Wed, 18 Dec 2002 17:04:53 -0500
From: "Aaron Simmons" <asimmons@mitre.org>
Subject: Re: Why does Perl handle "return undef;" differently then "return;"
Message-Id: <atqrck$2eq$1@newslocal.mitre.org>
OK, from PerlDoc:
"If no EXPR is given, returns an empty list in list context, the undefined
value in scalar context, and (of course) nothing at all in a void context."
So for this code,
printArgs('1st', test(), '2nd');
test() is being called in a "void context" -- so in order to fix this I need
to do this:
printArgs('1st', scalar(test()), '2nd');
but that's just plain silly, cumbersome, and unclear. Why can't Perl assume
that in this context (a list of arguments for a subroutine/method call),
that we are dealing with a scalar if it returns "nothing"?
Also, what is "nothing" versus "undef"? I don't see any difference in the
context of scalars? Is there any Perl function that distinguishes between
these two for scalars?
######################
my $a;
my $b = undef;
my $c = test();
print 'a:' . defined($a) . "\n";
print 'b:' . defined($b) . "\n";
print 'c:' . defined($c) . "\n";
sub test { return }
######################
Here's another strange thing dealing with "nothing" -- consider this:
######################
# (1) This fails (compilation error)
my @a = (,,);
# (2) But this works
my @a = (test(),test(),test());
sub test { return }
######################
Aren't #1 and #2 logically the same? (I know someone is probably going to
say it's a compile-time vs. run-time issue. But this just makes me more
confused what "nothing" is--why is there a "nothing" when we already have
undef?)
--
Aaron Simmons
G066-Software Systems Eng, Lead
(703) 883-3394
AaronSimm1 (AIM)
"Uri Guttman" <uri@stemsystems.com> wrote in message
news:x7y96nqaph.fsf@mail.sysarch.com...
> >>>>> "AS" == Aaron Simmons <asimmons@mitre.org> writes:
>
> AS> What actually gets returned when a "return;" is done? Why doesn't
> AS> it do an implicit "return undef"?
>
> AS> printArgs('1st', test(), '2nd');
> AS> printArgs('1st', test2(), '2nd');
>
>
> AS> sub test { return }
> AS> sub test2 { return undef }
>
> AS> sub printArgs { print join(',', @_) . "\n" }
>
> AS> My opinion: either (a) "return;" should be illegal or (b) "return;"
should
> AS> do the same thing as "return undef;"
>
> AS> Thoughts? Can anyone explain why "return;" doesn't do an implicit
"return
> AS> undef;"?
>
> read perlsub carefully. and check your calling context.
>
> uri
>
> --
> Uri Guttman ------ uri@stemsystems.com --------
http://www.stemsystems.com
> ----- Stem and Perl Development, Systems Architecture, Design and
Coding ----
> Search or Offer Perl Jobs ----------------------------
http://jobs.perl.org
------------------------------
Date: Wed, 18 Dec 2002 22:40:44 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Why does Perl handle "return undef;" differently then "return;"
Message-Id: <3E00F921.256DCFCA@acm.org>
Aaron Simmons wrote:
>
> OK, from PerlDoc:
> "If no EXPR is given, returns an empty list in list context, the undefined
> value in scalar context, and (of course) nothing at all in a void context."
>
> So for this code,
> printArgs('1st', test(), '2nd');
>
> test() is being called in a "void context" -- so in order to fix this I need
> to do this:
> printArgs('1st', scalar(test()), '2nd');
No, test is being called in a list context:
$ perl -e'
printArgs("1st", test(), "2nd");
sub test {return defined(wantarray)?wantarray?"LIST":"SCALAR":"VOID"}
sub printArgs { print join(",", @_) . "\n" }
'
1st,LIST,2nd
This is just how lists work. :-)
$ perl -e'
printArgs("1st", (), "2nd");
@a = ("1st", (), "2nd");
printArgs( @a );
sub printArgs { print join(",", @_) . "\n" }
'
1st,2nd
1st,2nd
John
--
use Perl;
program
fulfillment
------------------------------
Date: Wed, 18 Dec 2002 22:38:46 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Why does Perl handle "return undef;" differently then "return;"
Message-Id: <3e00f184.84401870@news.erols.com>
[Please do not top-post replies. Please trim reply-quoted material to
the minimum necessary to establish context.]
"Aaron Simmons" <asimmons@mitre.org> wrote:
: OK, from PerlDoc:
: "If no EXPR is given, returns an empty list in list context, the undefined
: value in scalar context, and (of course) nothing at all in a void context."
:
: So for this code,
: printArgs('1st', test(), '2nd');
:
: test() is being called in a "void context"
No, it is not.
"void context" means that whatever the sub may return is wafted into
the ether--discarded and never seen again.
In that example, the return from test() is not discarded.
It becomes part of the argument list passed to printArgs().
test() is being called in list context.
------------------------------
Date: Wed, 18 Dec 2002 19:11:33 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Why is system call exit code factor of 256?
Message-Id: <0vh10vgba5768ll3rdqi321f60q7q0l3km@4ax.com>
Brandon Hoppe wrote:
>The value returned from a system() call to a perl program returns the exit
>value * 256. For example, if the program exited with exit(2), then system()
>would return 512.
>
>Why is this?
From `perldoc -f system`, I quote:
You can check all the failure possibilities by inspecting "$?"
like this:
$exit_value = $? >> 8;
$signal_num = $? & 127;
$dumped_core = $? & 128;
which explains what the lower 8 bits of this result are used for.
--
Bart.
------------------------------
Date: 18 Dec 2002 11:51:07 -0800
From: J_Steven_Walker@hotmail.com (Steve Walker)
Subject: Wierd loop/chdir behavior
Message-Id: <534c971c.0212181151.71e1e1d2@posting.google.com>
Anybody have an idea what is going on here?
Background:
goal - build a configuration file parser (yes, I know there is a
package now but I didn't then) for use by a perl script.
requirements -
1) since script will use 'find' on a directory that includes
symbolic links and I don't want to include '-follow', paths configured
to be searched should be globbed/converted to an absolute path before
use.
2) multiple target paths are to be considered
Sample config: (file name clarit.conf)
BuildPath=~/Data/build
ActivePath=Data/active
NewDataPath=~/epaa:~/epab:~/epac:~/epaa:~/epab:~/epac
WebPath=~/clhtml
Note: these are all symbolic links
Process: My script reads the file looking for keywords and saving the
associated data. When it hits NewDataPath it starts a loop to harvest
and convert multiple paths. The weirdness is that the chdir to the
target directory fails EVERY OTHER pass of the loop. You can
serialize the effort as much as you want and each one will work but
each pass of the loop that fails prevent chdir to all of the path
elements in that pass. This sample uses a strange loop structure but
I started with 'foreach' as the obvious choice and worked my way
through a number of other looping structures till this method of last
resort. Some of this stuff will seem clumsy and would not be here but
was useful trying to figure out what was going on.
Just for the record, my system is 5.1 Tru64 on an Alpha server. I
started with the Perl 5.005 native distribution then upgraded to 5.8
thinking that it might just be a problem with the older version. Both
versions have the same behavior.
Does your Unix platform do this too?
Thanks,
Steve
The script:
#! /usr/bin/perl
#
# Wed Dec 18 14:41:48 EST 2002
#
# Usage: clconfig
#
# This script scans a configuration file to determine key variables
to use when
# building new Clarit databases
#
# jsw
#
$,="\n";
$conf_file = shift @ARGV;
$conf_file ="clarit.conf" unless defined($conf_file);
print "opening $conf_file...\n";
exit unless (-e $conf_file);
open (CONF,"$conf_file");
chop ($work_dir = `/usr/bin/pwd`);
print "Config open at $work_dir";
# Find the paths to be searched and the name of the binding file
where the
# database segments will be defined.
while (<CONF>)
{
@build = split(/[:\n]/,$') if m/^\s?BuildPath\s?=/ ;
@active = split(/[:\n]/,$') if m/^\s?ActivePath\s?=/ ;
@web = split(/[:\n]/,$') if m/^\s?WebPath\s?=/ ;
@newdata = split(/[:\n]/,$') if m/^\s?NewDataPath\s?=/ ;
}
close (CONF);
print "Build at @build[0]\n";
chop($bp = `/usr/bin/pwd`) if chdir( glob "@build[0]");
chdir $work_dir ;
print "Active data at @active[0]\n";
chop($ap = `/usr/bin/pwd`) if chdir( glob "@active[0]");
chdir $work_dir ;
print "Web output at @web[0]\n";
chop($wp = `/usr/bin/pwd`) if chdir( glob "@web[0]");
chdir $work_dir ;
print @newdata,"\n";
# Wierdness starts here
TOP: chdir $work_dir ;
$ddir=shift @newdata;
print "New Scan data at $ddir\n";
if (chdir(glob "$ddir"))
{
chop($pwd=`/usr/bin/pwd`);
print "now in $ddir ($pwd)\n" ;
$dp .= " $pwd" ;
chdir $ddir;
chop($pwd=`/usr/bin/pwd`);
print "Second try in $ddir ($pwd)\n" ;
}
$ddir=shift @newdata;
print "New Scan data at $ddir\n";
if (chdir(glob "$ddir"))
{
chop($pwd=`/usr/bin/pwd`);
print "now in $ddir ($pwd)\n" ;
$dp .= " $pwd" ;
chdir $ddir;
chop($pwd=`/usr/bin/pwd`);
print "Second try in $ddir ($pwd)\n" ;
}
goto TOP if (@newdata);
print "$bp $ap $wp $dp\n" ;
Output:
> clconfig
opening clarit.conf...
Config open at /usr/clarit/home/clarit
Build at ~/Data/build
Active data at Data/active
Web output at ~/clhtml
~/epaa
~/epab
~/epac
~/epaa
~/epab
~/epac
New Scan data at ~/epaa
now in ~/epaa (/clarimg5/clarit/images/epa-cina)
Second try in ~/epaa (/clarimg5/clarit/images/epa-cina)
New Scan data at ~/epab
now in ~/epab (/clarimg3/clarit/images/epa-cinb)
Second try in ~/epab (/clarimg3/clarit/images/epa-cinb)
New Scan data at ~/epac
now in ~/epac (/usr/clarit/home/clarit)
Second try in ~/epac (/usr/clarit/home/clarit)
New Scan data at ~/epaa
now in ~/epaa (/usr/clarit/home/clarit)
Second try in ~/epaa (/usr/clarit/home/clarit)
New Scan data at ~/epab
now in ~/epab (/clarimg3/clarit/images/epa-cinb)
Second try in ~/epab (/clarimg3/clarit/images/epa-cinb)
New Scan data at ~/epac
now in ~/epac (/clarimg6/clarit/images/epa-cinc)
Second try in ~/epac (/clarimg6/clarit/images/epa-cinc)
/usr/clarit/v3.4.0d/data/build /usr/clarit/v3.4.0d/data/active
/usr/clarit/v3.4.0d/system/clhtml /clarimg5/clarit/images/epa-cina
/clarimg3/clarit/images/epa-cinb /usr/clarit/home/clarit
/usr/clarit/home/clarit /clarimg3/clarit/images/epa-cinb
/clarimg6/clarit/images/epa-cinc
------------------------------
Date: 18 Dec 2002 20:32:25 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Wierd loop/chdir behavior
Message-Id: <atqm0p$q59$1@mamenchi.zrz.TU-Berlin.DE>
According to Steve Walker <J_Steven_Walker@hotmail.com>:
> Anybody have an idea what is going on here?
[...]
> and convert multiple paths. The weirdness is that the chdir to the
> target directory fails EVERY OTHER pass of the loop. You can
[...]
> #! /usr/bin/perl
> #
> # Wed Dec 18 14:41:48 EST 2002
> #
> # Usage: clconfig
> #
> # This script scans a configuration file to determine key variables
> to use when
> # building new Clarit databases
> #
> # jsw
> #
> $,="\n";
>
> $conf_file = shift @ARGV;
> $conf_file ="clarit.conf" unless defined($conf_file);
> print "opening $conf_file...\n";
>
> exit unless (-e $conf_file);
> open (CONF,"$conf_file");
You should always check the result of open(). A prior test for
existence is no replacement, for various reasons.
> chop ($work_dir = `/usr/bin/pwd`);
> print "Config open at $work_dir";
>
> # Find the paths to be searched and the name of the binding file
> where the
> # database segments will be defined.
>
> while (<CONF>)
> {
> @build = split(/[:\n]/,$') if m/^\s?BuildPath\s?=/ ;
> @active = split(/[:\n]/,$') if m/^\s?ActivePath\s?=/ ;
> @web = split(/[:\n]/,$') if m/^\s?WebPath\s?=/ ;
> @newdata = split(/[:\n]/,$') if m/^\s?NewDataPath\s?=/ ;
It would be interesting to know if these lists contain fully qualified
or relative path names.
[some code snipped]
> # Wierdness starts here
>
> TOP: chdir $work_dir ;
> $ddir=shift @newdata;
> print "New Scan data at $ddir\n";
> if (chdir(glob "$ddir"))
^ ^
Those quotes are unnecessary.
> {
So now you are in the directory glob() made out of $ddir...
> chop($pwd=`/usr/bin/pwd`);
> print "now in $ddir ($pwd)\n" ;
> $dp .= " $pwd" ;
...as the printout probably verifies.
> chdir $ddir;
Here, you are trying again to change your directory to $ddir, this time
without a glob(). If the glob() was needed in the first place, you'll
need it here too. If what it returns is an absolute path, then this
second chdir should succeed (with, possibly, a glob added). If it's a
relative path, it is now interpreted from the changed working directory
and cd() will fail, except under unusual circumstances.
[rest snipped, there are too many when's and if's already]
Anno
------------------------------
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 4284
***************************************