Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use data structure references more commonly

Tags:

reference

perl

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 functions 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.

like image 647
Joel Berger Avatar asked Feb 12 '11 19:02

Joel Berger


People also ask

What is the most common data structure used?

Arrays. An array is the simplest and most widely used data structure. Other data structures like stacks and queues are derived from arrays.

What is reference in data structure?

A reference is an indirect link to a value, an atom or an array. A variable can contain a single reference to a value, or it can contain an array of references. Variables and arrays can themselves contain references, nested to any depth.


2 Answers

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.

like image 99
Eric Strom Avatar answered Oct 13 '22 01:10

Eric Strom


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.

like image 23
bvr Avatar answered Oct 13 '22 01:10

bvr