Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

[Perl 6]: Small discoveries V, True / False / FileNotFound

by holli (Monsignor)
on Oct 19, 2017 at 17:54 UTC ( #1201692=perlmeditation: print w/replies, xml ) Need Help??

Omg, I love this. Did you ever have a clear, slick little function that needs to return a boolean, and you also want to communicate an error condition? You basically have the choice of returning two values, reversing the consuming condition (meaning an empty return value be considered true), or using a string reference as an argument to the function.

Witness Perl 6:
sub slick() { if do-stuff { return "SomeValue"; } else { return "Some error message" but False; } } if my $result = slick { process( $result ); } else { log-error( $result ); }


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re: [Perl 6]: Small discoveries V, True / False / FileNotFound
by RonW (Vicar) on Oct 20, 2017 at 00:32 UTC

    Scalar::Util has dualvar which returns a scalar with both a numeric value and a string value.

    In theory, could do:

    # untested sub alsoslick() { if do-stuff { return dualvar 1, "SomeValue"; # or just a non-0 numeric value } else { return dualvar 0, "Some error message"; } } my $result = alsoslick; if 0+$result { process( '' . $result ); # or just $result if non-error result is +numeric } else { log-error( '' . $result ); }

    (Not as clean as Perl6.)

      Perl 6 also has dual vars

      sub slick() { if do-stuff { return IntStr.new: 1, "SomeValue"; } else { return IntStr.new: 0, "Some error message"; } } my $result = slick; if +$result { process( ~$result ); } else { log-error( ~$result ); }

      I would not do this though, as you can pass around Failures just like any data type which but which act as undefined values.

      sub slick() { if do-stuff { return "SomeValue"; } else { fail "Some error message"; } } with slick() # is it defined -> $result { process $result } else -> $error { log-error $error.exception.message }

      Actually I would make it so that log-error accepts Failure objects, so that the .exception.message can be removed.

      proto sub log-error ( $ ) {*} multi sub log-error ( Str $message ) { say $message; # do the logging here } multi sub log-error ( Exception $exception ) { samewith $exception.message } multi sub log-error ( Failure $failure ) { samewith $failure.exception.message }

      You can also change out any method if you mixin a role.

      # this has the same effect as `but False` 'Some error message' but role { method Bool ( --> False ) {} }
        # this has the same effect as `but False` 'Some error message' but role { method Bool ( --> False ) {} }
        This is some serious voodoo. No wonder eval is warned of even more.


        holli

        You can lead your users to water, but alas, you cannot drown them.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1201692]
Front-paged by ww
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2018-07-16 18:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (346 votes). Check out past polls.

    Notices?