Sub::Called - get information about how the subroutine is called
version 0.06
use Sub::Called;
sub test {
if( Sub::Called::with_ampersand() ){
print "you called this subroutine this way: &test\n",
"note that this disables prototypes!\n";
}
}
use Sub::Called 'already_called', 'not_called';
sub user {
unless (already_called) { # only gets called once
My::Fixtures::Users->load;
}
...
}
sub schema {
if ( not_called ) {
# setup schema
}
else {
return $schema;
}
}
There are no subroutines exported by default, but you can export all subroutines explicitly
use Sub::Called qw(with_ampersand already_called not_called);
This function must be called from inside a subroutine. It will return false if the subroutine has not yet been called. It will only return false once.
This subroutine is only exported on demand.
This function must be called from inside a subroutine. It returns the
opposite value of already_called
. Aside from this, there is no difference.
You may find aesthetically more pleasing.
This subroutine is only exported on demand.
This function must be called from inside a subroutine. It returns 1 if the subroutine
was called with an ampersand (e.g. &subroutine()
).
This subroutine is only exported on demand.
There are limitations and I don't know if I can solve these "problems". So this section is also named "TODO". If you know a solution for any of these limitations, please let me know.
It seems that there are some problems with subroutine references.
This may not work:
sub test2 {
if( Sub::Called::with_ampersand() ){
die "die hard";
}
};
my $sub2 = main->can( 'test2' );
&$sub2();
If you call subroutines in a module but outside any subroutine (so the subroutine calls are executed when the module is loaded), I cannot give a correct answer ;-)
package Check;
use strict;
use warnings;
use Sub::Called qw(with_ampersand);
&test;
sub test {
if( with_ampersand() ){
print "yada yada yada\n";
}
}
Renee Baecker, <module at renee-baecker.de>
Curtis "Ovid" Poe, <ovid at cpan.org>
An issue tracker and the code repository are available at http://github.com/reneeb/Sub-Called.
The distribution is contained in a Git repository, so simply clone the repository
$ git clone git://github.com/reneeb/Sub-Called.git
and change into the newly-created directory.
$ cd Sub-Called
The project uses Dist::Zilla
to
build the distribution, hence this will need to be installed before
continuing:
$ cpanm Dist::Zilla
To install the required prequisite packages, run the following set of commands:
$ dzil authordeps --missing | cpanm
$ dzil listdeps --author --missing | cpanm
The distribution can be tested like so:
$ dzil test
To run the full set of tests (including author and release-process tests),
add the --author
and --release
options:
$ dzil test --author --release
Renee Baecker reneeb@cpan.org
This software is Copyright (c) 2008 by Renee Baecker.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)