I have a class, and I want to be able supply it with a debug sub for printing debug messages, on a per-object basis.

My first implementation looks something like this:

package test; use strict; use warnings; sub new { my ($class, %params) = @_; my $self = { debug => sub {}, %params }; bless $self, $class; return $self; } sub method1 { my ($self) = @_; $self->{'debug'}->("Entering method1"); } package main; use strict; use warnings; my $t1 = test->new( debug => sub { print STDERR "debug: @_\n"; } ); my $t2 = test->new( debug => sub { print STDERR "DEBUG: @_\n"; } ); $t1->method1; $t2->method1;

This works fine, but I'm rather lazy and would prefer to avoid the long $self->{'debug'}->("text") invocation for each debug message. I'd prefer to be able to just say debug("text") and have it DWIM.

So I changed the code to this:

package test; use strict; use warnings; our $self; sub debug { $self->{'debug'}->(@_); } sub new { my ($class, %params) = @_; my $self = { debug => sub {}, %params }; bless $self, $class; return $self; } sub method1 { local ($self) = @_; debug "Entering method1"; } package main; use strict; use warnings; my $t1 = test->new( debug => sub { print STDERR "debug: @_\n"; } ); my $t2 = test->new( debug => sub { print STDERR "DEBUG: @_\n"; } ); $t1->method1; $t2->method1;

Which seems to do the trick.

So now I have two questions:

  1. What are the downsides to doing it this way? One obvious issue is that the passed subroutine can now access and modify $self. I suspect there may be some issues once inheritance comes into play, but I haven't done any testing on that yet.
  2. Is there a better way to do this? (or should I just forget about the whole thing and use the long invocation)


In reply to Short invocation for per-object methods by Crackers2

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.