Why is there a + before the first left parenthesis

2020-04-08 13:10发布

Recently I came across a statement like this

print +( map { $_ + 1 } @$_ ), "\n" for @$array; # <- AoA

I had not seen the + operator used with print like this before. Running the statement, it is not hard to infer what it is doing. But I am having trouble finding documentation on the + operator used this way.

my @tab = (
[1,2,3,4],
[qw(a b c d)],
);

my $test = "(", +( map { qq( >>$_<< ) } @$_ ), ")\n" for @tab;
print $test;
my @test = "(", +( map { qq( >>$_<< ) } @$_ ), ")\n" for @tab;
print @test;
my %test = "(", +( map { qq( >>$_<< ) } @$_ ), ")\n" for @tab;
print %test;

Above produces the warnings: Useless use of a constant ()) in void context for all three tests. Use of uninitialized value for the scalar test and Odd number of elements in hash assignment for both the array and hash tests, It would be nice to be able to store the statements output in a scalar. I know I can store an Identical string in a scalar using a for loop like below.

print "(", +( map { qq( >>$_<< ) } @$_ ), ")\n" for @tab;

my $out = '';
for (@tab) {
    $out .= "(" . ( join '', map { qq( >>$_<< ) } @$_ ) . ")\n";
}   
print $out;

# output ...
( >>1<<  >>2<<  >>3<<  >>4<< )
( >>a<<  >>b<<  >>c<<  >>d<< )
---
( >>1<<  >>2<<  >>3<<  >>4<< )
( >>a<<  >>b<<  >>c<<  >>d<< )

Why can't I store the statement in any variable or test its type. Id like in detail what is actually happening with the print builtin when using the + operator.

EDIT: I belive my original post was kind of confusing, I did want to learn more about the + operator used with print but what I was really after is how to store each iteration of the shorthand for statement into a scalar string - I found a way after some testing ...

use strict;
use warnings;
my @a = ([1,2,3],[qw/a b c/]);
my $one = '';
$one .= "(" . ( join '', map { " >>$_<< " } @$_ ) . ")\n" for @a;
print $one;

# output ...
( >>1<<  >>2<<  >>3<< )
( >>a<<  >>b<<  >>c<< )

标签: perl
2条回答
兄弟一词,经得起流年.
2楼-- · 2020-04-08 13:51

perldoc -f print:

Be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print; put parentheses around all arguments (or interpose a "+", but that doesn't look as good).

In this case, you do not want map to consume the "\n", you must put parentheses around the map { $_ + 1 } @$_. But, the moment you do that you run into the situation described above unless you put a + before the left parenthesis so that +(map { $_ + 1 } @$_) becomes a single argument to print instead of (map { $_ + 1 } @$_) being interpreted as the complete argument list.

If you had

print ( map { $_ + 1 } @$_ ), "\n" for @$array;

the comma following the right parenthesis above would be interpreted as the comma operator. print would just print the numbers, and not the newline:

$ perl -we '$array=[[1,2,3]];print ( map { $_ + 1 } @$_ ), "\n" for @$array;'
print (...) interpreted as function at -e line 1.
Useless use of a constant ("\n") in void context at -e line 1.
234%

The print is evaluated. As a side effect, it prints the numbers. The return value of the print is discarded, and the value of the expression becomes "\n". Since this value is not assigned to anything, you get the warning. Basically, the code ends up looking like:

"\n" for @$array;

except for the side effect of invoking print.

If you want to store the output of the loop in a variable, you can do:

my $string;
$string .= sprintf( "(%s)\n", join('', map {$_ + 1} @$_) ) for @tab;
查看更多
在下西门庆
3楼-- · 2020-04-08 13:54

I think you've asked two questions here, so I'll answer both of them as well as I can... Firstly, you're referring to the unary "+" operator, from perlop

Unary "+" has no effect whatsoever, even on strings. It is useful syntactically for separating a function name from a parenthesized expression that would otherwise be interpreted as the complete list of function arguments.

To expand on what exactly is going on in the print statement, I found the explanation on perlmaven to be pretty good.

The documentation explains that the + separates the print function from the parentheses ( and tells perl that these are not the parentheses wrapping the parameters of the print function.

That might satisfy you, but if you are further intersted you can use the B::Deparse module to ask perl how does it understand this code-snippet:

print +(stat $filename)[7];

We save that content in the plus.pl file and run perl -MO=Deparse plus.pl. The result is:

print((stat $filename)[7]);

Hope this is helpful!

Edit

Explaining in detail why you can't use the same command to print that sequence and assign it to a scalar is maybe outside of the scope of this question, but I'll attempt to briefly explain... with the note that if I'm wrong about something, I'd love a correction, I've come to understand how this works through writing code using this, not from reading the perl docs on its inner workings :) With the print statement

print "(", +( map { qq( >>$_<< ) } @$_ ), ")\n" for @tab;

perl is interpreting it as

(print "(", +( map { qq( >>$_<< ) } @$_ ), ")\n") for @tab;

That is, it's grouping all of the stuff you want to print, and performing it for each element in @tab. However, with your scalar assignment

my $test = "(", +( map { qq( >>$_<< ) } @$_ ), ")\n" for @tab;

perl is performing the for @tab code on ")\n", thus your Useless use of a constant ()) in void context warning. To do what you want, you need to make it explicit what you want for'd

my $test; ( $test .= "(" . join ('', ( map { qq( >>$_<< ) } @$_)) . ")\n") for @tab; print $test;

Should get you what you're looking for. Note the parenthesis around the entire assignment, and the omission of the unary "+" before the map, which is no longer necessary.

查看更多
登录 后发表回答