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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|