jhyland87 has asked for the wisdom of the Perl Monks concerning the following question:

So I am creating my first perl module, (atleast my first advanced one thats more than a few lines of code), and hope to submit it to CPAN, so id like to get it working perfectly

Right now, im trying to get the subroutines to return both a standard return code (0 or non 0), with a "fault code"

I guess heres an example of the current code (not really, but this is basically an example of what I have, in the relative aspect)

File: ./Module/Test.pm
package Test.pm; sub new { my ($self, $value) = (@_); my @return %return = ("code" => 1, "fault" => "You didnt even send a value to + me!\n") if (!$value); %return = ("code" => 0, "value" => "Cool, you sent me $value\n") i +f ($value); }
Test File: ./test.pl
use Module::Test; $test = Module::Test->new(); die($test->{'fault'}) if (!$test); # Didn't die print $test->{'value'} if ($test);

Kinda get what I mean? Return both an exit code, as well as a comment that I can reference? THE ABOVE CODE IS NOT TESTED!! In fact, I doubt it works, so don't diagnose that, im just trying to get the idea of my goal to you through a code example...

Thanks in advance! (and yes, I googled it)

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

    What you want is certainly possible, but your code in Module::Test is not how you construct objects (or "objects"). Please have a look at bless or one of the object tutorials. If your objects are merely collections of data, just return an unblessed hash reference:

    sub new { ... return \%return };
      ok, so bless the hash and then return it, got it. How would I structure it though so that perl sees the exit code within the hash?

        What do you mean by that? Do you ask how to set a key in your hash?

        I think it would help me if you show what you got and show what you want.

        I would recommend not returning an error code or value. If you don't get the data you need, a good option is to carp/warn and return undef, or die/croak ( depending on whatever is appropriate ).
Re: Perl Module Object - Return fail/success with a fault string
by BrowserUk (Patriarch) on Jan 03, 2012 at 18:15 UTC

    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?

      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?