[ic] Usertag return value

Peter peter at pajamian.dhs.org
Thu Feb 12 08:59:04 UTC 2009


On 02/12/2009 12:00 AM, jimbo wrote:
> Gert van der Spoel wrote:
> 
>>> $return= $image->Write(filename=>'/file/location/'.$name.'_image.jpg')
>>> or return 0;
>>> $Tag->log("1: return=$return error=$!");
>>> $return= chmod(0644,"/file/location/".$name."_image.jpg") or return 0;
>>> $Tag->log("2: return=$return error=$!");
> 
> Why not write it like this:
> 
> return 0
>    if ! $image->Write(filename=>'/file/location/'.$name.'_image.jpg');
> 
> return 0
>    if ! chmod(0644,"/file/location/".$name."_image.jpg");
> 
> It seems to me you're trying to assign '$return' the value of 'return 0' 
> in the case of failure. But 'return' can't be assigned to an lvalue, 
> AFAIK. Even if it could, as soon as 'return 0' is called '$return' would 
> go out of scope and you wouldn't be able to access it anyway.

Perl is designed to be able to work that way and it is in fact
recommended by the perl style guide
<http://perldoc.perl.org/perlstyle.html>:

Just because you CAN do something a particular way doesn't mean that you
SHOULD do it that way. Perl is designed to give you several ways to do
anything, so consider picking the most readable one. For instance

    open(FOO,$foo) || die "Can't open $foo: $!";

is better than

    die "Can't open $foo: $!" unless open(FOO,$foo);

because the second way hides the main point of the statement in a
modifier. On the other hand

    print "Starting analysis\n" if $verbose;

is better than

    $verbose && print "Starting analysis\n";

because the main point isn't whether the user typed -v or not.



Peter




More information about the interchange-users mailing list