Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

This is a nice example of why the way I learned to write subs makes me happy. I'm sharing it with you, so you can join in the fun.

sub a_subroutine { my ($opt, @bad ) = @_; my $string = delete $opt->{a_descriptively_named_string}; my $desktop_info = delete $opt->{desktop_config}; my $important_files = delete $opt->(important_files} // []; checkopt( \%opt, \@bad, a_descriptively_named_string => defined $string, desktop_config => ref $desktop_info eq 'HASH', important_files => ref $important_files eq 'ARRAY +', ); # Do stuff. } # checkopt is a nice little function along these line. # You can customize it a bit, (add support for testing for invocants, +fancy crap with passing additional error messages, what have you) # But in pretty much this form it has caught made many a bug shallow sub checkopt { while (@_) { if ( ref eq 'HASH' ) { my $opts = shift; @args = keys @$opts; croak "Unexpected options in subroutine call: @args" if %$opts; } elsif ( ref eq 'ARRAY' ) { my $bad = shift; croak "Unexpected arguments in subroutine call." if @$bad } else { my $option_name = shift; my $option_okay = shift; croak "Invalid option '$option_name' in subroutine call" unless $option_okay; } } }

This code is nice because it is very explicit. It shows you exactly what it expects. It doesn't really rely on any weirdness.

So we start off by passing in a single hashref with a set of named arguments. Extra arguments are sucked up by the @bad variable.

We then unpack the options hashref into variables, deleting keys as we go. This ensures that the hash will be empty when all known arguments have been unpacked. This is a handy place to set default values for options.

Finally we call our check function. We pass in @bad and %opt by reference, to ensure that they are empty. Other arguments are handled by passing in key/value pairs. This version croaks when a false value is found for any key.

Yes this code is a bit verbose. But it is easy to read, very regular and can contain a lot of information. Including what type each variable/argument is.

It's also worth thanking tye who wrote the original version of this check function I recalled for you. Any errors in its operation as embodied here are solely mine. The real thing actually works.

TGI says moo

In reply to Re^5: Improve readability of Perl code. Naming reference variables. by TGI
in thread Improve readability of Perl code. Naming reference variables. by hakonhagland

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2023-12-09 22:12 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (38 votes). Check out past polls.