NAME

Package::CopyFrom - Copy (some) contents from another package

VERSION

This document describes version 0.001 of Package::CopyFrom (from Perl distribution Package-CopyFrom), released on 2020-02-15.

SYNOPSIS

package My::Package;
use Package::CopyFrom; # exports copy_from()

copy_from 'Your::Package';

DESCRIPTION

This module provides "copy_from" to fill the contents of the specifed (source) package into the caller's (target) package, with some options. copy_from can be used for reuse purpose, as a "poor man"'s "non-inheritance" OO: you copy routines (as well as package variables) from another "base" package then add/modify some.

FUNCTIONS

copy_from

Usage:

copy_from [ \%opts, ] $source_package

Load module $source_package if not already loaded (unless the load option is set to false), then copy the contents of package into the caller's package. Currently only subroutines, scalars, arrays, and hashes are copied.

Options:

  • overwrite

    Boolean, default false. When this setting is false, if a symbol (variable/subroutine) already exists in the target package, it will not be overwritten. Setting this option to true will overwrite.

    See "GOTCHAS".

  • load

    Boolean, default true. If set to false, no attempt to load module named $source_package is made.

  • 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'];
  • 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.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Package-CopyFrom.

SOURCE

Source repository is at https://github.com/perlancar/perl-Package-CopyFrom.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Package-CopyFrom

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.

GOTCHAS

During parsing (compile-time), whenever a variable is mentioned (even if the corresponding statement never gets executed) it will spring into existence. If you do copy_from during run-time, you will miss copying the mentioned variable unintendedly. Consider this example:

# in lib/Cwd2.pm
package Cwd2;
use Package::CopyFrom;
copy_from 'Cwd';

# in main.pl
require Cwd2;

say @Cwd2::EXPORT if 0;

# Cwd2's @EXPORT will not be copied from Cwd's @EXPORT because Cwd2's @EXPORT
# springs into existence during compile-time due to the above statement.

SEE ALSO

Package::Rename can also copy packages.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

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.