in reply to Re: Re: Re: Re: Why breaking can() is acceptable
in thread Why breaking can() is acceptable
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 beIf 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.Larryuse Shell qw(echo cat ps cp);
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
Although I could not test it under inheritance because I could not get my class to be inheritiedpackage 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;
The output this produces is:#!/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;
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Re: Re: Why breaking can() is acceptable
by tilly (Archbishop) on Apr 06, 2004 at 16:22 UTC | |
by simonm (Vicar) on Apr 06, 2004 at 18:43 UTC | |
by tilly (Archbishop) on Apr 06, 2004 at 19:01 UTC | |
by stvn (Monsignor) on Apr 06, 2004 at 19:24 UTC | |
by tilly (Archbishop) on Apr 06, 2004 at 21:34 UTC | |
by stvn (Monsignor) on Apr 06, 2004 at 22:03 UTC |