in reply to Re: Regex to replace $vars with subroutine
in thread Regex to replace $vars with subroutine

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

Replies are listed 'Best First'.
Re^3: Regex to replace $vars with subroutine
by kyle (Abbot) on Jun 07, 2008 at 01:19 UTC

    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.