Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: multiple layers of referencing and dereferencing

by Herkum (Parson)
on Jun 23, 2009 at 15:28 UTC ( [id://774062]=note: print w/replies, xml ) Need Help??


in reply to Re^2: multiple layers of referencing and dereferencing
in thread multiple layers of referencing and dereferencing

The advice I would give you then is that hashes are for context and arrays are for order.

Things that you don't need to know the order of should be in some sort of hash because the keys of a hash give the data context. Example:

my @cats = qw(persian siamese cute); my @dogs = qw(doverman bulldog); ## Bad data structure my @data = (\@cats, \@dogs); function( \@data ); ## Good data structure function1({ cats => \@cats, dogs => \@dogs, });

The first example with a bad data structure requires explicit know of the exact order of your arguments to know where cats and dogs are. In addition if you just deference the data you still don't necessary know what you are working with. Don't do it.

The second example explicitly designates which array references go with what type of data. Your funtion() will need to deference the data via a name which puts your code into a context which makes it easier to understand.

sub function1 { my $params = shift; warn "My Dogs\n"; for (@{$params->{'dogs'}}) { warn "\t$_\n" } warn "My Cats\n"; for (@{$params->{'cats'}}) { warn "\t$_\n" } }

Hope this helps.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-19 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found