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

Stuff::Base - The right "use base"

SYNOPSIS

  # Load "MyBase1" and "MyBase2", add them to ISA and
  # import defs from them and their baases.
  use Stuff::Base 'MyBase1 MyBase2';
  
  # Load "Stuff::Object" and add it to ISA,
  # import defs from "Stuff::Object" and its baases.
  use Stuff::Base -Object;
  
  # Import "def" function.
  use Stuff::Base -def;
  
  # Relative bases.
  package Very::Long::Package::Name;
  use Stuff::Base '.Haha';   # same as: use Stuff::Base 'Very::Long::Package::Name::Haha';
  use Stuff::Base '..Haha';  # same as: use Stuff::Base 'Very::Long::Package::Haha';
  use Stuff::Base '...Haha'; # same as: use Stuff::Base 'Very::Long::Haha';
  # .. etc

FUNCTIONS

extend

  Stuff::Base::extend( $package, @base_packages );

Adds @base_packages to $package's ISA list and imports autoimported subs from @base_packages.

  use Stuff::Base @args;

is equvalent to

  BEGIN {
    Stuff::Base::extend( __PACKAGE__, @args );
  }

make_sub

  Stuff::Base::make_sub( $package, $name => $value );

Defines a autoexported function or constant in package $package that will be exported into child package with import_subs or extend (which calls import_subs).

Stuff::Base::make_sub adds package name (or object) the function was called from to arguments of created function when it's called.

  package MyPackage;
  use Stuff::Base -Object;
  
  # This is just an example, better use C<def fn => sub { print join ' ' => @_; }>.
  Stuff::Base::make_sub( __PACKAGE__, fn => sub { print join ' ' => @_; } );
  
  fn( 'test' ); => "MyPackage test";
  MyPackage->fn( 'test' ); => "MyPackage test";
  MyPackage->new->fn( 'test' ); => "MyPackage=HASH(...) test";

  package AnotherPackage;
  # use Stuff::Base 'MyPackage';
  BEGIN {
    Stuff::Base::extend( __PACKAGE__, 'MyPackage' );
    # or
    # Stuff::Base::import_subs( __PACKAGE__, 'MyPackage' );
  }
  
  fn( 'test' ); => "AnotherPackage test";
  AnotherPackage->fn( 'test' ); => "AnotherPackage test";

set_autoexported

  Stuff::Base::set_autoexported( $package, @names );

Marks @names in package $package as autoexported, so import_sub will import them in child class.

unset_autoexported

  Stuff::Base::unset_autoexported( $package, @names );

Marks @names in package $package as not autoexported, so import_sub will not import them in child class.

IMPORT

def

  use Stuff::Base -def;

This will add to caller "def" function. Which is a wrapper for Stuff::Base::make_sub.

  package My;
  use Stuff::Base -def;
  
  # The following two lines are equivalent:
  def a => sub { ... };
  Stuff::Base::make_sub( __PACKAGE__, sub { ... } );

SEE ALSO

Stuff

AUTHOR

Nikita Zubkov <nikzubkov@gmail.com>.