Use data structure references more often

I read some of the perl513 * delta files, and I saw some of the new features coming in Perl 5.14. Starting with Perl 5.13.7, many of the array / hash functions will work in array / hash refs as well . Although this is probably mostly seen as syntactic sugar, or Perl does what you expect, I wonder if this will / should change the paradigm for declaring data structures in Perl? With the famous warning that it violates compatibility with Perl headphones, what are the arguments for and against using anonymous structures in the first place?

For instance:

#!/usr/bin/env perl

use strict;
use warnings;

use 5.13.7;

my $hashref = {
  english => 'hello',
  spanish => 'hola',
  french => 'bon jour'
};

foreach my $greeting (keys $hashref) {
  say $hashref->{$greeting}; #use say since we need a later version anyway
}

rather than the more traditional way to use a named hash ( %hash).

P.S. , CW, .

+5
3

- - , . :

my $array = [1 .. 10]

  • , map, grep, sort, reverse, print, say, printf, - , @$array @array .

  • for/foreach , @$array

  • $array , , @$array

    while ($array)  { infinite loop }
    while (@$array) { what you probably wanted }
    while (@array)  { no room for error here }
    
  • - @array, $array[$idx] (~ 15%), $array->[$idx], . , 3%, - .

, , , . pre v5.13.7 , my @array; my %hash;, , @{ ... } %{ ... } .

+4

, . keys $hashref, perl 5.14 . , ​​.

+1

Good syntactic sugar is important. Perl itself is the "only" syntax sugar over C, which sugars assembler, which sugars machine code.

This will not change the way I use my top level in your example, but it will help to reduce the inconvenient syntax found when using complex structures, that is, "push @ ($ this → {somekey)), $ stuff" will become "push $ this → {somekey} , $ stuff. "

+1
source

All Articles