perl - Use data structure references more commonly -


i have been reading of perl513*delta files , have seen of new features coming perl 5.14. beginning perl 5.13.7 many of array/hash functions work on array/hash refs well. while seen syntactic sugar, or perl doing expect, wonder, will/should change paradigm of declaring data structures in perl? known caveat breaks compatibility eariler perl's, arguements , against using anonymous structures primarily?

for example:

#!/usr/bin/env perl  use strict; use warnings;  use 5.13.7;  $hashref = {   english => 'hello',   spanish => 'hola',   french => 'bon jour' };  foreach $greeting (keys $hashref) {   $hashref->{$greeting}; #use since need later version anyway } 

rather more traditional way using named hash (%hash).

p.s. if seen augmentative can change cw, curious hear perspectives.

the ability use array , hash functions on references syntactic sugar , need not impact way work first level plural structures. there several reasons this:

given my $array = [1 .. 10]

  • list processing functions map, grep, sort, reverse, print, say, printf , many others still need passed proper lists, means using @$array vs simpler @array these functions.

  • the for/foreach loop needs passed list, requiring @$array

  • $array true, determine length need write @$array

    while ($array)  { infinite loop } while (@$array) { wanted } while (@array)  { no room error here } 
  • sub-scripting real @array $array[$idx] marginally faster (~15%) $array->[$idx] since dereference not need happen on each access. difference hashes smaller, around 3%, due overhead of hashing function.

basically, moving references, different set of functionality needs use dereferencing sigils. instead, take advantage of pre v5.13.7 functionality declaring immediate use my @array; %hash; , utilize new syntax shortcuts in areas have used excessive @{ ... } or %{ ... } constructs applicable functions.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -