Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: look-behind regex

by I0 (Priest)
on Jul 24, 2002 at 23:49 UTC ( #185068=note: print w/replies, xml ) Need Help??


in reply to look-behind regex

s/(toto)|foo/${[$1,'bar']}[!$1]/

Replies are listed 'Best First'.
Re: Re: look-behind regex
by Anonymous Monk on Jul 25, 2002 at 02:21 UTC
    Don't be afraid to use the e modifier: s/(toto)|foo/defined $1 ? $1 : 'bar'/e
    Cheers,
    -Anomo
      How about s/(toto)|foo/$1||'bar'/e
        TMTOWTDI...

        But the reason I used defined() was that I wanted to give a general solution. Someone might copy the code and change toto to something else that matches something that is false without reflecting on how it works. Or you might change it yourself later. Then you perhaps abstract the pattern, and suddenly you have a hard-to-trace bug. I always try to code so that I don't bite myself later on when I patch my code.

        Cheers,
        -Anomo

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://185068]
help
Chatterbox?
[Corion]: ... and also a visit to my godson+family of course :)
Corion orders some gifts online :)
[ambrus]: We're spending Christmas and the week after at my brother's place. First time ever, because the last four years they could came home instead.
[ambrus]: robby_dobby: wait what? Glasgow? isn't that one of the most boring towns ever?
[marto]: ambrus: no :)

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2017-12-15 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (431 votes). Check out past polls.

    Notices?