in reply to Re: return +0
in thread return +0

Provided, i found below code in Apache-Compress-1.005/Compress.pm

I belive '+' has some importance.

if (!defined($how_decide) || lc($how_decide) eq 'header') { return +($r->header_in('Accept-Encoding')||'') =~ /gzip/; } elsif (lc($how_decide) eq 'user-agent') {

updated: Removed the duplicate "found" word in my comment.

Replies are listed 'Best First'.
Re^3: return +0
by lidden (Curate) on Nov 27, 2007 at 09:07 UTC
    In that example the + tells perl that the parens are not enclosing the arguments to a function call, so it is parsed correctly.

      Here's a nice simple example of when you might need to use it:

      $ perl -e 'print (2+3)/5' 5

      How on earth does that print 5? Because it evaluates as "print the result of 2 + 3, then divide print's return value (which is 1 if print worked) by 5, and then do nothing with the result". This is obvious if you stick another print in there:

      $ perl -e 'print print (2+3)/5' 50.2

      which is parsed as:

      print((print(2+3))/5)

        And just to close the logic loop (++DrHyde and lidden, btw), this leading plus helps the parser evaluate all those numbers into a single value before printing.

        $ perl -e 'print +(2+3)/5' 1

        So I assume the author of the code the OP was reading thought there might be some ambiguity and wanted to help the parse out with that return statement.


        I humbly seek wisdom.
Re^3: return +0
by syphilis (Archbishop) on Nov 27, 2007 at 08:50 UTC
    I belive '+' has some importance.

    For me, return values of +0, -0, and 0 are indistinguishable from each other - which means that the '+' has no significance at all:
    C:\_32>perl -MDevel::Peek -e "Dump(+0)" SV = IV(0x1fe2d28) at 0x343cbc REFCNT = 1 FLAGS = (PADBUSY,PADTMP,IOK,READONLY,pIOK) IV = 0 C:\_32>perl -MDevel::Peek -e "Dump(-0)" SV = IV(0x432d2c) at 0xd73cec REFCNT = 1 FLAGS = (PADBUSY,PADTMP,IOK,READONLY,pIOK) IV = 0 C:\_32>perl -MDevel::Peek -e "Dump(0)" SV = IV(0x2492d28) at 0x23cbc REFCNT = 1 FLAGS = (PADBUSY,PADTMP,IOK,READONLY,pIOK) IV = 0
    (Replace the double quotes with single quotes if you're on a *nix-type operating system.)

    Cheers,
    Rob

      Just note that -.0 is distinct from .0.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        Perhaps you could be less vague. There is no such distinction in my tests:

        > perl -MDevel::Peek -e"Dump(+.0)" SV = NV(0xa4a5fc) at 0x3d53c4 REFCNT = 1 FLAGS = (PADBUSY,PADTMP,NOK,READONLY,pNOK) NV = 0 > perl -MDevel::Peek -e"Dump(-.0)" SV = NV(0xa4a60c) at 0x3d53f4 REFCNT = 1 FLAGS = (PADBUSY,PADTMP,NOK,READONLY,pNOK) NV = 0 > perl -MDevel::Peek -e"Dump(.0)" SV = NV(0xa4a5fc) at 0x3d53c4 REFCNT = 1 FLAGS = (PADBUSY,PADTMP,NOK,READONLY,pNOK) NV = 0

        - tye