Why does not return $ foo if / pattern /; `return $ foo, although the matching pattern is $ foo?

I use the tool that was published in the Science article, but it causes me a lot of problems because I am not familiar with Perl.

The code contains:

return $equa if /\@BOUNDARY/;

I believe that code should return $equaif it contains text @BOUNDARY, but it does not. Does the provided code have an error?

I am going to change it to:

if ($equa =~ /\@BOUNDARY/) {
    return $equa;
}

Does this perform the same function?


For reference, the entire function in the source code:

sub correctBoundaryReac {
    my $equa = shift;
    print $equa;
    return $equa if /\@BOUNDARY/;
    my( $left, $arrow, $right ) = ( '', '', '' );
    if( $equa =~ /^(<--|<==>|-->) (.+)/ ){
        $arrow = $1;
        $right = $2;
        $left = $right;
        $left =~ s/\@\S+/\@BOUNDARY/g;
    }
    elsif( $equa =~ /(.+) (<--|<==>|-->)$/ ){
        $left  = $1;
        $arrow = $2;
        $right = $left;
        $right =~ s/\@\S+/\@BOUNDARY/g;
    }
    else{
        die "Don't know how to fix bounadry reaction: $equa\n";
    }
    return "$left $arrow $right";
}
+4
source share
3 answers

, if (/$pattern/) if ($_ =~ /$pattern/). . perlvar. : – $_ , ?

Perl $_ . , $_ -. @_ , .

- $_ , -

use feature 'say';

sub show_it { say "I see: $_" }

for ('a'..'c') {
    show_it();    # prints with a through c
}

, , , sub '@BOUNDARY' $_ – , sub. , , . , , , ($_ ). , , .

, , , ,

return $equa if $equa =~ /\@BOUNDARY/;

, sub , . - '@BOUNDARY'.

+4

Perl-ism, , .

Perl " ", $_. , , , $_ . ,

foo() if /bar/;

if ($_ =~ /bar/) {
    foo();
}

, , , - , .

, , , $_ .

+1
return $equa if /\@BOUNDARY/;

return $equa if $_ =~ /\@BOUNDARY/;

if if

if (/\@BOUNDARY/) {
    return $equa;
}

if ($_ =~ /\@BOUNDARY/) {
    return $equa;
}

, . ,

return $equa if $equa =~ /\@BOUNDARY/;

should have been used, in which case it’s great to use

if ($equa =~ /\@BOUNDARY/) {
    return $equa;
}
+1
source

Source: https://habr.com/ru/post/1656275/


All Articles