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

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

  • Comment on Re^2: Perl Module Object - Return fail/success with a fault string
  • Download Code

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

    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?