in reply to Re: variables in substition/eval
in thread variables in substition/eval

In a situation like this, my own preference is to use look-arounds to avoid capturing (and substituting back) more than necessary (tested under 5.8.9):

c:\@Work\Perl\monks>perl -wMstrict -le "my @lines = ( 'Once upon a time', 'scrooge & donald', 'went for a long walk', ); ;; my @list = qw(huey dewey louis); ;; foreach my $who (@list) { print qq{who '$who'}; foreach my $ln (@lines) { (my $newln = $ln) =~ s{ scrooge (?= \s+ & \s+ donald) }{$who}xmsg +; print qq{ '$newln'}; } } " who 'huey' 'Once upon a time' 'huey & donald' 'went for a long walk' who 'dewey' 'Once upon a time' 'dewey & donald' 'went for a long walk' who 'louis' 'Once upon a time' 'louis & donald' 'went for a long walk'


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: variables in substition/eval
by kcott (Archbishop) on Aug 19, 2017 at 04:29 UTC

    My intention was to show the use of the 'r' modifier to resolve the problem. Because 'r' may have appeared to replace 'g', I commented on that also.

    Not commenting on other aspects of the code should not be construed as tacit approval. I still don't know what output was expected; I don't know the context of this code; and there are other unknowns, such as how "scrooge" relates to the anything here.

    There are in fact all sorts of issues with the code, ranging from poorly named variables (e.g. @list) to bad grammar (e.g. non-capitalised proper nouns).

    I possibly would have used an easily identifiable placeholder such that the regex required no captures or lookaround assertions. The substitution may simply have been something like:

    ... s/__NEPHEW__/$nephew/r ...

    Update: Did Donald Duck have a relative named Scrooge? Perhaps that explains the "scrooge" connection. Still just guessing. :-)

    — Ken

      Donald does indeed have a relative named Scrooge: his uncle, Scrooge McDuck, a notoriously penny-pinching millionaire! At some point, the Duck family of the McDuck clan apparently changed their surname, but I ran out of interest before I could pin down the precise genealogy.


      Give a man a fish:  <%-{-{-{-<

        "... I ran out of interest before I could pin down the precise genealogy."

        My interest in Disney cartoon characters probably waned before my age reached double digits: so, just a vague memory from childhood.

        Initially, I was trying to work out the relationship between a Dickens' character and Donald Duck. Unsurprisingly, I had little success. :-)

        — Ken