What this operator does :();

I saw perl statements that look like this:

() unless $some_var;

What does it mean to achieve?

+3
source share
6 answers

Several things happen here, but the most significant is the fact that such a line usually appears as the last line of an executable block [1]. In this context, the value of the block is the value of the last statement executed [2]. Now the expression containing contains the unlessvalue of the left side, if the right side is true and the value of the right side otherwise, so this is the way to say: "the value of the block $some_varif it $some_varturns out to be true, ()otherwise".

() - , , $some_var (, $some_var - undefined.)

or,

$some_var or ();

, :

use strict;
use warnings;

my @x = do { () unless 0 };
my @y = do { () unless 5 };

print "\@x: <@x>\n\@y: <@y>\n";

@x - , 0 , @y , 5, 5 .

, , () unless $some_var; -. once, $some_var .

[1]: , eval do () map grep, , .

[2]: " " - ; . Perl , , Perl .

+9

:

GetOptions(
    "-interactive",
    "-request_file=s",
) or usage();

usage unless ($opt_request_file);
() unless ($opt_interactive);

, '();' - , $opt_interactive ... perl "" $var " ".

, !

+3

, , $some_var true, , .

+1

nop. ( ) do(), return, .

+1

, , .

unless , , (.. ).

. : http://perldoc.perl.org/perlsyn.html

() , , $some_var (, $some_var - undefined).

0

It returns an empty list if $ some_var is false.

If I write @s = () unless $some_var;, I emptied @sif it $some_varis false.

This is the only way I can think.

0
source

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


All Articles