Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

(scalar or array context|new|decorated) function for Data::Section::Simple?

by metaperl (Curate)
on Aug 03, 2011 at 15:08 UTC ( [id://918298]=perlquestion: print w/replies, xml ) Need Help??

metaperl has asked for the wisdom of the Perl Monks concerning the following question:

Data::Section::Simple has a single function which returns a part of a DATA section as a string. I'm using this module in Locale::US as well as Regexp::Common::profanity_us. However, in both cases, I've needed to take the string of data and convert it into a series of lines:
my $data = Data::Section::Simple::get_data_section('profanity'); my @line = split "\n", $data;
So I'm thinking it would be nice to be able to get the section back as an array of lines. However, there are two approaches to this, ok 3:
  1. add an argument to get_data_section indicating to return an array of lines
  2. write a separate function get_data_section_as_lines
  3. have this single function operate in scalar or array context
What is the "best" option from a software engineering point of view?

Replies are listed 'Best First'.
Re: (scalar or array context|new|decorated) function for Data::Section::Simple?
by jethro (Monsignor) on Aug 03, 2011 at 17:40 UTC

    IMHO (and since software engineering is such a squichy science it is important to note that this is only an opinion) your third option is more error prone than the other two options. It would be different if most libraries did use the same technique, but in perl only the builtin functions use it extensively. So everyone knows to be careful about context when using builtins but the same can't be said about CPAN library functions.

    I would prefer option 2, especially if the conversion is more complex than a simple split.

Re: (scalar or array context|new|decorated) function for Data::Section::Simple?
by Perlbotics (Archbishop) on Aug 03, 2011 at 17:19 UTC

    I would suggest to use wantarray. Perhaps:

    sub my_get_data_section { my $section = shift; my $data = Data::Section::Simple::get_data_section($section); # probably error handling here... return wantarray ? split("\n", $data) : $data; } my $line = my_get_data_section( $section ); my @lines = my_get_data_section( $section );
    You could also ask the author to support this feature.
    Another option would be to overwrite Data::Section::Simple::get_data_section() with the implementation of my_get_data_section(), but that trick is probably quite dirty from a software engineering point of view...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-23 17:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found