in reply to Regexp and object's methods.

Many thanks, brothers, for your suggestion.
I'm sorry for my not clear question. I'd like to define it more exactly.
I have a package with defined constants:
package C; my $consts = { CONST1 => 1, CONST2 => 2, CONST3 => 3, CONST4 => 4, . . . }; use overload '``' => sub { return $const_values{shift} } ; # All constants can be accessed as by using AUTOLOAD sub AUTOLOAD { no strict 'refs', 'subs'; if ($AUTOLOAD =~ /.*::([A-Z]\w+)$/) { my $const_name = $1; *{$AUTOLOAD} = sub {return $const_values{$const_name}}; return $const_values{$const_name}; } return undef; } 1;
In may script I have some choises:
if($status == C->CONST1) { print "1" } elsif($status =~ /(2|3)/ { print "2 or 3" } else { print 'Something' }
I'd like to use my constants intead of numbers:
if($status == C->CONST1) { print "1" } elsif($status =~ /(C->CONST2|C->CONST3)/ { print "2 or 3" } else { print 'Something' }
but it doesn't work. Of course, I know that I can write like this:
$status == C->CONST3 or $status == C->CONST3
But I wouldn't like to do it.
Any suggestion will be helpful.
      
--------------------------------
SV* sv_bless(SV* sv, HV* stash);

Replies are listed 'Best First'.
Re: Re: Regexp and object's methods.
by Tomte (Priest) on May 02, 2003 at 12:47 UTC

    As far as I read perlre, (?{}) is the only way to get code directly into a regexp.

    so your wanted code must look like:

    if ($status == C->CONST1) { print "1" } elsif ($status =~ /(?{ C->CONST2 })|(?{ C->CONST3 })/) { print "2 or 3" ; } else { print 'Something'; }

    I don't see, why you don't want to use some of the more readable solutions suggested by the other monks, espacially a sub that assembles the regexp for you seems a good way to me, though...
    maybe that I really don't understand what you want to do...

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.