in reply to Non-destructive string substitution

This is one way: map { s/this/that/; $_ } do { $str }; You can substitute [$str]->[0] for do { $str } if you'd like.
_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: Non-destructive string substitution
by kvale (Monsignor) on Apr 20, 2004 at 18:24 UTC
    This doesn't work either:
    my $foo = "foo"; print map {s/foo/bar/g; $_} do{$foo}; print "\n$foo\n";
    prints
    bar bar
    This, however, does:
    my $foo = "foo"; print map {s/foo/bar/g; $_} @{[$foo]}; print "\n$foo\n";

    -Mark

      Hmm. The debugger runs them differently. Oh well. Regardless, if you don't mind stringification, you can use "$foo" as the argument to map().
      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;