One of the biggest blights on the Perl language is the dereferencing syntax. This can't be solved the Perl 6 way because leading sigils are not invariant:
my $foo = [qw/this that/]; my @foo = qw/one two/; print $foo->[0]; print $foo[0];
If we had invariant sigils, the -> could be optional. We don't, so we can't. However, if we're willing to introduce another backwards incompatible change, we can clean up slices a bit:
use Data::Dumper; my $foo = [ qw/foo bar baz/ ]; my @foo = $foo->[1,2]; print Dumper \@foo; @foo = @$foo[1,2]; print Dumper \@foo; __END__ $VAR1 = [ 'baz' ]; $VAR1 = [ 'bar', 'baz' ];
In short, allow $foo->[1,2] to return a list. Of course, this also implies: $bar->{'this', 'that'}.
Schwern has listed some of the other suggestions and Johan Vromans has proposed a working SUPER (yes, please!).
Update: Doesn't work as it does break backwards-compatibility. From perldoc perlvar:
$; The subscript separator for multidimensional array emulation. If you refer to a hash element as $foo{$a,$b,$c} it really means $foo{join($;, $a, $b, $c)} But don't put @foo{$a,$b,$c} # a slice--note the @ which means ($foo{$a},$foo{$b},$foo{$c}) Default is "\034", the same as SUBSEP in awk. If your keys contain binary data there might not be any safe value for $;. (Mnemonic: comma (the syntactic subscript separator) is a semi-semicolon. Yeah, I know, it's pretty lame, but $, is already taken for something more important.) Consider using "real" multidimensional arrays as described in perllol.
Johan Vromans has proposed a working SUPER (yes, please!)
I commented on this on p5p as well, SUPER:: is broken simply because it is ambiguous in the case of multiple inheritance. The NEXT module provides one solution, but it is notoriously slow and has some issues with edge cases (as the Catalyst folk about that). We should instead start encouraging people to use the $self->next::method syntax which the mro pragma has introduced with 5.10 and the C3 mro if they are using multiple inheritance. This will ensure that they can have the dynamism needed (which SUPER:: doesn't provide) as well as a sane and predictable answer as to what next::method calls (the next thing in the mro).
And all with the added benefit of not either breaking SUPER:: or having to make SUPER:: so complex in order to support backwards compat too.
- Stevan
use Tie::Math qw(f X Y);
my %pascal;
tie %pascal, 'Tie::Math',
sub {
if( X <= Y and Y > 0 and X > 0 ) {
f(X,Y) = f(X-1,Y-1) + f(X,Y-1);
}
else {
f(X,Y) = 0;
}
},
sub {
f(1,1) = 1;
f(1,2) = 1;
f(2,2) = 1;
};
$height = 5;
for my $y (1..$height) {
print " " x ($height - $y);
for my $x (1..$y) {
print $pascal{$x,$y} . " ";
}
print "\n";
}
I like the idea, but see the point about compatibility. How about just a 'slice' function? (built-in or module)
@foo = slice($foo,1,2);
# vs
@foo = @{$foo}[1,2];
More keystrokes, but less line noise -- intent is clear. Could DWIM for any slice-able variable:
@foo = slice( @array, 1, 2 );
@foo = slice( $a_ref, 1, 2 );
@foo = slice( %hash, qw/one two/ );
@foo = slice( $h_ref, qw/one two/ );
/me wonders if this is already on CPAN
--dagolden