Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

List assignment question

13 views
Skip to first unread message

Vincent Foley

unread,
Nov 14, 2006, 10:15:42 PM11/14/06
to perl6-l...@perl.org
Hello everyone,

I was toying around with Pugs and I tried the following Perl 5 list assignment

my ($a, undef, $b) = 1..3;

Which gave me the following error message:

Internal error while running expression:
***
Unexpected ","
expecting word character, "\\", ":", "*" or parameter name
at <interactive> line 1, column 14

I had a quick discussion on #perl6 with TimToady++ during my lunch
hour, and he said that assignment to undef was no longer valid. His
suggestion was to use the whatever operator (*) instead.

Now, this isn't implemented and my Haskell skills and knowledge of
Pugs' internals aren't really advanced enough to implement this
feature, however I can surely contribute the tests. Here's what I
have right now in my working copy, let me know if this seems
reasonable.

my @a = 1..3;
my ($one, *, $three) = @a;
is(~($one, $three), "1 3", "list assignment my ($, *, $) = @ works");

my (*, $two) = @a;
is($two, 2, "list assignment my (*, $) = @ works");
my (*, *, $three) = @a;
is($three, 3, "list assignment my (*, *, $) = @ works");

my (*, @b) = @a;
is(~@b, "2 3", "list assignment my (*, @) = @ works");

Any input would be very much appreciated (and some links to documents
that could help me contribute to the internals of Pugs),

Vincent.

Larry Wall

unread,
Nov 15, 2006, 11:18:09 AM11/15/06
to perl6-l...@perl.org
On Tue, Nov 14, 2006 at 10:15:42PM -0500, Vincent Foley wrote:
: Hello everyone,

:
: I was toying around with Pugs and I tried the following Perl 5 list
: assignment
:
: my ($a, undef, $b) = 1..3;
:
: Which gave me the following error message:
:
: Internal error while running expression:
: ***
: Unexpected ","
: expecting word character, "\\", ":", "*" or parameter name
: at <interactive> line 1, column 14
:
: I had a quick discussion on #perl6 with TimToady++ during my lunch
: hour, and he said that assignment to undef was no longer valid. His
: suggestion was to use the whatever operator (*) instead.

FWIW, I think we should follow gaal++'s suggestion and just use $ instead.
Now that I think about it some more, the * is likely to be confused with
the slurpy *, at least by readers if not by the parser.

: Now, this isn't implemented and my Haskell skills and knowledge of


: Pugs' internals aren't really advanced enough to implement this
: feature, however I can surely contribute the tests. Here's what I
: have right now in my working copy, let me know if this seems
: reasonable.

(assuming * replaced by $ here)

: my @a = 1..3;


: my ($one, *, $three) = @a;
: is(~($one, $three), "1 3", "list assignment my ($, *, $) = @ works");

Why not just interpolate: "$one $three"

: my (*, $two) = @a;


: is($two, 2, "list assignment my (*, $) = @ works");
: my (*, *, $three) = @a;
: is($three, 3, "list assignment my (*, *, $) = @ works");

Note that this "my $three" is declaring the same $three lexical,
unlike in Perl 5, which would warn about a redeclaration. That's part
of why it's customary to put each test chunk in its own bare block
to prevent such accidental collisions.

: my (*, @b) = @a;


: is(~@b, "2 3", "list assignment my (*, @) = @ works");

Other than that, looks good to me.

Larry

Mark J. Reed

unread,
Nov 15, 2006, 12:04:31 PM11/15/06
to Vincent Foley, perl6-l...@perl.org
On 11/14/06, Vincent Foley <vfo...@gmail.com> wrote:
> I was toying around with Pugs and I tried the following Perl 5 list assignment
>
> my ($a, undef, $b) = 1..3;

Huh. I didn't think that worked in Perl 5, either. What am I misremembering?
I distinctly recall having to do things like (my $a, undef, my $b) to
avoid errors because you can't assign to undef. Maybe I'm just
hallucinating.

ava...@gmail.com

