开发者

Find and replace without storing variable

Is there a way to 开发者_如何学Pythondo this in one line?

my $b = &fetch();
$b =~ s/find/replace/g;
&job($b)


Sure, with a do {} block:

use strict;
use warnings;

sub fetch { 'please find me' }
sub job   { print @_, "\n"   }

job( do { $_ = fetch(); s/find/replace/g; $_ } );

The reason being 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 introduce the /r flag (which makes the regex return the string with substitutions rather than the number of substitutions) and will reduce the above to:

job( do { $_ = fetch(); s/find/replace/gr; } );

edited thanks to FM: can shorten the above to:

job( map { s/find/replace/g; $_ } fetch() );

And once Perl 5.14 will be out it can be shortened to:

job( map { s/find/replace/gr } fetch() );


If you are asking whether you have to go through a scalar variable to do the replace, the answer is yes. This is because the result of the substitution has to be stored somewhere. Also, the substitution does not return the variable used, it returns the number of substitutions made. Therefore, you can't inline it (i.e., &job($b =~ s/find/replace/g) ) in the function call.


for (fetch() . '') {   # append with '' to make sure it is a non-constant value
    s/find/replace/g;
    job($_)
}

or use apply from one of the List modules:

use List::Gen qw/apply/;

job( apply {s/find/replace/g} fetch() );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