Exporter-Extensible

 view release on metacpan or  search on metacpan

lib/Exporter/Extensible.pm  view on Meta::CPAN

	'$' => [ '_generateSCALAR_', '_generateScalar_' ],
	'@' => [ '_generateARRAY_', '_generateArray_' ],
	'%' => [ '_generateHASH_', '_generateHash_' ],
	'*' => [ '_generateGLOB_', '_generateGlob_' ],
	'&' => [ '_generate_', '_generateCODE_', '_generateCode' ],
);
$sigil_to_generator_prefix{''}= $sigil_to_generator_prefix{'&'};
our %ord_is_sigil= ( ord '$', 1, ord '@', 1, ord '%', 1, ord '*', 1, ord '&', 1, ord '-', 1, ord ':', 1 );
our %ord_is_directive= ( ord '-', 1, ord ':', 1 );

my ($carp, $croak, $weaken, $colon, $hyphen);
$carp=   sub { require Carp; $carp= \&Carp::carp; goto $carp; };
$croak=  sub { require Carp; $croak= \&Carp::croak; goto $croak; };
$weaken= sub { require Scalar::Util; $weaken= \&Scalar::Util::weaken; goto $weaken; };
$colon= ord ':';
$hyphen= ord '-';

sub import {
	my $self= shift;
	# Can be called as class method or instance method
	$self= bless { into => scalar caller }, $self
		unless ref $self;
	# Optional config hash might be given as first argument
	$self->exporter_apply_global_config(shift)

lib/Exporter/Extensible.pm  view on Meta::CPAN

	for my $i (reverse 1..$#flat_install) {
		if (ref $flat_install[$i] eq 'HASH') {
			splice @flat_install, $i-1, 2, map +($flat_install[$i-1] => $_), values %{$flat_install[$i]};
		}
	}
	# Then pass that list to the installer (or uninstaller)
	$self->$method(\@flat_install);
	# If scope requested, create the scope-guard object
	if (my $scope= $self->{scope}) {
		$$scope= bless [ $self, \@flat_install ], 'Exporter::Extensible::UnimportScopeGuard';
		$weaken->($self->{scope});
	}
	# It's entirely likely that a generator might curry $self inside the sub it generated.
	# So, we end up with a circular reference if we're holding onto the set of all things we
	# exported.  Clear the set.
	%$install= ();
	1;
}

sub _exporter_build_install_set {
	my ($self, $todo)= @_;

t/05-scope.t  view on Meta::CPAN

#! /usr/bin/env perl
use strict;
use warnings;
no warnings 'once';
use Test::More;
use Scalar::Util 'weaken';

use_ok( 'Exporter::Extensible' ) or BAIL_OUT;

ok( eval q{
	package Example;
	$INC{'Example.pm'}=1;

	use Exporter::Extensible -exporter_setup => 0;
	our ($scalar, @array, %hash);
	sub code { 1 }

t/05-scope.t  view on Meta::CPAN

	}
	1;
}, 'declare Example' ) or diag $@;

Example->import({ into => 'CleanNamespace1', scope => \my $scope }, ':group1');
# use eval to prevent instantiating vars at compile time
is( eval '*CleanNamespace1::array{ARRAY}', *Example::array{ARRAY}, 'Exported to package' );

# the $scope holds a reference to the blessed importer.  This isn't part of the API,
# but reach in and grab it in order to test that the objects actually got cleaned up.
weaken( my $exporter_instance= $scope->[0] );

# $scope should be the only reference to that scope object, so weakening it should
# garbage collect it, and unimport the symbols.
weaken($scope);
is( $scope, undef, 'Weakened scope got garbage collected' );
is( $exporter_instance, undef, 'Exporter instance also got garbage collected' );
is( eval '*CleanNamespace1::array{ARRAY}', undef, 'Exported symbols were removed' );

done_testing;

t/06-prefix-suffix-as.t  view on Meta::CPAN

#! /usr/bin/env perl
use strict;
use warnings;
no warnings 'once';
use Test::More;
use Scalar::Util 'weaken';

use_ok( 'Exporter::Extensible' ) or BAIL_OUT;

ok( eval q{
	package Example;
	$INC{'Example.pm'}=1;

	use Exporter::Extensible -exporter_setup => 0;
	our ($scalar, @array, %hash);
	sub code { 1 }

t/07-not.t  view on Meta::CPAN

#! /usr/bin/env perl
use strict;
use warnings;
no warnings 'once';
use Test::More;
use Scalar::Util 'weaken';

use_ok( 'Exporter::Extensible' ) or BAIL_OUT;

ok( eval q{
	package Example;
	$INC{'Example.pm'}=1;

	use Exporter::Extensible -exporter_setup => 1;
	export(qw( stat log log_debug epilogue ));
	sub stat {}

t/08-replace.t  view on Meta::CPAN

#! /usr/bin/env perl
use strict;
use warnings;
no warnings 'once', 'redefine';
use Test::More;
use Scalar::Util 'weaken';

use_ok( 'Exporter::Extensible' ) or BAIL_OUT;

ok( eval q{
	package Example;
	$INC{'Example.pm'}=1;

	use Exporter::Extensible -exporter_setup => 1;
	our %EXPORT= ( alpha => \&alpha, beta => \&beta, gamma => \&gamma, delta => \&delta );
	sub alpha { 'a' }

t/09-generators.t  view on Meta::CPAN

#! /usr/bin/env perl
use strict;
use warnings;
no warnings 'once', 'redefine';
use Test::More;
use Scalar::Util 'weaken';

use_ok( 'Exporter::Extensible' ) or BAIL_OUT;

ok( eval q{
	package Example;
	$INC{'Example.pm'}=1;

	use Exporter::Extensible -exporter_setup => 1;
	our %EXPORT= ( alpha => \ \ "alpha", beta => \ \ &beta, '@gamma' => &gamma, '*zeta' => '_generateGlob_zeta' );
	our %EXPORT_TAGS= ( delta => \ "delta" );

t/10-options.t  view on Meta::CPAN

#! /usr/bin/env perl
use strict;
use warnings;
no warnings 'once', 'redefine';
use Test::More;
use Scalar::Util 'weaken';

use_ok( 'Exporter::Extensible' ) or BAIL_OUT;

my @events;
ok( eval q{
	package Example;
	$INC{'Example.pm'}=1;

	use Exporter::Extensible -exporter_setup => 1;
	our %EXPORT= (

t/20-export-dwim.t  view on Meta::CPAN

#! /usr/bin/env perl
use strict;
use warnings;
no warnings 'once', 'redefine';
use Test::More;
use Scalar::Util 'weaken';

use_ok( 'Exporter::Extensible' ) or BAIL_OUT;
ok( eval q{
	package Example;
	use Exporter::Extensible -exporter_setup => 1;
	
	sub alpha { 'a' }
	sub beta  { 'b' }
	sub gamma { 'g' }
	sub delta { 'd' }

t/21-export-attribute.t  view on Meta::CPAN

#! /usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Scalar::Util 'weaken';

use_ok( 'Exporter::Extensible' ) or BAIL_OUT;

my @tests= (
	[ 'export a sub',
		'sub alpha :Export {}',
		q{ is_deeply(\%Example::EXPORT, { alpha => \&Example::alpha }) }
	],
	[ 'export a sub in groups',
		'sub alpha :Export( :group1 ) {}',



( run in 0.418 second using v1.01-cache-2.11-cpan-1f129e94a17 )