Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

It doesn't mean what you think it means

by Fletch (Chancellor)
on Apr 11, 2002 at 13:14 UTC ( #158306=note: print w/replies, xml ) Need Help??

in reply to Compiling regular expressions to perform substitution

qr// only compiles regular expressions, not substitution operators. What you've done is compiled a regular expression for matching `s/foo/bar/g'. You can use compiled regexen on the left hand side of an s/// expression, but you can't use one to capture the whole expression.

Now if what you're trying to do is capture an operation or three to be performed on a string that you can select programattically, you possibly could use a hash of coderef like this:

my %mungers = ( foo => sub { $_[0] =~ s/foo/bar/g }, fru => sub { $_[0] =~ s/(fru.*)/uc($1)/e }, ); if( $phase_of_moon > WAXING_GIBBOUS ) { $mungers->{foo}->( $string ); } else { $mungers->{fru}->( $string ); }

Update: Feh, as was pointed out that first -> inside the if/else blocks is extraneous. That's what I get for posting while trying to eat breakfast.

Replies are listed 'Best First'.
Re: It doesn't mean what you think it means
by Stegalex (Chaplain) on Apr 11, 2002 at 15:05 UTC
    Slight modification needed to make this compile. Here it is:
    my %mungers = ( foo => sub { $_[0] =~ s/foo/bar/g }, fru => sub { $_[0] =~ s/(fru.*)/uc($1)/e }, ); if( $phase_of_moon > WAXING_GIBBOUS ) { $mungers{foo}->( $string ); # this line changed } else { $mungers{fru}->( $string ); # this line changed }
    Again, thanks for everyone's help. I like chicken.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://158306]
[vrk]: Hope he gets better soon.
[Discipulus]: hdb sorry i profit; is known that a prime number is never a figurate one nor the sumattion of two adjacent figurates of the same order? like 3th triangular+3th tetrahedric
[Discipulus]: if known where i can read about the matter?
[choroba]: he's not really sick, we just got nobody to look after him today
[Discipulus]: create an account for him; we are funny baby sitters ;=) come here child, want to listen the typeglob tale?
[vrk]: choroba Oh, that's good.
[vrk]: Discipulus I don't know much about prime numbers, but wouldn't that kind of an answer be in a graduate-level number theory book?
[Discipulus]: my 8 yo daughter has two weeks of sleep disturbs.. we are gonna be crazy..

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2017-04-27 07:59 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (501 votes). Check out past polls.