unread,
Nov 15, 2006, 12:41:24 PM11/15/06
to Mark J. Reed, Vincent Foley, perl6-l...@perl.org

(my $x, undef, my $y) = 1 .. 3; parses to my ($x, undef, $y) = 1 .. 3
and always has as far as I know, so please share your hallucinogens
with the list:)

Brandon S. Allbery KF8NH

unread,
Nov 15, 2006, 1:21:53 PM11/15/06
to Mark J. Reed, perl6-l...@perl.org

It didn't work in early Perl, but certainly did by 5.00503. I don't
recall when it was added.

--
brandon s. allbery [linux,solaris,freebsd,perl]
all...@kf8nh.com
system administrator [openafs,heimdal,too many hats]
all...@ece.cmu.edu
electrical and computer engineering, carnegie mellon university
KF8NH


Paul Seamons

unread,
Nov 15, 2006, 1:29:38 PM11/15/06
to perl6-l...@perl.org
> > my ($a, undef, $b) = 1..3;
>
> Huh. I didn't think that worked in Perl 5, either. What am I
> misremembering? I distinctly recall having to do things like (my $a, undef,
> my $b) to avoid errors because you can't assign to undef. Maybe I'm just
> hallucinating.

Are you remembering this:

my $a = 1;
($a, undef, my $b) = 1..3;

If you attempted to do

my ($a, undef, $b) you'd get a warn error about re-declaring $a.

Paul

Nicholas Clark

unread,
Nov 15, 2006, 6:17:57 PM11/15/06
to Ævar Arnfjörð, Mark J. Reed, Vincent Foley, perl6-l...@perl.org

I thought that allowing undef in my ($a, undef, $b) came in around 5.004ish,
but I can't find it in perldelta, and I don't have a version compiled to
test with (or any quick way to compile them, given that pretty much only
AIX is so stable that early perls compile unmodified. Probably someone will
tell me that VMS is also good enough, for 5.002 or later)

Nicholas Clark

Dave Mitchell

unread,
Nov 15, 2006, 7:28:47 PM11/15/06
to Ævar Arnfjörð, Mark J. Reed, Vincent Foley, perl6-l...@perl.org
On Wed, Nov 15, 2006 at 11:17:57PM +0000, Nicholas Clark wrote:
> I thought that allowing undef in my ($a, undef, $b) came in around 5.004ish,
> but I can't find it in perldelta, and I don't have a version compiled to
> test with (or any quick way to compile them, given that pretty much only
> AIX is so stable that early perls compile unmodified. Probably someone will
> tell me that VMS is also good enough, for 5.002 or later)

$ perl-5322 -we'my ($x,undef,$y) = 1..3'
Can't declare undef operator in my at -e line 1, near ") ="
Execution of -e aborted due to compilation errors.
$ perl545 -we'my ($x,undef,$y) = 1..3'
$

--
"There's something wrong with our bloody ships today, Chatfield."
-- Admiral Beatty at the Battle of Jutland, 31st May 1916.

Mark J. Reed

unread,
Nov 15, 2006, 8:54:29 PM11/15/06
to Dave Mitchell, Ævar Arnfjörð Bjarmason, Mark J. Reed, Vincent Foley, perl6-l...@perl.org
On 11/15/06, Dave Mitchell <da...@iabyn.com> wrote:

> $ perl-5322 -we'my ($x,undef,$y) = 1..3'
> Can't declare undef operator in my at -e line 1, near ") ="
> Execution of -e aborted due to compilation errors.
> $ perl545 -we'my ($x,undef,$y) = 1..3'
> $

Ah-hah! So I'm not crazy! Necessarily, anyway. Just behind the times.

Thanks, Dave!

--
Mark J. Reed <mark...@mail.com>

Mark J. Reed

unread,
Nov 15, 2006, 8:51:23 PM11/15/06
to Ævar Arnfjörð Bjarmason, Mark J. Reed, Vincent Foley, perl6-l...@perl.org

