Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Thursday January 17, 2008
10:37 AM

What I want in 5.12

[ #35412 ]

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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

  • Tie::Math [cpan.org] makes use of the existing behavior, though I'd gladly sacrifice it to see a nicer hash slice syntax.

        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);
                 }
         

  • 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