Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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.

Comment on Re: Passing array into a hash
Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025981]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2015-11-28 20:54 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (744 votes), past polls