Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Basically I was wondering if I could use s/ to call a function that would manipulate one of the matches and use that as the replacement string.
This is code that sort of represents what I am trying to explain.
my $foo = 'bar 123 moo cow'; $foo = s/(123)/function($1)/; sub function { my $argument = shift; $argument = "rab"; return $argument; }
The basic idea is that 123 is replaced by what function returns. Obviously the operation it represents could be easily done via s/ but this is just the example instead of what I am actually trying to do with &function

Replies are listed 'Best First'.
Re: using s/ to not only substitute but also modify text
by waswas-fng (Curate) on Jul 26, 2004 at 16:56 UTC
    Here ya go:
    my $foo = 'bar 123 moo cow'; print "$foo\n"; $foo =~ s/(123)/function($1)/e; print "$foo\n"; sub function { my $argument = shift; $argument = "rab"; return $argument; }


    -Waswas
Re: using s/ to not only substitute but also modify text
by hardburn (Abbot) on Jul 26, 2004 at 16:55 UTC

    The /e modifier can be used for that.

    ----
    send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Re: using s/ to not only substitute but also modify text
by gellyfish (Monsignor) on Jul 26, 2004 at 16:56 UTC

    Yes, you want to use the /e modifier to the substitution like:

    $foo =~ s/(123)/function($1)/e;

    /J\

        Ooops, yes fixed now

Re: using s/ to not only substitute but also modify text
by tbone1 (Monsignor) on Jul 26, 2004 at 17:18 UTC
    A word of warning, though. I did this at work and, one day when I was away, there was a production problem (not caused by my code, might I add), and I was called while driving in Washington DC during rush hour. (This is not something that I recommend to anyone.) So, if others without much Perl experience will have to help support it, I recommend comments at the very least.

    --
    tbone1, YAPS (Yet Another Perl Schlub)
    And remember, if he succeeds, so what.
    - Chick McGee

      tobne1, ack. I would hope it is pretty self documenting when you use the paren form sub call. If the person you have looking at your code has more than a passing "huh?" when looking at that, you may want to find someone whom knows perl a little bit better. =)


      -Waswas
Re: using s/ to not only substitute but also modify text
by Anonymous Monk on Nov 22, 2008 at 07:37 UTC
    Hi monks, I tried the suggested code and got an Error stating "Use of uninitialized value $_ in substitution s/// at..." Seems only the default $_ yields no errors, if any other user-defined variable (eg. $testvar) is used, the error shows up. Can anyone tell me why this is? Thanks.
      Sorry, the Error I mentioned came from another test I did after I got the initial 'error' from using the posted code. Basically, when I ran the code, it worked but instead of getting the hard-coded substituted text "rab", I got "function()". Then I edited the code and used an arbitrary variable name like $testvar, this is when I got the Error telling me about the uninitialized value. Thanks in advance for any suggestions anyone can tell me.
        s/.../.../;

        means

        $_ =~ s/.../.../;

        The warning is issued when $_ contains undef.