Priority Question + Repeat Modifier

Please explain to me this apparently inconsistent behavior:

use strict;
my @a;

print "a" x 2; # this prints: aa
@a = "a" x 2; print @a; # this prints: aa

print ("a") x 2; # this prints: a
@a = ("a") x 2; print @a; # this prints: aa

Shouldn't the latter print one 'a'?

Editing: Okay, so now it makes sense to me: “Binary x” is a repeat operator ... In the context of a list, if the left operand is enclosed in parentheses or is a list formed by qw / STRING /, it repeats the list " perlop

It's as clear as mud to me (binary x - why use the word binary? Is there a notation X?) But anyway: @a = ("a") x 2 # seems to be in the context of the list, since we have an array at the beginning - the array is not a list, but contains a list, so I think we probably have a list context (and not an array context, although they may be synonyms).

Suppose the "left operand" ("a"). (This is either this or @a). perlop does not say what the operand really is, the query perldoc.perl.org gives "No matches were found," and googling gives "In computer programming, the operand is the term used to describe any object that can be manipulated." Like for example an array.

, , , , " ". : ("a") x 2 : ("a")

("a") x 2, ("a") x 2 ("a") x 2. .

: print $a[1], "a" , " " , Perl ("a") x 2 ("a", "a"), @a=("a", "a")

print ("a") x 2 ("a", "a"). , print " " . , : (print ("a")) x 2

- , , @a = =, . . ​​

+3
3

Perl. print ("a") x 2 :

(print ("a")) x 2;

, :

print (("a") x 2);  # prints aa
+3

use warnings; script, ,

print (...) interpreted as function .
Useless use of repeat (x) in void context .

print (("a") x 2); #, : aa

, , , . Perltidy.

Perltidy - Perl script, Perl, . Perl , , , .

Perltidy:

perltidy , ..

, . .

Perltidy HTML- .

Perltidy ; . , tidyview perltidys , . tidyview CPAN.

. use strict use warnings .

+7

, , , , print perl - . :

, , , ; .

Putting brackets makes it act like a function. Consider whether you are running this test case:

x("y") x 2;

sub x {
    my $y = shift;
    print "1: $y\n"; #result 1: y
    my $y = shift;
    print "2: $y\n"; #result 2:
}

When writing in the form of a function call, your results are consistent for the function, which is evaluated by its arguments, and not for calculating the assignment after the following operations.

+4
source

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


All Articles