Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks.

I was wondering what the '+' sign was doing in this post by CountZero.

I'm using ActiveState 5.16.3 and the code works if I comment out the import of the Modern::Perl module but it doesn't unless I include the line: use feature qw /say/;

Thanks.

Replies are listed 'Best First'.
Re: Confused by '+' syntax in code sample.
by space_monk (Chaplain) on Jun 26, 2013 at 05:56 UTC
    It is to prevent say interpreting the parenthesised expression as its function arguments, at least not before only using the first element of the array returned from split instead of the entire array.

    It doesn't really "do" anything except assist how Perl interprets the expression.

    # say would think that its arguments are the whole statement in bracke +ts say +(split ':')[0] while (<DATA>); #..so we use the + to make it clear that the contents of the brackets +are not function arguments # but part of an expression say +(split ':')[0] while (<DATA>);
    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)
Re: Confused by '+' syntax in code sample.
by davido (Cardinal) on Jun 26, 2013 at 05:56 UTC

    It's there for syntax disambiguation. The + starts an expression, which gives the compiler a clue to not treat the parens that follow as if they were part of the syntax of a function call parameter list for say.


    Dave

Re: Confused by '+' syntax in code sample.
by muba (Priest) on Jun 26, 2013 at 09:05 UTC

    The examples in perlfunc demonstrate this syntax quite neatly:

    Any function (...) may be used either with or without parentheses around its arguments. (...) If you use parentheses, the simple but occasionally surprising rule is this: It looks like a function, therefore it is a function, and precedence doesn't matter. Otherwise it's a list operator or unary operator, and precedence does matter. Whitespace between the function and left parenthesis doesn't count, so sometimes you need to be careful:

    print 1+2+4; # Prints 7. print(1+2) + 4; # Prints 3. print (1+2)+4; # Also prints 3! print +(1+2)+4; # Prints 7. print ((1+2)+4); # Prints 7.
Re: Confused by '+' syntax in code sample.
by kcott (Archbishop) on Jun 26, 2013 at 06:26 UTC

    It would have been useful if you'd posted the original code. Here it is:

    use Modern::Perl; say +(split ':')[0] while (<DATA>);

    say (and the same goes for print which has fuller documentation) can take a FILEHANDLE as its first argument.

    say FILEHANDLE LIST print FILEHANDLE LIST

    In short, the '+' indicates that the FILEHANDLE (typically STDOUT) is to be assumed and the arguments following the '+' are the LIST to be output.

    Edit: Sorry, that last paragraph was poorly worded. Hopefully, this next one is better. Also see the example code in the Update at the end.

    If the FILEHANDLE is omitted, the '+' indicates that the arguments following the '+' (once expanded) are the LIST to be output. If the FILEHANDLE is included, the '+' is not required.

    More complete details can be found in print and perlop - Symbolic Unary Operators.

    Regarding Modern::Perl, that will effectively do the use feature 'say'; for you; if you comment that out, you will need to do it yourself. I think that's what you were getting at; although, I did get a little lost in the multiple negatives and conditions: "the code works if ... but it doesn't unless ...".

    Update: I felt my response might have been misconstrued as being contrary to the previous responses. This is certainly not my intention; in fact, there weren't any responses when I wrote mine. The links to documention that I provided do give more detailed information; however, the following code examples may clarify what I was saying.

    $ perl -Mstrict -Mwarnings -E 'my @x = (1,2,3); say (@x)[1]' say (...) interpreted as function at -e line 1. syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.
    $ perl -Mstrict -Mwarnings -E 'my @x = (1,2,3); say STDOUT (@x)[1]' 2
    $ perl -Mstrict -Mwarnings -E 'my @x = (1,2,3); say +(@x)[1]' 2

    -- Ken

Re: Confused by '+' syntax in code sample.
by Happy-the-monk (Canon) on Jun 26, 2013 at 08:33 UTC

    I was wondering what the '+' sign was doing

    Thank you, Anonymous Monk, for asking this.
    I was actually looking for this syntax, having seen this before and I was getting weary of all the parantheses I'd need without it.

    Cheers, Sören

    (hooked on the Perl Programming language)