Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2015/12/03

Working with Perl Critic around Exporter

I am working on a framework for modern web APIs using CGI and old-school Perl objects, inspired by what I learned from perlbrew. This is more or less how the included modules go. This auto-exports every function that starts with 'api_', so that I could write helper_one() and helper_two() with no worries about them being exported, and without having to add api_butt, api_stump, and api_tail to @EXPORT, because it's striking me that the export list follows documentation as a pernicious form of code duplication.

  1. package API_Proof ;use strict ;  
  2. use warnings FATAL => 'all' ;  
  3.   
  4. use Exporter qw{import} ;  
  5.   
  6. our $VERSION = 0.1 ;  
  7.   
  8. our @EXPORT ;  
  9. for my $entry ( keys %API_Billing_0_1:: ) {  
  10.     next if $entry !~ /^api_/mxs ;  
  11.     push @EXPORT$entry ;  
  12.     }  
  13.   
  14. sub api_stub {  
  15.     # code goes here  
  16.     }  

I intend to put the whole deal up on +GitHub eventually, but to avoid complication, I'll just get to the point where it's used, below. I'm exporting everything that starts with api_, so it's all available for $api->run() when we get there. (It's all in previous posts.)

  1. #!/usr/bin/env perl  
  2.   
  3. use strict ;  
  4. use warnings ;  
  5. use lib '/path/to/lib' ;  
  6.   
  7. my $api = API->new( @ARGV ) ;  
  8. $api->run() ;  
  9.   
  10. package API ;  
  11. <pre class="perl" name="code">use lib '/path/to/lib' ;  
  12. </pre>  
  13. use base 'API_Base' ;  
  14. use API_Stub ;  

And here is where we run into Perl Best Practices and Perl::Critic. I've been running some modules through perlcritic -1 for sake of being complete, and it lead me to do some changes, and there's one that is keeping me from being clean. It's that I'm using @EXPORT. I should be using @EXPORT_OK or %EXPORT_TAGS instead, it says. This means, that first code block should have something like this instead.

  1. my @export ;  
  2. for my $entry ( keys %API_Bard:: ) {  
  3.     next if $entry !~ /^api_/ ;  
  4.     push @export$entry ;  
  5.     }  
  6.   
  7. $EXPORT_TAGS{ api } = [ @export ];  
  8. our @EXPORT_OK = ( @{ $EXPORT_TAGS'api' } } ) ;  

And then use API_Stub qw{:api}. I am not quite convinced. I'm open, though. I guess I would just like to know what the problem with export by default is, but this isn't in PBP.

No comments:

Post a Comment