[Pdx-pm] New DA question - sub return'd values
Roderick A. Anderson
raanders at acm.org
Thu Jan 8 12:12:42 CST 2004
On Thu, 8 Jan 2004, Ovid wrote:
Got all the above. And basically understood it already.
> > I ask because I'm having some issues using the values in a (faked)
> > switch
> > construct.
> >
> > $_ = typeofcust($username, $password);
> >
> > CASE: {
> >
> > /1/ and do { ... ;
> > last CASE; };
> > /2/ and do { ... ;
> > last CASE; };
> > /3/ and do { ... ;
> > last CASE; }
> > blow_chunks;
> >
> > }
> >
> > If typeofcust() returns a value from 1, 2 or 3 is $_ treated as a
> > string
> > or number by the match operation?
> In this case, because you're using a regular expression, the $_ is
^^^^^^^^^^^^^^^^^^
Damn I should have known/remembered this.
> automatically treated as a string (regexes do not operate on numbers).
> I might, however, add a "local" statement to the assignment:
>
> local $_ = typeofcust($username, $password);
This is OK inside of the main context?
> If you don't do that and any other place in your code is using $_, you
> just altered their value. That can be a nasty bug to track down.
>
> Also, check out Switch.pm
> (http://search.cpan.org/~dconway/Switch-2.09/Switch.pm). It's more
> robust:
Geez. I never think of CPAN when I've seen good code ie. from an animal
book author. But I guess I can accept code from Damian.
Well this should clean up my act a bit. Thanks
> use Switch;
>
> switch ($val) {
> case 1 { print "number 1" }
> case "a" { print "string a" }
> case [1..10,42] { print "number in list" }
> case (@array) { print "number in list" }
> case /\w+/ { print "pattern" }
> case qr/\w+/ { print "pattern" }
> case (%hash) { print "entry in hash" }
> case (\%hash) { print "entry in hash" }
> case (\&sub) { print "arg to subroutine" }
> else { print "previous case not true" }
> }
Rod
--
"Open Source Software - You usually get more than you pay for..."
"Build A Brighter Lamp :: Linux Apache {middleware} PostgreSQL"
More information about the Pdx-pm-list
mailing list