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

Dear Monks,

How should I do the following:
use File::Spec ; $a = "bla::blabla" ; $b =~ s/\:\:/File::Spec->rootdir()/ ; print "$b\n" ;
which should give: bla/blabla , but instead I get:
blaTestFile::Spec->rootdir()blabla

Thanks
Luca

Replies are listed 'Best First'.
Re: Function call inside regexp
by mickeyn (Priest) on Mar 07, 2006 at 10:21 UTC
    Hi Luca,

    use the /e modifier to evaluate the right-hand side of the RE.

    $b =~ s/\:\:/File::Spec->rootdir()/e ;

    Enjoy,
    Mickey

      The /e is the best option, I think.
      But just to mention it, there is another way to do it:
      $b =~ s/\:\:/${\(File::Spec->rootdir())}/;
      The trick is to use de-reference a refence to a value. You can put almost any expresion inside the ${\( )} and it will be well evaluated.
Re: Function call inside regexp
by davorg (Chancellor) on Mar 07, 2006 at 11:20 UTC

    mickeyn has already given you the answer (use /e) but it's probably worth pointing out how you could have found that out for yourself.

    s/// is an operator and therefore it is documented in perldoc perlop. In particular, it in the section about Rexexp Quote-Like Operators operators. This includes full documentation of the options and you'll see this:

    e - Evaluate the right side as an expression.

    Also there's an example (using sprintf) which is very similar to what you want.

    No-one minds helping you out with questions like this (it is, after all what Perl Monks is here for) but you'll almost certainly get the answer much quicker if you learn to look through the documentation before asking here.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg