Package::CopyContents - Copy (some) contents from another package
This document describes version 0.004 of Package::CopyContents (from Perl distribution Package-CopyContents), released on 2020-02-16.
package My::Package; use Package::CopyContents; # exports copy_contents_from() BEGIN { copy_contents_from 'Your::Package' } # provide your own variables/subroutines our $scalar = 'foo'; sub func1 { ... } ... 1;
This module provides "copy_contents_from" to fill the contents of the caller's (target) package from the specified (source) package, with some options. copy_contents_from can be used as an alternative to OO inheritance: you copy routines from another "base" package then add/modify some other.
copy_contents_from
Usage:
copy_contents_from [ \%opts, ] $source_package
Load module $source_package if not already loaded (unless the load option is set to false, in which case no module loading is done), then copy the contents of the package into the caller's package. Currently only coderefs, scalars, arrays, and hashes are copied.
$source_package
load
Options:
to
String. Default to copy_contents_from's caller package. Can be used to explicitly set the target package.
Boolean, default true. If set to false, no attempt to load module named $source_package is made.
dclone
Boolean, default false. By default, only shallow copying of arrays and hashes are done. If this option is true, Storable's dclone is used.
skip_sub
Boolean, default false. Whether to exclude all subs.
skip_scalar
Boolean. Whether to exclude all scalar variables.
skip_array
Boolean, default false. Whether to exclude all array variables.
skip_hash
Boolean, default false. Whether to exclude all hash variables.
exclude
Arrayref. List of names to exclude.
Examples:
exclude => ['@EXPORT', '@EXPORT_OK', '%EXPORT_TAGS', '$VERSION'];
Please visit the project's homepage at https://metacpan.org/release/Package-CopyContents.
Source repository is at https://github.com/perlancar/perl-Package-CopyContents.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Package-CopyContents
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
If your copying module is loaded by user during runtime instead of compile-time, then subroutine name from your module will be overwritten by the runtime copy_contents_from invocation. Illustration:
# in Source.pm package Source; sub func1 { ... } sub func2 { print "Source's version" } 1; # in YourModule.pm package YourModule; use Package::CopyContents; copy_contents_from 'Source'; # modify func2 sub func2 { "YourModule's version" } 1; # in script1.pl use YourModule; YourModule::func2(); # prints "YourModule's version", ok. # in script2.pl require YourModule; YourModule::func2(); # prints "Source's version"!
To ensure that your subroutines do not get copied (overwritten) by the source package's that have the same name, perform the copying at compile-time:
# in YourModule.pm package YourModule; BEGIN { use Package::CopyContents; copy_contents_from 'Source' } # modify func2 sub func2 { "YourModule's version" } 1;
Package::Rename can also copy packages.
Variants: Perinci::Package::CopyContents
perlancar <perlancar@cpan.org>
This software is copyright (c) 2020 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Package::CopyContents, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Package::CopyContents
CPAN shell
perl -MCPAN -e shell install Package::CopyContents
For more information on module installation, please visit the detailed CPAN module installation guide.