Re: stat() bug
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 27 Oct 2005 09:47:31 -0700
ssojoodi@xxxxxxxxx wrote:
> While debugging a Perl script, I came across a statement like this:
>
> my @myarray = ("str1", "str2", "str3", "str4");
> my $foo = (stat(@myarray))[2];
> print $foo;
>
> [result]: str3
>
> I know that this is not the proper usage of the stat function, but I'm
> wondering why this script works! I could not find documentation on
> this within the perlfunc manual, so I figured this may be a bug.
>
> Any ideas?
Running the above code through -MO=Deparse shows us:
my(@myarray) = ('str1', 'str2', 'str3', 'str4');
my $foo = (stat @myarray)[2];
print $foo;
>>From that, you can see that '@myarray' is not being passed as an
argument to stat, but rather that array is being expanded so that the
expression is really saying:
(stat 'str1', 'str2', 'str3', 'str4')[2]
stat is defined as a unary operator, so `stat 'str1'` binds more
tightly than does the comma operator. Therefore, the list that's
created above is the list containing: (return_val_of_stat('str1'),
'str2', 'str3', 'str4')
Since the entire operation is being done in scalar context (that is,
you are assigning to the scalar variable $foo, the stat() call is also
done in scalar context, so it simply returns a true or false value, as
defined by the relevant perldoc.
You are then taking the third element of that list, which is 'str3'.
(Please note that I am mildly confused as to why @myarray isn't forced
into scalar context by the stat call, rather than expanded into a list,
but this does explain the results that are created)
Paul Lalli
.
- Prev by Date: Re: How to run a perl script after an interval of 10 seconds
- Next by Date: Re: stat() bug
- Previous by thread: How to run a perl script after an interval of 10 seconds
- Next by thread: Re: stat() bug
- Index(es):
Relevant Pages
|