开发者

How can I use the match variable $1 following a substitution in Perl?

开发者 https://www.devze.com 2022-12-26 23:51 出处:网络
What is the cleanest way of picking up a match variable from a substitution in Perl I sometimes find myself writing

What is the cleanest way of picking up a match variable from a substitution in Perl I sometimes find myself writing

    s/(something)// ;
    my $x = $1 ;

then I realise that if the s/ / / fails $1 might b开发者_如何学JAVAe carrying over a value from a previous match. So I try

    my  $x = 'defaultvalue' ;
    if ( s/(something)// )
    {
     $x = $1 ;
    }

Is this the cleanest way of doing it?


TMTOWTDI.

The idiomatic Perl usually seen might be somthing like:

my $x='defaultvalue';

$x=$1 if (s/(something)//) ;


As others have pointed out, TIMTOWTDI.

I'd personnally wrap it up as a single expression, so as not to distract too much from the point:

my $x = s/(something)// ? $1 : 'defaultvalue';


@mobrule points out in comments that you can use

s{(...)}{} && ($x=$1)

I would recommend

s{(...)}{} and $x = $1;

I used s{}{} because I hate it when Markdown marks // as comments in Perl code.

and has lower precedence than && which makes it possible to remove the parentheses around the assignment.

From perldoc perlop:

As more readable alternatives to && and || when used for control flow, Perl provides the and and or operators (see below). The short-circuit behavior is identical. The precedence of "and" and "or" is much lower, however, so that you can safely use them after a list operator without the need for parentheses:


This is highly subjective, and the best answer will depend greatly on how well it matches your coding style, but I'll throw my hat into the ring anyway.

My favorite for assignments with defaults is the tertiary operator:

my $x = ($data =~ s/(foo)//) ? $1 : 'defaultvalue';

Although the new (well, not that new, 5.10 introduced it) "defined or" operator can reduce the need for the tertiary statement in many cases. I generally use it as such:

my $var = defined($candidate) ? $candidate : 'default';

But now that can be simplified to:

my $var = $candidate // 'default';

or

my $var = 'default';
$var //= $candidate;
0

精彩评论

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

关注公众号