Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Passing array into a hash

by sundialsvc4 (Monsignor)
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (18)
As of 2014-08-27 16:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (244 votes), past polls