Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Reopen a class, add methods ?

by ikegami (Patriarch)
on May 13, 2007 at 01:58 UTC ( [id://615135] : note . print w/replies, xml ) Need Help??


in reply to Re: Reopen a class, add methods ?
in thread Reopen a class, add methods ?

There's no real difference between the two solutions you provide,

The difference is real enough. One of the variants has multiple non-obvious modes of failure.

First of all, the difference is that sub Package::func { ... } compiles the function in the current package and places it in the target package, whereas { package Package; sub func { ... } } compiles the function in the target package. There are a few issues with using sub Package::func { ... }:

It causes a problem when Carp is used. Carp will treat the function as belonging in the current package (main in this case), while it should treat it as being part of the target package to properly identify the caller.

It causes a problem when the function imports symbols. The symbols will be placed in the current pacakge while they should be placed in the target package.

It causes a problem when bless is used with one argument in the function. It will bless objects into the current package while they should be blessed into the target package.

It causes __PACKAGE__ to be the name of the current package instead of the name of the target package.