Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Versatile subs

by eff_i_g (Curate)
on Sep 02, 2005 at 23:24 UTC ( [id://488815]=perlquestion: print w/replies, xml ) Need Help??

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

Sentient Beings,

This is an extension to Hashing it out: defined? exists?. While making these file hashings modular; e.g., get_states(), get_ids(), etc., I am finding that they have the same shell of code, like so:

sub { # get possible args # initialize hash # open file # while file { # split # possible clean up code # hash } # close file # possible counter # possible write to log # return hash }


There are about 15 or so of these. I'm considering creating a versatile sub that can take additional code; something that would be called in this manner:

get_data( pre => undef, while => '$_ =~ tr/ / /s;', post => 'write_log("Found $count instances");', );


  1. Is this a good approach?
  2. If so, would it be better to use eval or do? (or something else?)


Advice appreciated.

Thanks.

Replies are listed 'Best First'.
Re: Versatile subs
by revdiablo (Prior) on Sep 02, 2005 at 23:52 UTC

    If you do end up deciding to go with this type of framework where you pass in code, please don't use string eval. That's yucky. You'd be better served with anonymous coderefs, along the lines of:

    get_data( pre => undef, while => sub { my $content = shift; $content =~ tr/ / /s; return $content; }, post => sub { my $instances = shift; write_log("Found $count instances"); }, );

      To the OP, yes, it seems like you're starting down a reasonable road. Along that road is revdiablo's suggestion, which is a significant improvement, both in speed and maintainability (compile time errors can be caught easier).

      To continue along this road (IMO), which is something I've done many times, I've started to take some serious advantage of $_. Rather than passing in a variable, just assign it to $_. Then your get_data call would look like:

      get_data( pre => undef, while => sub { # 'handle' may be a better key, IMO tr/ / /s; }, post => sub { write_log("Found $count instances"); }, );
      As you can see, this can really trivialise the code. The cost (and there is always a cost, otherwise it wouldn't be called a 'trade-off' ;-}) is that you have to be extra careful to localise $_ appropriately. So you have to localise $_ in the get_data function, saving the original value, perhaps, if you'll still need it after the callback, and, should you need $_ again in the callback, localise it again there. However, 99.999% of the time, you won't need to do so in the callback, continuing to make this a pretty good tradeoff.

      As a secondary change, I'd call this function "_get_data" instead as a signifier that this is an internal-to-your-module function and probably should not be called directly by anyone outside your function. However, I wouldn't go overboard and force it on anyone by, for example, making it a lexically scoped anonymous sub, such as:

      my $_get_data = sub { ... };
      But that's just me.

Re: Versatile subs
by NetWallah (Canon) on Sep 03, 2005 at 00:19 UTC
    I like revdiablo's approach (++).

    This could be taken one step furthur - Make an object class out of it.

    What you are operating on looks like a File, so, the myFile package could hage a get_data method. You would create an instance of "myFile", provide it with a file name, filter conditions, pre and post processing anon subrefs, then simply call get_data on each instance of the myFile class.

         "Man cannot live by bread alone...
             He'd better have some goat cheese and wine to go with it!"

Re: Versatile subs
by itub (Priest) on Sep 03, 2005 at 00:33 UTC
    I suggest reading Higher Order Perl, particularly the first chapter, which talks about callbacks.
      Thanks itub! I plan to put the 30% off coupon from Borders to use :)
Re: Versatile subs
by Roger (Parson) on Sep 02, 2005 at 23:47 UTC
    It depends on what you are trying to do. Do get_states, get_ids, etc., operate on the same set of files in your program?

    It may be better if you just have a single sub that returns an array (or reference to an array) of file attributes, or whatever form best suits your application needs, in a manar similar to the stat function.

    This way, you keep all your code in one spot, plus you have the added benefit of doing only a single pass on the file, instead of each sub doing a separate pass on the same file.

    You can also pass in an optional parameter, say, -want => [ qw/ id states / ], so that you can tell the sub that you only want a subset of file hashes, otherwise the sub will return the complete set of file hashes.

      Roger,

      Each sub works with a unique file.
Re: Versatile subs
by Anonymous Monk on Sep 02, 2005 at 23:52 UTC
    Why eval, and not a code block (like what you'd use with sort, map, or grep)?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 07:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found