in reply to Re: Faster indexing an array
in thread Faster indexing an array

I for one, tend to use scalars and refs because that makes using the variables more consistent (arrows for everybody!). It makes for less effort to pass around the refs to helper functions. I'll still use a direct array variable for something very local and/or temporary, but in general it'll be refs.

These days, I often go all the way to a $universal hashref, which makes it almost trivial to save and restore the program state.

PS:
Such a $universal hashref technique is not recommended for anyone who has issues with spelling key names consistently and accurately.

Replies are listed 'Best First'.
Re^3: Faster indexing an array
by doom (Deacon) on Feb 17, 2015 at 22:23 UTC
    (1) I suggest using %hash and @array for local use, and if you need to pass it around, take a ref to it... I'll often have a routine that creates a %hash and then does a "return \%hash;". The reasoning is that the sigils are something like built-in hungarian notation, and you might as well take advantage of them when you can. (By the way: Conway recommends using a "_ref" suffix on references, but my take is that's either too heavy-handed or doesn't go far enough. If you're going to say "_ref" you might as well specify which it is: "_aref" or "_href". If all you care about is whether it's a reference or a scalar, then use plural or singular names: "$items" or "$item".

    (2) But the real reason I'm writing is this remark: "These days, I often go all the way to a $universal hashref, which makes it almost trivial to save and restore the program state."

    (a) 'tis true that hash field names get autovivified and hence defeat "use strict", but if you have a known set of allowed names, you can strictify them yourself, using the "lock_keys" routine from Hash::Util.

    my %uni = map{ $_ => undef } @allowed_names; Hash::Util::lock_keys( %hash );
    (b) but you're reinventing perl objects. You might as well just do objects... then if you're using something moose-like, like Mouse or Moo, the "allowed names" get turned into a list of "has" lines.