[Pdx-pm] Austin's $VERSION trick

Eric Wilhelm ewilhelm at sbcglobal.net
Fri Jul 15 15:51:12 PDT 2005


# from Michael G Schwern
# on Thursday 14 July 2005 04:30 pm:

>  our $VERSION = require Hello::World::Version;
>
>This takes advantage of the fact that require returns the last
> evaluated expression of the file being required (a documented but
> little known feature).
>
>Unfortunately, this only happens the *first* time something is
> required. The second time it just returns 1 since the file has
> already been loaded.

Wasn't the point of this trick to make the CPAN system happy?  I guess 
it could bite you if you expected to rely on it for something within 
the running code.  Anyway, this seems to work:

our $VERSION = $Hello::World::Version || require(Hello::World::Version);


#############
/tmp/lib$ tree
.
`-- Hello
    |-- World
    |   `-- Version.pm
    `-- World.pm

2 directories, 2 files

/tmp/lib$ cat Hello/World.pm
package Hello::World;

use Hello::World::Version;
our $VERSION = $Hello::World::Version || require(Hello::World::Version);

print "version: $VERSION\n";


/tmp/lib$ cat Hello/World/Version.pm
package Hello::World;
our $Version = '0.76_02';
return($Version);


/tmp/lib$ PERL5LIB=/tmp/lib/ perl  -e 'use Hello::World;'
version: 0.76_02

#############

Comment the 'use' out of World.pm to check.

--Eric
-- 
The opinions expressed in this e-mail were randomly generated by
the computer and do not necessarily reflect the views of its owner.
--Management
---------------------------------------------
    http://scratchcomputing.com
---------------------------------------------


More information about the Pdx-pm-list mailing list