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::Closures_Demo - example with RE and no quoting

SYNOPSIS

        #!/usr/bin/perl -W -T
        use strict;
        
        use Package::Transporter sub{eval shift}, sub {
                $_[0]->register_potential('::Closures_Demo', 'FOR_ANY', 'calc_(\d+)');
        };
        
        sub calc {
                my ($correction, $a, $b) = @_;
                return($a * $correction/100 + $b);
        };
        
        package Other;
        use Package::Transporter sub{eval shift};
        
        print calc_5(7, 8), "\n"; # sets $correction = 5
        #my $result = calc_5 7, 8; # error

        exit(0);

DESCRIPTION

This generator is an example for two features: regular expressions for subroutine matching and how to avoid quoting when assembling the body text of a subroutine.

The RE 'calc_(\d+)' does what you might expect: a RE m// match.

The 'no quoting' feature is in the generator class and cited here:

                my $sub_text = ... q{
                        my ($fixed_argument) = (shift(@_));
                        ...
                }, ...
                ... $pkg->transport(\$sub_text, $fixed_argument);

This way you can transport information from the defining package or the generator into the requesting package. And vice versa. No serialization and no quoting required.

Set this package global to allow the generator to eventually trigger other generators. Nothing wrong with that, it just exists to highlight what to look for when writing generators.

$Package::Transporter::Generator::Closures_Demo::ONLY_DEFINED_ORIGINALS = 0;

ANYTHING ELSE

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