Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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'

In reply to Re: by grinder
in thread by dada

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (6)
    As of 2020-10-23 11:23 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (238 votes). Check out past polls.