Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: Can I add methods to an existing object?

by linuxer (Curate)
on Apr 02, 2009 at 23:55 UTC ( [id://755151]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Can I add methods to an existing object?
in thread Can I add methods to an existing object?

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__

Replies are listed 'Best First'.
Re^4: Can I add methods to an existing object?
by educated_foo (Vicar) on Apr 03, 2009 at 13:18 UTC
    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.
      And both are the Right Way to alias a function. Warnings are hints, not laws.

      I didn't write, that it's a wrong way. And I didn't write about warnings being laws. I like to get rid of warnings without writing no warnings;, so I mentioned another way which doesn't show a warning.

      Is it bad to look for ways to avoid warnings without writing no warnings;?

      sub foo::bar { my ( $self ) = shift; $self->foo(@_); }

      So, is that so much worse than aliasing the function?

      All in all, I I don't know how to handle your answer.
      What's your intention?
      Just information, that the aliasing was done right?
      Should I consider something else?

      Thanks in advance for your explanation.

        Is it bad to look for ways to avoid warnings without writing "no warnings?"
        Yes, when the only point of looking is to avoid warnings. When you find yourself thinking "I know what this code is doing, and I want it to do that, but 'use warnings' bitches at me," you should avoid "use warnings."

        In addition to what educated_foo was saying: You can scope and limit the warnings too and generally should even if you know why you're turning them off to prevent stupid mistakes or assumptions which might bury bugs.

        { # some scoping block/sub no warnings "once"; # one time whatever... }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-20 06:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found