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

Re: Replacing If Elsif Else with Hash

by TomDLux (Vicar)
on Nov 24, 2012 at 16:33 UTC ( #1005395=note: print w/replies, xml ) Need Help??


in reply to Replacing If Elsif Else with Hash

I would suggest putting this chunk into a routine. It provides some documentation, it becomes easier to modify behaviour in the future, because any changes are isolated within a short routine, and implementation details are separated from business logic.

use Readonly; Readonly my %BGCOLOR => ( Extreme => 'red', ... ); Readonly my $DEFAULT_BGCOLOR => 'grey'; sub get_bg_color { my ( $severity ) = @_; return $DEFAULT_BGCOLOR unless exists $BGCOLOR{$severity}; return$BGCOLOR{$severity}; } my $bg = get_bg_color( $condition );

edit - corrected typo $BGCOLOR => %BGCOLOR; thanks, Anon Monk!

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Replies are listed 'Best First'.
Re^2: Replacing If Elsif Else with Hash
by sundialsvc4 (Abbot) on Nov 25, 2012 at 03:35 UTC

    I strongly agree with this ... no matter how exactly you choose to write the routine or to initialize the hash.

    When writing this sort of logic, be extremely careful to test for the existence, or the absence, of the hash key ... not for “falsehood.”   Someday you will have a hash-entry whose legitimate value translates to false.   Or even maybe to undef.   It’s just a hungry sort of “gotcha” that’s just waiting for your glutes.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005395]
help
Chatterbox?
[Corion]: Oh yay. The (external, not guided by me) programmers have chosen Moose+DBIx::Class for some implementation, and now seem to do 1+n SELECT statements for each row, as is usual when using ORMs.
[Corion]: So maybe I should investigate how to plug in a cache in front of DBIx::Class so I can do a ->selectall_hashre f and then satisfy the "sub"-selects from that cached single SELECT statement ...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2017-09-25 10:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (279 votes). Check out past polls.

    Notices?