I must say as a premise that I'm not keen myself on indirect object notation, and I've at best seldom adopted it, if ever.

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:

# -*- 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__
Such that the do_cb method accepts and runs a callback. Now, if I use it like thus:
#!/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__
then everything is fine. But if I try
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.)


In reply to Speaking of indirect object notation... by blazar

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.