Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I prefer hashes or hashrefs in Perl?

Tags:

perl

I'm still learning perl.

For me it feels more "natural" to references to hashes rather than directly access them, because it is easier to pass references to a sub, (one variable can be passed instead of a list). Generally I prefer this approach to that where one directly accesses %hashes".

The question is, where (in what situations) is better to use plain %hashes, so

$hash{key} = $value;

instead of

$href->{key} = $value

Is here any speed, or any other "thing" what prefers to use %hashes and not $hashrefs? Or it is matter only of pure personal taste and TIMTOWTDI? Some examples, when is better to use %hash?

like image 894
cajwine Avatar asked Jul 01 '13 21:07

cajwine


2 Answers

I think this kind of question is very legitimate: Programming languages such as Perl or C++ have come a long way and accumulated a lot of historical baggage, but people typically learn them from ahistorical, synchronous exposés. Hence they keep wondering why TIMTOWDI and WTF all these choices and what is better and what should be preferred?

So, before version 5, Perl didn't have references. It only had value types. References are an add-on to Perl 4, enabling lots more stuff to be written. Value types had to be retained, of course, to keep backward compatibility; and also, for simplicity's sake, because frequently you don't need the indirection that references are.

To answer your question:

Don't waste time thinking about the speed of Perl hash lists. They're fast. They're memory access. Accessing a database or the filesystem or the net, that is where your program will typically spend time.

In theory, a dereference operation should take a tiny bit of time, so tiny it shouldn't matter.

If you're curious, then benchmark. Don't draw too many conclusions from differences you might see. Things could look different on another release.

So there is no speed reason to favour references over value types or vice versa.

Is there any other reason? I'd say it's a question of style and taste. Personally, I prefer the syntax without -> accessors.

like image 184
Lumi Avatar answered Sep 21 '22 13:09

Lumi


If you can use a plain hashes, to describe your data, you use a plain hash. However, when your data structure gets a bit more complex, you will need to use references.

Imagine a program where I'm storing information about inventory items, and how many I have in stock. A simple hash works quite well:

$item{XP232} = 324;
$item{BV348} = 145;
$item{ZZ310} = 485;

If all you're doing is creating quick programs that can read a file and store simple information for a report, there's no need to use references at all.

However, when things get more complex, you need references. For example, my program isn't just tracking my stock, I'm tracking all aspects of my inventory. Inventory items also have names, the company that creates them, etc. In this case, I'll want to have my hashes not pointing to a single data point (the number of items I have in stock), but a reference to a hash:

$item{XP232}->{DESCRIPTION}  = "Blue Widget";
$item{XP232}->{IN_STOCK}     = 324;
$item{XP232}->{MANUFACTURER} = "The Great American Widget Company";

$item{BV348}->{DESCRIPTION}   = "A Small Purple Whatzit";
$item{BV348}->{IN_STOCK}      = 145;
$item{BV348}->{MANUFACTURER}  = "Acme Whatzit Company";

You can do all sorts of wacky things to do something like this (like have separate hashes for each field or put all fields in a single value separated by colons), but it's simply easier to use references to store these more complex structures.

like image 34
David W. Avatar answered Sep 18 '22 13:09

David W.