Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Shell is a perfect example of how AUTOLOAD should be used. And looking over it, I cannot see a good way to write can for it since it really just executes on the shell anything you give it (whoa security risk!). However if you specified your available shell commands, then you could make a reasonable can with no trouble at all. Even Larry seemed to think maybe they were playing with fire (quoted from here)

That's maybe too gonzo. It actually exports an AUTOLOAD to the current package (and uncovered a bug in Beta 3, by the way). Maybe the usual usage should be
use Shell qw(echo cat ps cp);
Larry
If we were to follow Larry's suggestion, the implementation of can becomes very simple to implement as we know what methods/functions are being created/AUTOLOADed.
Note that when I've chosen to use AUTOLOAD in the past, it tends to be short.
I have used AUTOLOAD on occasion too, but only when I really need to (and just as with you) it tends to be short. An example is in Class::Trait here. I am not sure if my usage breaks can, but you can be sure I will test it in the next release.

As for Class::FlyweightWrapper, here is your can

package Class::FlyweightWrapper; $VERSION = 0.01; use strict; use Carp; my $BASE_PACKAGE = <<'EOT'; # line 1 "'Flyweight wrapper PUBLIC for PRIVATE'" package PUBLIC; my %object = qw(PUBLIC PRIVATE); sub DESTROY { delete $object{$_[0]}; } sub AUTOLOAD { my $meth = $PUBLIC::AUTOLOAD; $meth =~ s/.*:://; my $self = $object{shift(@_)}; return $self->$meth(@_); } sub can { my $self = $object{shift(@_)}; my ($method_name) = @_; return $self->can($method_name); } # Make sure things cleanup properly END { %object = (); } EOT my $BASIC_CONSTRUCTOR = <<'EOT'; sub CONSTRUCTOR { my $self = bless \ my $scalar, "PUBLIC"; my $class = shift; $object{$self} = $object{$class}->CONSTRUCTOR(@_); $self; } EOT sub import { shift; # Not interested in my package my $public = shift || croak("Usage: use Class::FlyweightWrapper 'Public::Package';"); my $private = caller(); my @constructors = @_ ? @_ : 'new'; my $template = $BASE_PACKAGE; $template =~ s/PUBLIC/$public/g; $template =~ s/PRIVATE/$private/g; foreach (@constructors) { my $piece = $BASIC_CONSTRUCTOR; $piece =~ s/CONSTRUCTOR/$_/g; $piece =~ s/PUBLIC/$public/g; $piece =~ s/PRIVATE/$private/g; $template .= $piece; } eval $template; if ($@) { confess("Template\n$template\ngave error $@"); } } 1;
Although I could not test it under inheritance because I could not get my class to be inheritied
#!/usr/bin/perl package Test::Private; use Class::FlyweightWrapper "Test::Public"; sub new { return bless {}, ref($_[0]) || $_[0]; } sub helloWorld { print "Hello World!\n"; } package DerivedTest; @DerivedTest::ISA = qw(Test::Public); package main; my $test = Test::Public->new(); $test->helloWorld(); print (($test->can("helloWorld")) ? "we can\n" : "we can't\n"); my $test2 = DerivedTest->new(); # <<< dies here $test2->helloWorld(); print (($test2->can("helloWorld")) ? "we can\n" : "we can't\n"); 1;
The output this produces is:
Hello World! we can Can't call method "new" on an undefined value at 'Flyweight wrapper Te +st::Public for Test::Private' line 24.

Regarding Re (tilly) 1: Nested Classes, a can like addition to your object system is easily enough to implement since you have all the methods defined in a hash already. As this is an example of creating your own object system, I wont bother trying to think of how a version of can might fair in other situations (inheritance, multiple-inheritance, etc), since creating your own object system is hairy enough, and your implementation is incomplete.

As for Re (tilly) 1: Reverse Inheritance Irritance, you are just dispatching your calls to the parent object, so it makes sense you can dispatch requests to can as well. Like I said, it may not be easy, but I think in many cases it is worth it, you may not agree with that, and that is your choice.

-stvn

In reply to Re: Re: Re: Re: Re: Why breaking can() is acceptable by stvn
in thread Why breaking can() is acceptable by tilly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-16 22:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found