Extract the exported variables from a Perl module

This Perl program extracts a list of exported variables from a given module and puts their values into a hash. It assumes that it is being run in the home directory of the module, that the module's pm file is within the "lib" directory of that home directory, and that the module has an export tag ":all" which exports all the variables.

#!/home/ben/software/install/bin/perl
use warnings;
use strict;

=head2 extract_vars

    extract_vars ('Moby', \%vars);

Extract all the exported variables from the specified module, so if
Moby exports C<$data_dir> then 

    $vars{data_dir} = $Moby::data_dir

This examines C<@EXPORT_OK> in Moby to get the list of variables, and
also evaluates Moby. It assumes that it is being run from a
F<make-pod.pl> which is situated such that Moby is in F<lib/Moby.pm>.

Thus this is strongly dependent on a specific file layout.

=cut

sub extract_vars
{
    my ($module, $vars) = @_;
    eval "use lib \"./lib\";use $module ':all';";
    my @exports = eval "\@${module}::EXPORT_OK";
    for my $var (@exports) {
        if ($var =~ /\$(.*)/) {
            my $nodollar = $1;
            $vars->{$nodollar} = eval "\$$module::$nodollar";
        }
    }
}

(download)


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer