blazar has asked for the wisdom of the Perl Monks concerning the following question:
However, it occured to me a case in which for some reason it would have seemed natural for me to use it. Trimmed down to an absolute minimum suppose I have a class Foo:
Such that the do_cb method accepts and runs a callback. Now, if I use it like thus:# -*- Perl -*- package Foo; use strict; use warnings; sub new { bless [], shift } sub warn { shift; warn "[", __PACKAGE__, "] ", @_ } sub do_cb { my ($self, $cb)=@_; $self->$cb; } 1; __END__
then everything is fine. But if I try#!/usr/bin/perl use strict; use warnings; use Foo; my $f=Foo->new; $f->do_cb(sub { my $caller=shift; $caller->warn("no good for me\n"); }); __END__
warn $caller "no good for me\n";
instead, it doesn't work due to warn() being a builtin function. Incidentally, the latter form seems quite expressive to me in terms of message passing to the "caller" object.
So leaving aside for the moment any argument about how {risky,dangerous,error-prone,whatever} this could be (for I'm aware about them - and please do not point out I can call it "Warn" either, for I know that too), is it first of all possible, some way or another? (Especially given that in perl I can even replace most builtins with my own subs.)
UPDATE: Fixed a "minor but relevant" bug! (Thanks to Fletch.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Speaking of indirect object notation...
by Fletch (Bishop) on Apr 21, 2005 at 15:10 UTC | |
by blazar (Canon) on Apr 21, 2005 at 16:37 UTC | |
|
Re: Speaking of indirect object notation...
by dragonchild (Archbishop) on Apr 21, 2005 at 15:09 UTC | |
by blazar (Canon) on Apr 21, 2005 at 16:31 UTC | |
by dragonchild (Archbishop) on Apr 21, 2005 at 16:34 UTC | |
|
Re: Speaking of indirect object notation...
by Thelonious (Scribe) on Apr 22, 2005 at 01:17 UTC | |
by benizi (Hermit) on Apr 22, 2005 at 21:09 UTC | |
|
Re: Speaking of indirect object notation...
by Anonymous Monk on Apr 21, 2005 at 16:13 UTC |