On 12/4/05, Ovid <[EMAIL PROTECTED]> wrote:
> "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.

    I think that the "ignore private" feature is less than useful.  If
one agrees with the convention of prepending private methods with an
underscore (which I personally do find useful), then one won't *call*
can() on such a method.  But since that convention is not enforced by
the language, what is the value of codifying it in a module?

    As to the imported-subroutines feature, that could break a lot of
code.  Here's a simple example:

   package Foo;
   use Exporter;
   use strict::can;
   if (Foo->can('import')); # BOOM

    I can provide other examples from my own code, but I think you get
the point.

    The module is clever, but it's not clear to me that it's
practically useful.  Could you provide a real-world example where this
would help to prevent or detect a programming error?

    - Kurt


> 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/
>

Reply via email to