Sadly, the hallucinogens are essential, not external. But I'm pretty
sure those are two different parse trees.

I misremembered the reason for the construct, though: assigning to
undef has always been ok (even in perl4 and probably earlier), but
declaring "my undef" wasn't, iirc.

ava...@gmail.com

unread,
Nov 16, 2006, 9:10:38 AM11/16/06
to Mark J. Reed, Vincent Foley, perl6-l...@perl.org
> > (my $x, undef, my $y) = 1 .. 3; parses to my ($x, undef, $y) = 1 .. 3
> > and always has as far as I know, so please share your hallucinogens
> > with the list:)
> >
>
> Sadly, the hallucinogens are essential, not external. But I'm pretty
> sure those are two different parse trees.

They have the same B::Terse and B::Deparse output on 5.8.

Jonathan Rockway

unread,
Nov 15, 2006, 12:20:20 PM11/15/06
to perl6-l...@perl.org
Mark J. Reed wrote:
> I distinctly recall having to do things like (my $a, undef, my $b) to
> avoid errors because you can't assign to undef. Maybe I'm just
> hallucinating.

Maybe :)

$ perl -Mstrict -e 'my ($a, undef, $b) = 1..3; print "$a $b\n";'
1 3

This works as far back as v5.6.0 (which is the oldest I have around).

--
package JAPH;use Catalyst qw/-Debug/;($;=JAPH)->config(name => do {
$,.=reverse qw[Jonathan tsu rehton lre rekca Rockway][$_].[split //,
";$;"]->[$_].q; ;for 1..4;$,=~s;^.;;;$,});$;->setup;

Jonathan Rockway

unread,
Nov 15, 2006, 11:28:41 AM11/15/06
to perl6-l...@perl.org
Vincent Foley wrote:
> Hello everyone,
>
> I was toying around with Pugs and I tried the following Perl 5 list
> assignment
>
> my ($a, undef, $b) = 1..3;
>
> Which gave me the following error message:
>
> Internal error while running expression:
> ***
> Unexpected ","
> expecting word character, "\\", ":", "*" or parameter name
> at <interactive> line 1, column 14

For reference, this sort of operation works if you write it on two
lines, like:

my ($a, $b);


($a, undef, $b) = 1..3;

say "$a is 1 and $b is 3";

I'll look around in the source and see if I can make this work like
perl5 (unless that's a bad idea for some reason).

Regards,
Jonathan Rockway

Larry Wall

unread,
Nov 16, 2006, 12:12:22 PM11/16/06
to perl6-l...@perl.org
On Wed, Nov 15, 2006 at 10:28:41AM -0600, Jonathan Rockway wrote:
: For reference, this sort of operation works if you write it on two

: lines, like:
:
: my ($a, $b);
: ($a, undef, $b) = 1..3;
: say "$a is 1 and $b is 3";
:
: I'll look around in the source and see if I can make this work like
: perl5 (unless that's a bad idea for some reason).

That will just be "my ($a, $, $b) = 1..3" in Perl 6. It should be
an error to try to modify undef, I think, and we have a notation for
anonymous variables, so why not use it?

Larry

cognominal

unread,
Nov 20, 2006, 6:02:13 AM11/20/06
to
Nicholas Clark wrote:


> I thought that allowing undef in my ($a, undef, $b) came in around 5.004ish,
> but I can't find it in perldelta, and I don't have a version compiled to
> test with (or any quick way to compile them, given that pretty much only
> AIX is so stable that early perls compile unmodified. Probably someone will
> tell me that VMS is also good enough, for 5.002 or later)
>

Sorry to chime in so late:
in Changes5.004.

Change 651 on 1998/03/03 by TimB...@ig.co.uk

Title: "Fix C<my ($a, undef, $b) = @x>", #F018
From: Stephane Payrard <st...@francenet.fr>
Msg-ID: <1997120400...@www.zweig.com>
Files: op.c t/op/my.t

That was a change to make "my" behave like "local" in the presence of
lhs undef in list assignent.


> Nicholas Clark

0 new messages