procmail
[Top] [All Lists]

Offtopic: Perl mystery resolved (was Re: quotas and procmail.. )

1999-07-02 01:15:34
On Thu, 01 Jul 1999 13:32:53 -0500, Philip Guenther 
<guenther(_at_)gac(_dot_)edu>
wrote:
era eriksson <era(_at_)iki(_dot_)fi> writes:
Gack. Why does perl's stat just return a single-element array
containing 1? (And why did I just spend half an hour trying to
Did you use it in an array context? To directly subscript a
function's return value you have to wrap it in a list:
     (stat($file))[7]

Here's what I tried:

 $ perl -e '@stat=stat($ARGV[0]); print "$stat[7]\n"' /etc/motd
 313

Funny thing is, now it works :-/  (Checking back in bash' history ...)

Ah, now I see what I did wrong: Too much error checking! (Which is the
reason why I wasn't just doing print +((stat($ARGV[0]))[7]), "\n", I
wanted to see if there was an error before printing):

 $ perl -e '@stat=stat($ARGV[0]) || die "$!\n"; print "$stat[7]\n"' /etc/motd

(Yes, just a newline. And actually with "or" instead of "||" it works
fine. So it was just a plain old precedence problem, actually.)

Here's an improvement which does the right thing with error checking,
without a temporary variable:

 $ perl -e 'print ((stat($ARGV[0]))[7] or die "$!\n"), "\n"' /etc/motd
 313

... although it's definitely somewhat obscure.

/* era */

-- 
.obBotBait: It shouldn't even matter whether     <http://www.iki.fi/era/>
I am a resident of the state of Washington. <http://members.xoom.com/procmail/>
 * Sign the European spam petition! <http://www.politik-digital.de/spam/en/> *

<Prev in Thread] Current Thread [Next in Thread>