Why does this Perl line contain only a variable in itself?

I like perl, the more I get into it, but I had a question about the line that I saw in the subroutine in the module that I am viewing.

my $var = 1;
....
....
....
....
$var;

What throws me, I just see that $varon its own on the line. Is this just a roundabout way to return 1?

Many thanks!

Jane

+3
source share
2 answers

In perl, the value of a block is the value of the last expression in a block. This is just a shorthand for return $var.

EDIT: Purists indicate that these blocks do not return values ​​at all (e.g. in Scala, for example), so you cannot write:

my $x = if (cond) { 7 } else { 8 };  # wrong!

, eval do FILE - . :

sub f {
    my $cond = shift;
    if ($cond) { 7 } else { 8 }  # successfully returns 7 or 8 from f()
}

if/else, , , , .

+6

perldoc -f return:

return , eval FILE .

+6

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


All Articles