Of course, with the block do {}:
use strict;
use warnings;
sub fetch { 'please find me' }
sub job { print @_, "\n" }
job( do { $_ = fetch(); s/find/replace/g; $_ } );
The reason is that in Perl you cannot do fetch() =~ s/find/replace/;:
Can't modify non-lvalue subroutine call in substitution (s///) at ...
Perl 5.14 will present a flag /r(which causes the regular expression to return a string with replacements, rather than the number of substitutions) and reduce the above:
job( do { $_ = fetch(); s/find/replace/gr; } );
edited by FM: can be shortened above:
job( map { s/find/replace/g; $_ } fetch() );
And as soon as Perl 5.14 is released, it can be shortened to:
job( map { s/find/replace/gr } fetch() );
source
share