Skip Menu |
Report information
Id: 131061
Status: pending release
Priority: 0/
Queue: perl5

Owner: Nobody
Requestors: hakon.hagland [at] gmail.com
Cc:
AdminCc:

Operating System: (no value)
PatchStatus: (no value)
Severity: low
Type: unknown
Perl Version: (no value)
Fixed In: (no value)



To: perlbug [...] perl.org
Subject: Documentation of backslash operator inducing list context for its operand is missing
From: Håkon Hægland <hakon.hagland [...] gmail.com>
Date: Sat, 25 Mar 2017 23:24:49 +0100
Download (untitled) / with headers
text/plain 743b
I could not find any documentation for the following behavior:

$ perl -E '$s =\sort qw(a b c); say $$s'
c

According to the documentation for sort, the behavior of sort in scalar context is undefined

$ perl -E '$s = sort qw(a b c); say "undef" if !defined $s'
undef

Apparently when using the backslash operator, sort does not consider
itself to be in scalar context any more, or else the first example should give an undefined $$s.


Best regards,
Håkon Hægland
Date: Sat, 25 Mar 2017 22:36:39 +0000
From: Zefram <zefram [...] fysh.org>
Subject: Re: [perl #131061] Documentation of backslash operator inducing list context for its operand is missing
To: perl5-porters [...] perl.org
Download (untitled) / with headers
text/plain 719b
Hakon Haegland wrote: Show quoted text
>Apparently when using the backslash operator, sort does not consider >itself to be in scalar context any more,
Indeed, it is in list context. If we perform the sort statically, the code you gave is equivalent to $s = \qw(a b c); and the refgen distributes, making this effectively $s = (\"a", \"b", \"c"); which ultimately behaves as $s = \"c"; Show quoted text
> or else the first example should >give an undefined $$s.
No. Undefined behaviour means there is no specific thing that it "should" do, not even being consistent with its undefined behaviour under other circumstances. There is no bug here, in either the behaviour or the documentation. -zefram
To: perlbug-followup [...] perl.org
Subject: Re: [perl #131061] Documentation of backslash operator inducing list context for its operand is missing
From: Håkon Hægland <hakon.hagland [...] gmail.com>
Date: Sun, 26 Mar 2017 09:24:17 +0200
Download (untitled) / with headers
text/plain 1.1k
Thanks for the quick reply!

> Indeed, it is in list context.  

That is exactly what I am asking. Why is it list context? On the
left-hand side of the equation there is clearly a scalar context:

$ perl -E '$s =\sort qw(a b c); say $$s'
c

I can make it list context by for example writing: 

$ perl -E '($s) =\sort qw(a b c); say $$s'
a


2017-03-25 23:37 GMT+01:00 Zefram via RT <perlbug-followup@perl.org>:
Show quoted text
Hakon Haegland wrote:
>Apparently when using the backslash operator, sort does not consider
>itself to be in scalar context any more,

Indeed, it is in list context.  If we perform the sort statically,
the code you gave is equivalent to

        $s = \qw(a b c);

and the refgen distributes, making this effectively

        $s = (\"a", \"b", \"c");

which ultimately behaves as

        $s = \"c";

>                                         or else the first example should
>give an undefined $$s.

No.  Undefined behaviour means there is no specific thing that it
"should" do, not even being consistent with its undefined behaviour
under other circumstances.

There is no bug here, in either the behaviour or the documentation.

-zefram



From: Eirik Berg Hanssen <Eirik-Berg.Hanssen [...] allverden.no>
CC: Perl5 Porters <perl5-porters [...] perl.org>
Date: Sun, 26 Mar 2017 09:49:12 +0200
Subject: Re: [perl #131061] Documentation of backslash operator inducing list context for its operand is missing
To: Zefram <zefram [...] fysh.org>

On Sat, Mar 25, 2017 at 11:36 PM, Zefram <zefram@fysh.org> wrote:
Show quoted text
There is no bug here, in either the behaviour or the documentation.

  Well, the documentation is not wrong.  But I'd argue it is lacking.

  Given how important context is when programming in Perl, I'm surprised perlop doesn't tell us what context the various operators impose on their operands, or if they propagate context.

  I note that among the Symbolic Unary Operators, C<< \ >> is the only one that imposes list context on its operand – C<< + >> propagates (void, scalar, list) context, and the others impose scalar context.

  That the others impose scalar context is consistent with the "default" previously given in perlop: "With very few exceptions, these all operate on scalar values only, not array values."

  That C<< + >> propagates context is implied (but IMO could better be explicit) by perlop.

  That C<< \ >> imposes list context is not even implied by perlop.

  It seems to me Less Than Awesome that this exception for C<< \ >> is not documented in perlop.


Eirik
Date: Sun, 26 Mar 2017 08:58:32 +0100
To: perl5-porters [...] perl.org
From: Zefram <zefram [...] fysh.org>
Subject: Re: [perl #131061] Documentation of backslash operator inducing list context for its operand is missing
Download (untitled) / with headers
text/plain 370b
Hakon Haegland wrote: Show quoted text
>That is exactly what I am asking. Why is it list context?
Because the refgen operator supplies list context to its operand. The context in which the refgen operator is placed is irrelevant. Most Perl operators behave in this kind of manner, statically imposing a context on their operands rather than passing through the outer context. -zefram
Date: Sun, 26 Mar 2017 09:09:51 +0100
To: Perl5 Porters <perl5-porters [...] perl.org>
From: Zefram <zefram [...] fysh.org>
Subject: Re: [perl #131061] Documentation of backslash operator inducing list context for its operand is missing
Download (untitled) / with headers
text/plain 321b
Eirik Berg Hanssen wrote: Show quoted text
> Given how important context is when programming in Perl, I'm surprised >perlop doesn't tell us what context the various operators impose on their >operands, or if they propagate context.
Yes, good point. I agree that the context behaviour of \ in particular seems underspecified. -zefram
RT-Send-CC: perl5-porters [...] perl.org
Download (untitled) / with headers
text/plain 449b
On Sun, 26 Mar 2017 01:10:47 -0700, zefram@fysh.org wrote: Show quoted text
> Eirik Berg Hanssen wrote:
> > Given how important context is when programming in Perl, I'm surprised > >perlop doesn't tell us what context the various operators impose on their > >operands, or if they propagate context.
> > Yes, good point. I agree that the context behaviour of \ in particular > seems underspecified. > > -zefram >
Can someone suggest a patch? -- Karl Williamson
RT-Send-CC: perl5-porters [...] perl.org
Download (untitled) / with headers
text/plain 1.1k
On Wed, 03 May 2017 09:25:15 -0700, khw wrote: Show quoted text
> On Sun, 26 Mar 2017 01:10:47 -0700, zefram@fysh.org wrote:
> > Eirik Berg Hanssen wrote:
> > > Given how important context is when programming in Perl, I'm surprised > > >perlop doesn't tell us what context the various operators impose on their > > >operands, or if they propagate context.
> > > > Yes, good point. I agree that the context behaviour of \ in particular > > seems underspecified. > > > > -zefram > >
> > Can someone suggest a patch?
I am too busy right now to suggest a patch, but I can outline the behaviour, including details that are currently undocumented: \ gives its operand list context, regardless of its own context, but it behaves specially when it comes to sigilled items. \@foo and \%foo do not flatten the aggregate. Similarly, \&foo does not call the subroutine. Parentheses around an aggregate (\(@foo), \(%foo)) cause flattening, but make no difference to other sigils. Those parentheses only have that effect when they surround nothing else (except perhaps a unary + or trailing comma). So: \(@foo, @bar) # does not flatten either array \(@foo, (@bar)) # flattens only @bar -- Father Chrysostomos
Subject: Re: [perl #131061] Documentation of backslash operator inducing list context for its operand is missing
From: Zefram <zefram [...] fysh.org>
Date: Tue, 5 Dec 2017 17:52:39 +0000
To: perl5-porters [...] perl.org
Documentation patched in commit 39dc9d148281a0a05168f216359b3bdb696fec66. -zefram


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org