Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Can I add methods to an existing object?

by roubi (Hermit)
on Apr 02, 2009 at 20:33 UTC ( [id://755100]=note: print w/replies, xml ) Need Help??


in reply to Can I add methods to an existing object?

What type of restrictions are you dealing with here? There are various ways to accomplish this, but your post seems to imply that the simplest one (adding the methods to the file describing your object's class) is off-limit. A little more background would help narrow down the amount of hackery required in your case.
  • Comment on Re: Can I add methods to an existing object?

Replies are listed 'Best First'.
Re^2: Can I add methods to an existing object?
by pileofrogs (Priest) on Apr 02, 2009 at 22:10 UTC

    Right, sorry..

    I'm doing some Cat stuff and using Authen::Simple for authentication. My Cat instance has a Log::Dispatch logger which has methods like 'error','warning','debug' etc... Authen::Simple objects can take a logging object too, but it has to provide 'debug','info','warn' and 'error'. Log::Dispatch provides 'warning' but not 'warn'. To work around this, I made my own class which I called Log::Dispatch::Warn, which is a subclass of Log::Dispatch with a 'warn' sub which just calls 'warning'.

    It seems kind of silly to create a whole module just to make an object call $self->warning(@_) if someone calls $object->warn($blah).

      First, I thought of something like this:

      *Log::Dispatch::warn = *Log::Dispatch::warning;

      But I thought, that it doesn't look nice ;) so I changed it to:

      *Log::Dispatch::warn = \&Log::Dispatch::warning;

      But both threw warnings that Log::Dispatch::warn is "used only once: possible typo..."; And then, based upon the answers from morgon and mr_mischief I thought of

      sub Log::Dispatch::warn { my $self = shift; $self->warning(@_); }

      tested with:

      #!/usr/bin/perl -l use strict; use warnings; package foo; sub new { bless {}, shift(); } sub foo { my $self = shift; print "hello @_"; } package main; sub foo::bar { my ( $self ) = shift; $self->foo(@_); } *foo::baz = \&foo::foo; *foo::buzz = *foo::foo; my $o = foo->new(); $o->bar('world'); $o->baz('world'); $o->buzz('world'); __END__
        But both threw warnings that Log::Dispatch::warn is "used only once: possible typo...";
        And both are the Right Way to alias a function. Warnings are hints, not laws.
      It'd be interesting to hear from autarch himself why 'warn' wasn't added to the list of valid methods of Log::Dispatch when various other abbreviations like 'err' and 'crit' and 'emer' have been. Maybe he is just waiting to hear from someone like you? :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://755100]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found