[Melbourne-pm] creating useful package metadata

Mathew Robertson mathew.robertson at netratings.com.au
Wed Oct 22 16:26:44 PDT 2008


Hi folks,

this is just a little idea that I came up with once... I'm not sure if 
it has been used anywhere before, but I thought I'd share it anyway.

Here is some example code for a MyPackage.pm and a test program:


  package MyPackage;
  our @EXPORT_OK = qw (
    blah
  );
  sub blah {...}
  1;


  use strict;
  use warnings;
  use Data::Dumper;
  my $x;
  eval {
    $x = require "MyPackage.pm";
  }
  print Dumper($x);


Running the test program will print:

  $VAR0 = 1;

I had seen this:

  __PACKAGE__;

So I figured that we could the '1;' to:

  return bless({
    EXPORT_OK => [sort { uc($a) cmp uc ($b) } @EXPORT_OK],
  },__PACKAGE__);

The new result is:

  $VAR0 = bless({
    EXPORT_OK => [
      blah
    ]
  }, 'MyPackage');

In both cases we can use the returned value in boolean context to 
determine if the module loaded.  The second case however gives us a 
useful metadata, which could include the version, exported stuff, etc.

Other ideas include:
- rather than simply blessing a returned result, we could create a 
Contextual::Return object instead
- we could return a reference to some private data
- return a coderef to the initialisation function (as opposed to just 
calling 'import')
- return MyPackage's symbol

cheers,
Mathew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20081023/1a6d3ffe/attachment.html>


More information about the Melbourne-pm mailing list