in reply to Regex to replace $vars with subroutine

All you're missing is the /e modifier, see perlre.

$page =~ s/\$galleryname/galleryname()/ge

Replies are listed 'Best First'.
Re^2: Regex to replace $vars with subroutine
by Anonymous Monk on Jun 06, 2008 at 23:06 UTC
    Hi.
    Can you see why this isn't working as expected?
    use warnings; use strict; my $string = "this is a test \$placeholder"; $string =~ s/\$placeholder/test_sub()/e; print $string; sub test_sub { print "hello there"; }
    # prints "hello therethis is a test1

    I don't want the subroutine to print anything, just do everything it needs to do and print out all output back into the $string

      Change print to return, and it works.

      use warnings; use strict; my $string = "this is a test \$placeholder"; $string =~ s/\$placeholder/test_sub()/e; print $string; sub test_sub { return "hello there"; }

      If you really need to capture the output of what a sub prints, that's a whole other problem. I was doing something like that in Use of uninitialized value in open second time but not first., and there's more discussion with it about other ways to do it.