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

You can also declare that as:

sub Hubba::bubba { print "bubba" }

which looks more intuitive to me. BTW, is the "Bubba" capitalized in the name of the bubble gum brand?

Replies are listed 'Best First'.
Re^3: Can I add methods to an existing object?
by morgon (Priest) on Apr 03, 2009 at 11:47 UTC
    This is not quite the same thing.

    Your approach declares the bubba-method at compile-time, my example at run-time.

    You could not use your approach e.g. in a context where the name of the method to be added in not known at compile-time (e.g. is entered by the user).

    And "hubba" and "bubba" are my just my personal preference for generic names that I use where others may use "foo" and "bar" that others prefer - I can't remember where I got that from initially but it has nothing to do with bubble gum :-)

      pileofrogs didn't specify that it needs to be done at run-time. It would be my preference to resolve such issues at compile-time unless there is some feature of the final product that required deferral of the sub declaration until run-time.

      It is good to make the difference clear, I guess, just in case the difference really matters to someone looking for a way to add a class method from outside the package scope. That's not really the question that was asked, since pileofrogs asked about adding a method to an object. In many cases adding a method to the class is just as good as adding it to the object instance, so no harm done from that little difference. It is, BTW, easy to add a non-method subroutine to the object's data structure when using hash-based objects, but that could create more confusion than it solves.

      Just for kicks, I'll give you a link to Hubba Bubba®. It may not be where you got the words, but anyone who grew up chewing this Wrigley® product will recognize the name on sight.