开发者

Use data structure references more commonly

开发者 https://www.devze.com 2023-02-10 16:39 出处:网络
I have been reading some of the perl513*delta files and I have seen some of the new features coming to Perl 5.14. Beginning with Perl 5.13.7 many of the array/hash function开发者_如何学Gos will work o

I have been reading some of the perl513*delta files and I have seen some of the new features coming to Perl 5.14. Beginning with Perl 5.13.7 many of the array/hash function开发者_如何学Gos will work on array/hash refs as well. While this probably is seen mostly as syntactic sugar, or Perl doing what you expect, I wonder, will/should this change the paradigm of declaring data structures in Perl? With the known caveat that it breaks compatibility with eariler Perl's, what would be the arguements for and against using anonymous structures primarily?

For example:

#!/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 using a named hash (%hash).

P.S. If this is seen are augmentative I can change to CW, but I am curious to hear some perspectives.


The ability to use certain array and hash functions on references is just syntactic sugar and need not impact the way you work with first level plural structures. There are several reasons for this:

given my $array = [1 .. 10]

  • List processing functions like map, grep, sort, reverse, print, say, printf and many others still need to be passed proper lists, so this means using @$array vs the simpler @array with these functions.

  • The for/foreach loop needs to be passed a list, requiring @$array

  • $array is always true, to determine the length you need to write @$array

    while ($array)  { infinite loop }
    while (@$array) { what you probably wanted }
    while (@array)  { no room for error here }
    
  • sub-scripting a real @array as $array[$idx] is marginally faster (~15%) than $array->[$idx] since a dereference does not need to happen on each access. The difference with hashes is smaller, around 3%, due to the overhead of the hashing function.

Basically, by moving to all references, you get a different set of functionality that needs to use the dereferencing sigils. Instead, take advantage of pre v5.13.7 functionality for anything you are declaring for immediate use my @array; my %hash; and utilize the new syntax shortcuts in areas where you would have used excessive @{ ... } or %{ ... } constructs with the applicable functions.


I don't believe this upcoming change will break backward compatibility. Now you get an error on keys $hashref, with perl 5.14 it will be working. So effectively no current code could be using such feature.


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

This will not change how my top level usage per your example, but will help to reduce the awkward syntax found when using complex structures, ie "push @($this->{somekey}), $stuff" becomes "push $this->{somekey}, $stuff".

0

精彩评论

暂无评论...
验证码 换一张
取 消