"strict::can" is a module which, if used, imports a "can" method into
your namespace. This version of can() will not report imported helper
functions or "private" methods. Full docs below.
Comments and suggestions welcome.
Cheers,
Ovid
--
NAME
strict::can - Make can() ignore private methods and imported
subroutines
VERSION
Version 0.01
SYNOPSIS
package Foo;
use strict::can;
use HTML::Entities qw(encode_entities); # imported into Foo::
sub new { bless {}, shift }
sub _private { 'some private function' }
# later
Foo->can('new'); # true
Foo->can('encode_entities'); # false (because it's an imported
function)
Foo->can('_private'); # false (because it's a private
function, even
# though we can still call
it)
EXPORT
This module exports a "can()" method into your namespace.
DESCRIPTION
This module gives your classes a "can()" method which overrides
(not
replaces) "UNIVERSAL::can". In most respects it behaves like you
expect
it should, but does not report private methods (see below) or
imported
functions.
Imported functions
Frequently we import helper functions into our namespace.
Ordinarily
"can()" will return the code reference for these functions even
though
this is probably not what we want. "strict::can" will return false
for
any imported functions/methods. If you want to expose them, write
wrapper methods for them.
{
package Foo;
use strict::can;
use HTML::Entities qw/encode_entities/; # import into Foo::
...
sub encode {
my ($self, @args) = @_;
return encode_entities(@args);
}
}
# later
if (Foo->can('encode_entities')) {
# never gets to here
}
Private methods
Methods which begin with an underscore are, by convention,
considered
"private" and should not be relied on. Unfortunately, "can()"
succeeds
with those, too. "strict::can" will return false for private
methods
unless the class checking "can()" is a *subclass* of the class or
instance it is checking. This is because we're allowing a little
more
trust for subclasses as "private" methods are often "protected"
methods
which subclasses should be allowed to use.
{
package Foo;
use strict::can;
...
sub _private {
return 'Top Sekret!';
}
}
# later
{
package main;
if (Foo->can('_private')) {
# never gets to here
}
}
{
package Foo::Bar;
our @ISA = qw(Foo);
if (Foo->can('_private')) {
# success at last!
}
}
AUTHOR
Curtis "Ovid" Poe, "<[EMAIL PROTECTED]>"
BUGS
Please report any bugs or feature requests to
"[EMAIL PROTECTED]", or through the web interface at
<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=strict-can>. I will
be
notified, and then you'll automatically be notified of progress on
your
bug as I make changes.
SEE ALSO
The "B::svref_2object" function.
COPYRIGHT & LICENSE
Copyright 2005 Curtis "Ovid" Poe, all rights reserved.
This program is free software; you can redistribute it and/or
modify it
under the same terms as Perl itself.
--
If this message is a response to a question on a mailing list, please send
follow up questions to the list.
Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/