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

Re^2: Hash assignment: UndERDocumented feature?

by fsavigny (Novice)
on Aug 11, 2013 at 17:18 UTC ( [id://1049017]=note: print w/replies, xml ) Need Help??


in reply to Re: Hash assignment: Undocumented feature?
in thread Hash assignment: Undocumented feature?

(Oops. Didn't really need to dash off, on second thought.) I slightly suspect your example is more complex than I can deal with. What I meant is simply something like this (and I think it does not matter if the values are simple scalars or references to something complex):

%what_all_workers_have = ( holidays => 30, hours_per_week => 40, office_space => 10, job_title => "Clerk", ); ## turning to John, who is a wheelchair user: %what_john_has = ( %what_all_workers_have, office_space => 20, # needs room to maneuvre hours_per_week => 35, # needs more breaks );

One needn't even worry if %workers_have actually contains the keys office_space and hours_per_week. It will either add or overwrite them, whichever is required. To me, this seems quite elegant and time-saving. In any case, it's less code to type than any other version I could think of.

Replies are listed 'Best First'.
Re^3: Hash assignment: UndERDocumented feature?
by AnomalousMonk (Archbishop) on Aug 11, 2013 at 17:53 UTC
    %what_john_has = ( %what_all_workers_have, office_space => 20, # needs room to maneuvre hours_per_week => 35, # needs more breaks );

    That's kinda like what
        my %arguments = (%$hr_defaults, %{ $hr_args || {} });
    is doing in my example code: as you've written,  %what_all_workers_have is the default of what a particular worker has.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-29 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found