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

Further to Clarifying the Comma Operator, I am still working towards a revision of the description of the Fat Comma operator (=>) in perlop, and I would appreciate any comments regarding the following.

Currently, perlop says:

If the argument on the left is not a word, it is first interpreted as an expression, and then the string value of that is used.

So I tested this and I am suspicious that it is not true. Consider the following:

use strict; use warnings; use Devel::Peek; sub mysub { Dump(\@_); } my $x = 10; Dump(\$x); mysub( $x => "a string" );

This produces:

SV = RV(0x9fc41fc) at 0x9fc41f0 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x9fd4418 SV = IV(0x9fd4414) at 0x9fd4418 REFCNT = 2 FLAGS = (PADMY,IOK,pIOK) IV = 10 SV = RV(0x9fc41fc) at 0x9fc41f0 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x9fd43f8 SV = PVAV(0x9fc528c) at 0x9fd43f8 REFCNT = 3 FLAGS = () ARRAY = 0x9fd81f8 FILL = 1 MAX = 3 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x9fd4414) at 0x9fd4418 REFCNT = 2 FLAGS = (PADMY,IOK,pIOK) IV = 10 Elt No. 1 SV = PV(0x9fc20e8) at 0x9fe0a08 REFCNT = 2 FLAGS = (POK,READONLY,pPOK) PV = 0x9fd56f8 "a string"\0 CUR = 8 LEN = 12

The first element of @_ in the subroutine is an alias to $x. It is not the result of taking the string value after evaluating the expression $x. Therefore, it seems to me that the description of the => operator is incorrect.

If my test results are consistent with the description, a smack on the head would be appreciated, followed by anything that might help correct my misunderstanding. If not, suggestions of what the correct description is would be even more appreciated.

To begin with, I suspect that if the left operand of => isn't a word with the form of a simple identifier then the left operand of => is handled the same as if => was a comma. But the "list in scalar context" discussions make me mindful that I don't know all I might about the comma operator and might easily jump to wrong conclusions about what is happening.

update: Sorry, I didn't realize I had posted some earlier version of this, went on merrily editing and then hastily clicked update without checking to see what was originally posted.

Replies are listed 'Best First'.
Re: Further clarification of the Fat Comma
by ysth (Canon) on Jun 08, 2009 at 04:50 UTC
Re: Further clarification of the Fat Comma
by cdarke (Prior) on Jun 08, 2009 at 09:20 UTC
    You might be getting sidetracked into issues with @_. I tried running your examples with:
    mysub( $x, "a string" );
    and I got the same results as using =>, however they both differ slightly from yours (aside from the addresses):
    SV = RV(0x3b230) at 0x3b224 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x18ab37c SV = PVAV(0x3c2f0) at 0x18ab37c REFCNT = 3 FLAGS = () ARRAY = 0x18bae14 FILL = 1 MAX = 3 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x18ab3a8) at 0x18ab3ac REFCNT = 2 FLAGS = (PADMY,IOK,pIOK) IV = 10 Elt No. 1 SV = PV(0x381ec) at 0x18c6944 REFCNT = 2 FLAGS = (PADTMP,POK,READONLY,pPOK) # < difference here PV = 0x18c0b9c "a string"\0 CUR = 8 LEN = 12
    I'm running Windows ActiveState 5.10.0.
Re: Further clarification of the Fat Comma
by JavaFan (Canon) on Jun 08, 2009 at 08:04 UTC
    I already pointed this out here.