Questions about the operator \(...)

T

Todd

Hi perl guys,

See the results below, what confused me most is difference between the
"case3" and "case4":


#! /bin/perl -l

@a = (aa, bb, cc);
sub foo {@a}

print "case1: ", \foo;
print "case2: ", \@a;
print "case3: ", \(@a);
print "case4: ", \(@a, 1);

__END__
case1: SCALAR(0x10410e54)SCALAR(0x1042ec3c)SCALAR(0x1042ec48)
case2: ARRAY(0x1042eae0)
case3: SCALAR(0x10410f38)SCALAR(0x1042eab0)SCALAR(0x1042ec0c)
case4: ARRAY(0x1042eae0)SCALAR(0x1042ec3c)


From the results shown above, I get:

\(@a) is not (\@a), since the results are 3 references
\(@a, 1) is (\@a, \1), since the results are 2 references.

What's the reason to causes this differences?

I knew there must be some posts where this topics already has been
thoroughly talked, but i can't google it. So may you give me some
hints about this tricky thing?


Best regards,
Todd
 
M

Michele Dondi

From the results shown above, I get:

\(@a) is not (\@a), since the results are 3 references
\(@a, 1) is (\@a, \1), since the results are 2 references.

Well, first of all the referentiation operator is indeed very awkward
in that it is *distributive*, which is somewhat inconsistent -with the
rest of Perl- on its own. But then the behaviour that you discovered
is *very* inconsistent: in the first case it seems that @a is
flattened in the list, while in the second it is retained as an
aggregate. IMHO it should be flattened there too.


Michele
 
U

Uri Guttman

MD> Well, first of all the referentiation operator is indeed very awkward
MD> in that it is *distributive*, which is somewhat inconsistent -with the
MD> rest of Perl- on its own. But then the behaviour that you discovered
MD> is *very* inconsistent: in the first case it seems that @a is
MD> flattened in the list, while in the second it is retained as an
MD> aggregate. IMHO it should be flattened there too.

\ distributes over the syntactical elements in the following
parenthesized list. so it will work with \(@x, @y) and return \@x and
\@y. it is trivial to distribute \ over the expanded elements of the
list with map \$_, @x, @y. so consider the behavior as a syntactical
shortcut as you can get the other semantics with map. i still have never
seen a real world need or use of \() with distribution.

if there is only a single syntactical element inside the \(@x) it will
distribute over the array's elements since making that be the same as
\@x would be redundant.

not the most consistant thing but the choices larry made make the most
flexible use of the concept and syntax.

uri
 
X

xhoster

Todd said:
Hi perl guys,

See the results below, what confused me most is difference between the
"case3" and "case4":

#! /bin/perl -l

@a = (aa, bb, cc);
sub foo {@a}

print "case1: ", \foo;
print "case2: ", \@a;
print "case3: ", \(@a);
print "case4: ", \(@a, 1);

__END__
case1: SCALAR(0x10410e54)SCALAR(0x1042ec3c)SCALAR(0x1042ec48)
case2: ARRAY(0x1042eae0)
case3: SCALAR(0x10410f38)SCALAR(0x1042eab0)SCALAR(0x1042ec0c)
case4: ARRAY(0x1042eae0)SCALAR(0x1042ec3c)

From the results shown above, I get:

\(@a) is not (\@a), since the results are 3 references
\(@a, 1) is (\@a, \1), since the results are 2 references.

What's the reason to causes this differences?

My guess is that they wanted to provide an easy way to get a list of
references to each element of an array, so decided to make \(@foo) do that.
I knew there must be some posts where this topics already has been
thoroughly talked, but i can't google it. So may you give me some
hints about this tricky thing?

perldoc perlref

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
T

Todd

perldoc perlref
I don't think perlref have the explanation about this.

Let me make my example clearer:

#! /bin/perl -l

@a = (aa, bb, cc);
sub foo {@a}

print '\\@a: ', \@a;
print '\\(@a): ', \(@a);
print '\\(@a,1): ', \(@a, 1);
print;
print '\\foo: ', \foo;
print '\\(foo): ', \(foo);
print '\\(foo,1):', \(foo, 1);

__END__

\@a: ARRAY(0x1042eae0)
\(@a): SCALAR(0x10410f38)SCALAR(0x1042eab0)SCALAR(0x1042ec48)
\(@a,1): ARRAY(0x1042eae0)SCALAR(0x1042ec78)

\foo: SCALAR(0x10410e54)SCALAR(0x1042ec84)SCALAR(0x1042ec78)
\(foo): SCALAR(0x10410e54)SCALAR(0x1042ec84)SCALAR(0x1042ec78)
\(foo,
1):SCALAR(0x10410e54)SCALAR(0x1042ec84)SCALAR(0x1042ec78)SCALAR(0x1042ecb4)


From the results, I see that \@a and \(@a,1) are the different from
others. So i do think it's defined by syntax sugar for some special
usages. We can only treat them as syntax ruls, there are no expression
meaning here.

