[Melbourne-pm] Simple(?) question involving backticks and die

Stephen Steneker stephen at sydney.pm.org
Wed Jan 4 21:42:10 PST 2006

>   my @list = `/bin/ls /home` || die ("Couldn't list files");
>   die ("Couldn't list files") unless my @list = `/bin/ls /home`;
> they to be equivalent to me.

Hi Benji,

In your first example, the precedence of the binary or ('||') is  
higher than the
assignment so coerces the backtick results into scalar context for  

What you end up with is the equivalent of:
     my @list = (`/bin/ls /home` || die ("Couldn't list files"));

... so you really want to be using the lower precedence "or" :

     my @list = `/bin/ls /home` or die ("Couldn't list files");

Although for fun you could always make the brackets explicit:
    (my @list = `/bin/ls /home`) || die ("Couldn't list files");


* http://perldoc.perl.org/perlop.html#Operator-Precedence-and- 

More information about the Melbourne-pm mailing list