in reply to Perl Module Object - Return fail/success with a fault string

I think this is what you are looking for:

use Scalar::Util qw[ dualvar ];; sub alwaysFails { return dualvar -123, 'Some text to explain the error'; } $rv = alwaysFails();; print "Sub failed with error: $rv" if $rv != 0;; Sub failed with error: Some text to explain the error

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^2: Perl Module Object - Return fail/success with a fault string
by jhyland87 (Sexton) on Jan 03, 2012 at 19:39 UTC

    Kinda? but not really, the below doesn't seem to work

    print "Sub failed with error: $rv" if (!$rv);

    Meaning that it doesn't actually return a fail code, just a value of != 0

      The problem is that ! does not provide a numeric context.

      use Scalar::Util qw[ dualvar ];; sub alwaysFails { return dualvar 0, 'Some text to explain the error'; +};; $rv = alwaysFails();; print "Sub failed with error: $rv" if !$rv;;

      If you want 0 to stand for failed, you'd need to use a test that does provide a numeric context. Eg.

      print "Sub failed with error: $rv" if ! 0+$rv;; Sub failed with error: Some text to explain the error print "Sub failed with error: $rv" if ~$rv;; Sub failed with error: Some text to explain the error print "Sub failed with error: $rv" if $rv == 0;; Sub failed with error: Some text to explain the error

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?