Finally, i just them like the "special form" case of lisp language.

-Todd
 
B

Ben Morrow

Quoth Todd said:
I don't think perlref have the explanation about this.

It's not terribly obvious: I had to read it several times before I found
it. It's in "Making References", item 2 (about []):

Taking a reference to an enumerated list is not the same as using
square bracketsâ€â€instead it’s the same as creating a list of referâ€
ences!

@list = (\$a, \@b, \%c);
@list = \($a, @b, %c); # same thing!

As a special case, "\(@foo)" returns a list of references to the
contents of @foo, not a reference to @foo itself. Likewise for
%foo, except that the key references are to copies (since the keys
are just strings rather than fullâ€fledged scalars).

The 'special case' is actually backwards here: one would expect (@foo)
to interpolate the contents of @foo, so it's \(@foo, @bar) (and \@foo
itself, of course) that is the special case. It applies to any literal
list with more than one specified element as the argument to \,
including lists inside more parens and lists inside 'my', 'our', 'state'
and '+', but excluding lists inside 'do'.
Let me make my example clearer:

I think it was quite clear already. The behaviour is inconsistent and
slightly odd, but useful, as with much of Perl.

@a = (aa, bb, cc);
sub foo {@a}
print '\\foo: ', \foo;
print '\\(foo): ', \(foo);
print '\\(foo,1):', \(foo, 1);
\foo: SCALAR(0x10410e54)SCALAR(0x1042ec84)SCALAR(0x1042ec78)
\(foo): SCALAR(0x10410e54)SCALAR(0x1042ec84)SCALAR(0x1042ec78)
\(foo,
1):SCALAR(0x10410e54)SCALAR(0x1042ec84)SCALAR(0x1042ec78)SCALAR(0x1042ecb4)

\ passes list context to its argument (check with wantarray) so these
are all as expected.
From the results, I see that \@a and \(@a,1) are the different from
others. So i do think it's defined by syntax sugar for some special
usages. We can only treat them as syntax ruls, there are no expression
meaning here.

If you think about it, the evaluation of \ has to be rather special. @a
will normally evaluate to either its contents or its length, neither of
which lets you get a reference to array as a whole. The exception is the
behaviour of functions like 'push', which, at least nowadays, can be
understood in terms of prototypes, which are defined in terms of \.

Ben
 
T

Todd

Thanks, Ben.

But it seems that i'm not smart enough to understand these. I know for
Perl, in many places, syntax wins over expression explanation like
context, .... Hmm, but Perl doesn't have fixed sytax, right?

But anyway, it's Perl, just like any human language...

-Todd
 
B

Ben Morrow

Quoth Ben Morrow said:
Taking a reference to an enumerated list is not the same as using
square bracketsâ€â€instead it’s the same as creating a list of referâ€

GAH @groff for adding fancy Unicode formatting! I didn't realise and
failed to set the Content-type: sorry.

Ben
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Uri Guttman
\ distributes over the syntactical elements in the following
parenthesized list. so it will work with \(@x, @y) and return \@x and
\@y. it is trivial to distribute \ over the expanded elements of the
list with map \$_, @x, @y. so consider the behavior as a syntactical
shortcut as you can get the other semantics with map. i still have never
seen a real world need or use of \() with distribution.

if there is only a single syntactical element inside the \(@x) it will
distribute over the array's elements since making that be the same as
\@x would be redundant.

not the most consistant thing but the choices larry made make the most
flexible use of the concept and syntax.

Allow me to disagree.

1) (as with most of Perl defects) quite probably it might have been
not "a choice" made by anybody, but just a side effect of a
particular implementation (the side effect which was discovered too
late to change the semantic);

2) are you sure it was made by Larry, and not by "a random poster to p5-p"?

3) as far as I'm concerned, this "concept and syntax" is so convoluted
that nobody will be able to RELIABLY memorize it. Which makes it
unusable (and unused); I doubt one should call unusable stuff "the
most flexible"...

Hope this helps,
Ilya
 
X

xhoster

Ben Morrow said:
Quoth Todd said:
I don't think perlref have the explanation about this.

It's not terribly obvious: I had to read it several times before I found
it. It's in "Making References", item 2 (about []):

Taking a reference to an enumerated list is not the same as using
square bracketsâ€â€instead it’s the same as creating a list of
refer†ences!

@list = (\$a, \@b, \%c);
@list = \($a, @b, %c); # same thing!

As a special case, "\(@foo)" returns a list of references to the
contents of @foo, not a reference to @foo itself. Likewise for
%foo, except that the key references are to copies (since the keys
are just strings rather than fullâ€fledged scalars).

The 'special case' is actually backwards here: one would expect (@foo)
to interpolate the contents of @foo, so it's \(@foo, @bar) (and \@foo
itself, of course) that is the special case.

Well, except that it just explicitly said what \(@foo, @bar) did.

So \(@foo) is a special case *compared to what it had just said*.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top