<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
<font size="+1"><tt>Hi folks,<br>
<br>
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.<br>
<br>
Here is some example code for a MyPackage.pm and a test program:<br>
<br>
<br>
package MyPackage;<br>
our @EXPORT_OK = qw (<br>
blah<br>
);<br>
sub blah {...}<br>
1;<br>
<br>
<br>
use strict;<br>
use warnings;<br>
use <a class="moz-txt-link-freetext" href="Data::Dumper">Data::Dumper</a>;<br>
my $x;<br>
eval {<br>
$x = require "MyPackage.pm";<br>
}<br>
print Dumper($x);<br>
<br>
<br>
Running the test program will print:<br>
<br>
$VAR0 = 1;<br>
<br>
</tt></font><font size="+1"><tt>I had seen this:<br>
<br>
__PACKAGE__;<br>
</tt></font><font size="+1"><tt><br>
So I figured that we could </tt></font><font size="+1"><tt>the '1;' to:<br>
<br>
</tt></font><font size="+1"><tt> return bless({<br>
EXPORT_OK => [sort { uc($a) cmp uc ($b) } @EXPORT_OK],<br>
},__PACKAGE__);<br>
<br>
</tt></font><font size="+1"><tt>The new result is:<br>
<br>
$VAR0 = bless({<br>
EXPORT_OK => [<br>
blah<br>
]<br>
}, 'MyPackage');<br>
<br>
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.<br>
<br>
Other ideas include:<br>
- rather than simply blessing a returned result, we could create a
Contextual::Return object instead<br>
- we could return a reference to some private data<br>
- return a coderef to the initialisation function (as opposed to just
calling 'import')<br>
- return MyPackage's symbol<br>
<br>
cheers,<br>
Mathew</tt></font><br>
</body>
</html>