Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Access Hashes of Hashes etc.

by TomDLux (Vicar)
on Sep 14, 2011 at 16:19 UTC ( #925944=note: print w/ replies, xml ) Need Help??


in reply to Re: Access Hashes of Hashes etc.
in thread Access Hashes of Hashes etc.

I often find it convenient to have a routine which claims to process all the records, which actually goes through the top-level keys and invokes another routine to processing the corresponding sub-nodes.

I prefer to have short subroutines in any case, under 20 lines if at all possible, so this makes it easier to understand what each routine does. It's not like the 80s, when invoking a subroutine was drastically slower than doing things inline.

It does generate a problem of coming up with names for several levels of subroutine: process_all_names(), process_one_name(), process_first_name(), process_last_name(), ...

As Occam said: Entia non sunt multiplicanda praeter necessitatem.


Comment on Re^2: Access Hashes of Hashes etc.
Re^3: Access Hashes of Hashes etc.
by muba (Priest) on Sep 14, 2011 at 22:34 UTC

    I know what you mean. I'm currently in the middle of writing a JSON parser/generator, and I have these subroutines with lovely names such as _parser__handle_map, _parser__handle_array, _parser__handle_string, _parser__next_token_type, _parser__next_value, _to_json__format_hash, _to_json__format_array, and _to_json__format_string.

    Pretty straightforward names, and it positively beats those unruly if/elsif/.../else constructions.

    Basically a strategy like that does the same thing as I suggested but even takes it a step further. Instead of pulling data from the greater structure into a variable until you're at the deepest level you care about, you pull the data from the structure into a subroutine until you've got what you really wanted.

Log In?
Username:
Password:

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

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

    The best computer themed movie is:











    Results (248 votes), past polls