NAME
Package::Transporter::Generator::Potential::Hello_Anything - Full example from synopsis
SYNOPSIS
The following code registers two rules for use by AUTOLOAD. It demonstrates the tasks you can address with Package::Transporter: consume rules and define rules for automatic subroutine generation. All yes-no-tests are written to print 'Yes'.
package Synopsis;
use Package::Transporter sub{eval shift}, sub {
$_[0]->register_potential('::Export', 'FOR_BRANCH', ['hello_world']);
$_[0]->register_potential('::Hello_Anything', 'FOR_SELF', 'salut_');
};
sub hello_world { print "Hello World.\n"; }
package Synopsis::Desc1;
use Package::Transporter sub{eval shift};
yn(!defined(&hello_world));
yn(potentially_defined('hello_world'));
hello_world(); # first rule
yn(defined(&hello_world));
package sisponyS::Desc2;
our @ISA = ('Synopsis');
use Package::Transporter sub{eval shift};
my $obj = bless( \(my $o = 0), 'sisponyS::Desc2');
yn(!potentially_defined('hello_world')); # name is sisponyS::
yn(!defined(&salut_monde));
yn(!potentially_defined('salut_monde'));
yn($obj->potentially_can('salut_monde')); # no autovivification
yn($obj->can('salut_monde')); # with autovivification
$obj->salut_monde(); # second rule
yn(!defined(&sisponyS::Desc2::salut_monde));
yn(!defined(&Synopsis::Desc1::salut_monde));
yn(defined(&Synopsis::salut_monde));
package main;
use Package::Transporter sub{eval shift}, sub {
my $yn = q{
return(q{print STDOUT ($_[0] ? 'Yes' : 'No'), "\n";});
};
$_[0]->register_potential($yn, 'FOR_ANY', 'yn');
};
exit(0);
DESCRIPTION
This generator class was only designed to provide an example for the synopsis of Package::Transporter. Some boolean tests have been added for this version to document the behaviour in detail. All yn-tests should return 'Yes'.
The dynamically generated subroutine salut_monde prints Hello plus the second part of its name to the screen. The impartial translation is intentional to stress the key point - this package is not about localisation.
The last paragraph of tests should remind you that the method salut_monde springs into existence as a subroutine in the parent class, because it is defined there only.
ANYTHING ELSE
Please see the documentation of the upstream package Package::Transporter.