develooper Front page | perl.perl6.language | Postings from August 2006

could 'given' blocks have a return value?

Thread Next
From:
Mark Stosberg
Date:
August 29, 2006 20:23
Subject:
could 'given' blocks have a return value?
Message ID:
ed309t$njc$1@sea.gmane.org

Sometimes I use 'given' blocks to set a value. To save repeating myself
on the right hand side of the given block, I found I kept want to do this:

my $foo = given { }

...and have whatever value that was returned from when {} or default {}
populate $foo.

It turns out pugs already allow this, through the trick of wrapping the
given block in an anonymoose sub...which is then immediately executed:

my $rm = sub { given $rm_param {
        when Code  { $rm_param(self)   }
        when Hash  { %rm_param<run_mode> }
        default    { self.query.param($rm_param) }
    }}();

Not only do you get implicit matching on the left side, you get implicit
return values on the right!

I'd just like to be able to clean that up a little to:

my $rm = given $rm_param {
        	when Code  { $rm_param(self)   }
        	when Hash  { %rm_param<run_mode> }
        	default    { self.query.param($rm_param) }
    };

   Mark


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About