in reply to Regex Substitution Evaluations

Evaling strings is fraught with numerous pitfalls, and I would avoid that approach. Most modifiers can also be expressed directly in the regular expression. E.g.:
m/foo/i; # -> m/(?i)foo/; m/bar/m; # -> m/(?m)bar/; ...
See perldoc perlre for more details. So either add them yourself, or require that the caller do so.

Replies are listed 'Best First'.
Re^2: Regex Substitution Evaluations
by mobiusinversion (Beadle) on Jul 05, 2008 at 20:34 UTC
    Please refer to my original post.

    As you can see, I would absolutely like to avoid using eval, and I have consulted perldoc's perlre perlop perlretut and perlreref entries several times.

    How might you suggest going about substitutions?

      I think pc88mxer answered your question for the most part.

      $my_name = replace("august ferdinand mobius","\b(\w)","\U$1","igs"); # Becomes something more like... $my_name = replace("august ferdinand mobius", qr/(?igs)\b(\w)/, '\U$1' +);

      Update: You should consider that your solution will not work for a variety of names, like mccleod, vomdorp, decaro, etc. For proper names, a dictionary (with the right library under it, like proper names) is a better way to go, and there is no perfect solution since the same spellings are sometimes capitalized differently. :(

        I gave a few examples, more-or-less off the top of my head, in an article I wrote a number of years ago, when I was talking about the inadvisability of assumptions when it came to names:

        John deJongh
        Jan M. van de Geijn
        Kathleen O'Hara-Mears
        Siu Tim Au Yeung
        Nang-Soa-Anee Bongoj Niratpattanasai
        Mjölby J. de Waærn
        

        Also, assuming things about the order of, say, Chinese or Indian names is likely to run into problems - even within a given city, in the latter case.

        
        -- 
        Human history becomes more and more a race between education and catastrophe. -- HG Wells
        
        The names problem is interesting, although I'm sort of kicking myself for giving it as an example. The motivation for this post was and is to develop a general substitution framework for parametrized regex creation...