dcvr69 has asked for the wisdom of the Perl Monks concerning the following question:

Ok. I've been looking (perldoc/google/super search) for the right way to do this - right in the sense that I don't have to disable warnings to prevent the subroutine redefined messages.

I've reduced the problem to a very mininal test case:

package X; sub x { 1 } package X::Y; use base 'X'; 1;

This produces the following warning:

$ perl -cw X.pm Subroutine x redefined at /data/web/lib/X.pm line 3. X.pm syntax OK

Now, I (think I) understand why it's giving this error - because it's trying to use the base package when it hasn't actually finished loading the file the base package is in. Splitting the X::Y package into it's own file eliminates the problem.

The full code is a pair of Class::DBI classes for a master/detail pair of tables, and I'd prefer to keep them together in one file. Is there a "right" way to do that, and prevent the sub redefined warnings? If not, I can live with having 2 files (or N files later) rather than 1.

Humbly,

dcvr69

Update: seems I missed the essential perldoc relevant to the problem (base). Simply defining a $VERSION for my base class prevents the sub-classes from doing 'require "X"' unnecessarily.

This works as intended:

package X; $VERSION = 0.01; sub x { 1 } package X::Y; use base 'X'; 1;

Replies are listed 'Best First'.
Re: multiple packages in one file and subroutine redifned warnings
by japhy (Canon) on May 18, 2004 at 18:27 UTC
    The problem is that use base 'Package' loads Package.pm, which ends up redefining the functions you've already defined. When you use or require yourself (meaning, the file that you're in), you get that problem. You'd be better off just setting @ISA to 'A'.
    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
Re: multiple packages in one file and subroutine redifned warnings
by duff (Parson) on May 18, 2004 at 18:18 UTC
    The problem is that base automatically loads the file for you. Since you've named the file X.pm, it gets loaded from itself as it were. Just don't use base when you've put the base class in the same file, but rather modify @ISA yourself.
      Doh. That's too easy. ;)

      Guess I'll make a stab at a patch to the Class::DBI docs to mention that little fact, since they're fond of using examples with multiple packes in the same file - just without any sub definitions. :)

      Well, the patch should be pretty easy: add a $VERSION to your base class if you don't want 'sub redef' warnings.