Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid


by grinder (Bishop)
on May 17, 2002 at 20:40 UTC ( #167409=note: print w/replies, xml ) Need Help??

in reply to

I have a comment to make about the if_oper_status routine and similar lookup routines. Basically, you want to use a hash, as it makes things much easier to maintain.

BEGIN { my %oper = ( 0 => 'NON_OPERATIONAL', 1 => 'UNREACHABLE', 2 => 'DISCONNECTED', 3 => 'CONNECTING', 4 => 'AND_SO_ON', ); sub if_oper_status { my $status = shift; $oper{$status} || "UNKNOWN($status)"; } }

From there, it might be worthwhile to factor the different lookup routines into a single one, to which you pass the status code along with the hash that represents the text representation.

BEGIN { my %_status_oper = ( 0 => 'NON_OPERATIONAL', 1 => 'UNREACHABLE', 2 => 'DISCONNECTED', 3 => 'CONNECTING', 4 => 'AND_SO_ON', ); my %_status_admin = ( 0 => 'UP', 1 => 'DOWN', 2 => 'TESTING', ); sub _if_status { my $status = shift; my $text = shift; $text->{$status} || "UNKNOWN($status)"; } sub if_oper_status { _if_status( @_, \%_status_oper ) } sub if_admin_status { _if_status( @_, \%_status_admin ) } } printf " Admin status:%s Oper status:%s\n", if_admin_status($struct{dwAdminStatus}), if_oper_status($struct{dwOperStatus});

The idea I want to show is that all you have to do in this scenario is add another line to a hash and you're done. If you can modify a program simply by changing data structures you stand a better chance of not introducing bugs than if you have to change code.

Note that the above code would be a perfect use for using the &func method of calling a subroutine, because we want to pass the passed-in arguments down to the child routine without even looking at them. But when I wrote it that way it looked odd, as if I'd forgotten to pass a parameter, which only comforts me in my belief that the &func calling method is to be proscribed.

print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2020-10-24 18:34 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (246 votes). Check out past polls.