Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Passing array into a hash

by sundialsvc4 (Abbot)
on Mar 28, 2013 at 15:27 UTC ( #1025981=note: print w/replies, xml ) Need Help??

in reply to Passing array into a hash

What I usually do is to have a single parameter that is a hashref.   For example:

use strict; use warnings; errors( { 'all_msgs' => 'foo', 'msg_subject' => 'bar', } ); sub errors { my $args = shift; my $all = $$args{'all_msgs'} || ''; ... }

A few things to notice here, and they’re partly just stylistic:

  1. Perl lets you have a trailing-comma, as I do in my call to errors(), for the pure-and-simple reason that it's a bit easier to add new stuff after it as you revise the program.   The hash as-shown contains two keys, not three.
  2. I enclose the hash-keys in quotes, vs. the so-called “barewords.”
  3. To me, the use of use strict; use warnings; is obligatory.
  4. In the snippet of code defining errors() itself, $$args{'foo'} is a shorthand for $args->{'foo'} that I happen to prefer.

In this example, the first (and only) argument to the subroutine is “a reference to a hash,” otherwise known simply as a “hashref.”   Perl uses the notion of a “ref” quite extensively, allowing you to build up arbitrarily complex data structures.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025981]
[choroba]: Isn't Using PerlPod Creatively rather a meditation?
[choroba]: I don't see a question
[1nickt]: ugh, I stuck my head in the bass bin for 30 seconds on a dare at Ted Nugent at Hammersmith Odeon. Yes, I am 40% deaf now.
[johngg]: My daughter is incredibly jealous of my wife who got to see The Clash at Brixton many years ago. They went to see The Vaccines there recently.
[1nickt]: But the bands are even louder! I saw Spearhead (Michael Franti) at an outdoor show and had to walk a mile away to not feel pain in my chest! Babies were crying ... I asked the sound engineer why it was necessary to have the bass so loud and he laughed...
[Discipulus]: but the best i attended live was Mano Negra Patchanka at Forte Prenestino .. in 1990
[Corion]: Hmmm - Mano Negra or at least Manu Chao seem to put on a good live show. At least the one live CD I have from Manu Chao sounds good ;)
Discipulus feels the same jealousity of the johngg's daughter
[1nickt]: choroba I agree

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (13)
As of 2017-03-24 12:17 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (301 votes). Check out past polls.