Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

What does this mean? "return -$!"

by isync (Hermit)
on Nov 02, 2012 at 20:25 UTC ( #1002047=perlquestion: print w/replies, xml ) Need Help??
isync has asked for the wisdom of the Perl Monks concerning the following question:

Found it here: link
Does it wrap a textual error string in a magical way with numeric return/error constants??

Replies are listed 'Best First'.
Re: What does this mean? "return -$!"
by tobyink (Abbot) on Nov 02, 2012 at 20:35 UTC

    The variable $! is a dualvar. Used in string context, it returns test describing the last system error. Used in numeric context it returns a number identifying the last system error.

    Adding a minus in front of it supplies a numeric context. Of course, it means that instead of getting, say, 2 (No such file or directory) you get -2 instead.

    But in the case of Fuse, that module's documentation states that its functions use the convention of returning a negative number to indicate an error condition. Given that convention, returning -$! for system errors is quite a good solution.

    That said, using return values to indicate error conditions is generally frowned upon in programming languages such as Perl which have vaguely usable exception handling.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: What does this mean? "return -$!"
by aitap (Curate) on Nov 02, 2012 at 20:42 UTC
    $! is the last error from syscall/library call (like errno in C). Fuse functions should return standard error codes in case of errors, and they should be less than zero because positive number can mean a file descriptor or comething else. Thus, -$! is returned as it is (minus) error code from the last system or library call which caused the error.
    Sorry if my advice was wrong.
Re: What does this mean? "return -$!"
by isync (Hermit) on Nov 02, 2012 at 20:55 UTC
    Ah, I see, the minus forces it into a numeric context, and negative values is what we want here anyway. (Elegant!)

    And further, that $! is a dualvar, and that there is such a thing as "dualvars", explains to me what I, until today, never fully understood about error handling, which often deals with these two-sided text vs. return-value conditions... Heureka!

      Recent versions of Perl (since 5.8) have shipped with Scalar::Util that allows you to define your own dualvars. (The module is also available on CPAN for earlier versions of Perl.)

      use 5.010; use Scalar::Util qw( dualvar ); my $x = dualvar(7, "Seven"); say "$x"; say $x+0;
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1002047]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2018-05-27 02:08 GMT
Find Nodes?
    Voting Booth?