The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Package::Transporter::Generator::Potential::Homonymous_Tie - Subroutines from a DB file of the same name as the package

SYNOPSIS

        use strict;
        Package::Transporter::again sub{eval shift}, sub {
                $_[0]->register_potential('::Homonymous_Tie', 'FOR_SELF', undef);
        };
        
        yn(potentially_defined('hello_worlds'));
        yn(potentially_defined('hello_world'));
        yn(defined(&hello_world));

        hello_world();

        yn(defined(&hello_world));
        exit(0);
        

DESCRIPTION

The generator can be used to keep the subroutines of a module in a DB file. In case your module has a huge number of subroutines, out of which only a few are typically used, keeping the subroutines in an external source might conserve time and memory.

Homonymous means the same name. This generator uses the subroutine name as a key to a DB-file with the same base name as the package file. Example:

        Package: The::Requesting::Package
        Package File: /opt/perl5/5.10.0/The/Requesting/Package.pm

        DB File: /opt/perl5/5.10.0/The/Requesting/Package.(pag|dir);

The generator extracts all keys from the DB-file and uses that for rule matches. Newly added keys will only be seen on the next start of the program.

When running under -c ('syntax check only'), the module will load all definitions from the homonymous db file. This reduces the chance of unpleasant surprises.

Prototypes

Prototype definitions are supported, but have to be explicitely requested. It only makes sense if defining and requesting package are the same (therefore the explicit request). Beside that, the correct prototype might not be known at compile time. However, for AUTOLOADed subroutines the prototype is not checked in Perl5, so for the moment prototypes are pretty much useless.

The prototypes are given by appending the string '-prototype' to the key. See 'hello_world-prototype' below.

        Package::Transporter::again sub{eval shift}, sub {
                my $generator = Package::Transporter::Generator::Potential::Homonymous_Tie->new($_[0]);
                $generator->prototypes($_[0]);
                $_[0]->register_potential($generator, 'FOR_SELF', $generator->matcher($_[0]));
        }
        

Initial Creation

The following perl code was used to set up the main.dbm for the example.

        # this is now main.dbm was created:
        use GDBM_File;
        use Fcntl;
        tie(my %sub_bodies, 'GDBM_File', __PACKAGE__.'.dbm',
                O_RDWR|O_CREAT, 0644);
        $sub_bodies{'yn'} = q{print STDOUT ($_[0] ? 'Yes' : 'No'), "\n";};
        $sub_bodies{'hello_world'} = q{print "Hello World.\n";};
        $sub_bodies{'hello_world-prototype'} = q{};
        $sub_bodies{'hola_mundo'} = q{print "Hola Mundo.\n";};
        $sub_bodies{'salut_monde'} = q{print "Salut Monde.\n";};
        

ANYTHING ELSE

Please see the documentation of the upstream package Package::Transporter.