• Redefine warnings when subclassing

    From Henry Law@21:1/5 to All on Tue Jun 21 17:33:54 2022
    There's plainly something I don't understand. I have a file which
    contains four packages; the first is a base class and the other three are subclasses of it. The code runs fine but I'm getting warnings that the subroutines are redefined; I thought that was the whole purpose of
    subclassing! I know I can put "no warnings redefine", but should I have
    to?

    #!/usr/bin/perl

    use strict;
    use warnings;
    use 5.016;

    package A;

    sub new{
    print "This is the new method for A\n";
    }

    sub doodle{
    print "This is the doodle method for A\n";
    }

    1;

    package B;

    use parent 'A';

    sub new{
    print "This is a subclassed method for B\n";
    }

    1;

    # perl -c A.pm
    Subroutine new redefined at /home/henry/Perl/tryout/A.pm line 9.
    Subroutine doodle redefined at /home/henry/Perl/tryout/A.pm line 13.
    Subroutine new redefined at A.pm line 23.
    A.pm syntax OK

    PS: there's something really weird going on, because "doodle" isn't even
    being subclassed and yet it still gets a warning.

    --
    Henry Law n e w s @ l a w s h o u s e . o r g
    Manchester, England

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From E. Choroba@21:1/5 to Henry Law on Tue Jun 21 16:27:57 2022
    On Tuesday, June 21, 2022 at 7:36:41 PM UTC+2, Henry Law wrote:
    There's plainly something I don't understand. I have a file which
    contains four packages; the first is a base class and the other three are subclasses of it. The code runs fine but I'm getting warnings that the subroutines are redefined; I thought that was the whole purpose of subclassing! I know I can put "no warnings redefine", but should I have
    to?

    #!/usr/bin/perl

    use strict;
    use warnings;
    use 5.016;

    package A;

    sub new{
    print "This is the new method for A\n";
    }

    sub doodle{
    print "This is the doodle method for A\n";
    }

    1;

    package B;

    use parent 'A';

    sub new{
    print "This is a subclassed method for B\n";
    }

    1;

    # perl -c A.pm
    Subroutine new redefined at /home/henry/Perl/tryout/A.pm line 9.
    Subroutine doodle redefined at /home/henry/Perl/tryout/A.pm line 13. Subroutine new redefined at A.pm line 23.
    A.pm syntax OK

    PS: there's something really weird going on, because "doodle" isn't even being subclassed and yet it still gets a warning.

    --
    Henry Law n e w s @ l a w s h o u s e . o r g
    Manchester, England

    If you used separate files for the packages, you wouldn't get the warnings. The reason is that when perl sees
    use parent 'A';
    it tries to load A.pm as it hasn't loaded it yet. Older Perl versions had the current directory in @INC, so the file itself would be found and read again, redefining already defined subroutines.
    To prevent that, use separate files for the packages, or, if you really need to include both of them in one file, use the -norequire option for parent:
    use parent -norequire => 'A';
    This would stop Perl from loading A.pm.

    Ch.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From E. Choroba@21:1/5 to Henry Law on Tue Jun 21 16:26:42 2022
    On Tuesday, June 21, 2022 at 7:36:41 PM UTC+2, Henry Law wrote:
    There's plainly something I don't understand. I have a file which
    contains four packages; the first is a base class and the other three are subclasses of it. The code runs fine but I'm getting warnings that the subroutines are redefined; I thought that was the whole purpose of subclassing! I know I can put "no warnings redefine", but should I have
    to?

    #!/usr/bin/perl

    use strict;
    use warnings;
    use 5.016;

    package A;

    sub new{
    print "This is the new method for A\n";
    }

    sub doodle{
    print "This is the doodle method for A\n";
    }

    1;

    package B;

    use parent 'A';

    sub new{
    print "This is a subclassed method for B\n";
    }

    1;

    # perl -c A.pm
    Subroutine new redefined at /home/henry/Perl/tryout/A.pm line 9.
    Subroutine doodle redefined at /home/henry/Perl/tryout/A.pm line 13. Subroutine new redefined at A.pm line 23.
    A.pm syntax OK

    PS: there's something really weird going on, because "doodle" isn't even being subclassed and yet it still gets a warning.

    --
    Henry Law n e w s @ l a w s h o u s e . o r g
    Manchester, England

    If you used separate files for the packages, you wouldn't get the warnings. The reason is that when perl sees
    use parent 'A';
    it tries to load A.pm as it hasn't loaded it yet. Older Perl versions had the current directory in @INC, so the file itself would be found and read again, redefining already defined subroutines.
    To prevent that, use separate files for the packages, or, if you really need to include both of them in one file, use the -norequire option for parent:
    use parent -norequire 'A';
    This would stop Perl from loading A.pm.

    Ch.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eric Pozharski@21:1/5 to Henry Law on Thu Jun 23 13:29:42 2022
    with <OOucnQu3LuKfnC__nZ2dnZeNn_XNnZ2d@giganews.com> Henry Law wrote:

    There's plainly something I don't understand.

    Such lovely discussion you have here. I feel like intervining ;)

    I have a file which contains four packages; the first is a base class
    and the other three are subclasses of it. The code runs fine but I'm
    getting warnings that the subroutines are redefined; I thought that
    was the whole purpose of subclassing!

    Well, turns out you voided your warranty. Here comes my understanding.

    AIII, that's what they call "Koo-Koo module" (in this case it's even
    worse). Couple of years ago I've seen some calls for coming to senses
    and cleaning CPAN of these nasties. Obviously, those fall on deaf ears. Anyway, that's what happens:

    Noramlly, you 'require' then manipulate '@ISA'. 'parent.pm', 'base.pm',
    or whatever else combine these steps for you. Thus, whatever your
    habits, 'require' is involved. In your CME you have shebang, that makes
    it '.pl'. But your diagnostic says it's '.pm'. Your command-line says
    you *run* '.pm' (what can possibly go wrong?). Then, perl does what you
    told it to do: compile "A.pm", that module 'require's "A.pm" that hasn't
    been compiled yet (perl is lazy here). That leads to redefinition of
    subs that already have been compiled, then redefinition of subs that
    have been compiled by 'require' after 'require' has finished (look for yourself, first and second warning refer to fully qualified filename,
    then third refers to filename as seen on command-line). Totaly normal,
    nothing to see here. Just Perl doing it's thing :[

    I know I can put "no warnings redefine", but should I have to?

    Well you have options:

    * Do it properly -- one '.pm', one 'package'. Make ovid happy.

    * Plug warnings with "no warnings qw/ redefine /". Make perl happy.

    * Manipulate '@ISA' directly. Shoot yourself in foot sometime later.

    * Search CPAN, probably there is something in support of Koo-Koo
    modules, but I don't have any keywords to start with. Make yourself
    busy.

    * Did I miss something?

    *SKIP*
    PS: there's something really weird going on, because "doodle" isn't
    even being subclassed and yet it still gets a warning.

    I'm actualy surprised you haven't figured out all this by yourself. I
    blame RL.

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to Eric Pozharski on Sun Jun 26 18:59:51 2022
    Eric Pozharski <whynot@pozharski.name> writes:
    with <OOucnQu3LuKfnC__nZ2dnZeNn_XNnZ2d@giganews.com> Henry Law wrote:

    [...]

    I know I can put "no warnings redefine", but should I have to?

    Well you have options:

    * Do it properly -- one '.pm', one 'package'. Make ovid happy.

    There's no reason to make someone happy who suffers from Poettering's
    disease, ie, from the hard-coded assumption that "all of the world must
    work just as my laptop does!".


    * Plug warnings with "no warnings qw/ redefine /". Make perl happy.

    * Manipulate '@ISA' directly. Shoot yourself in foot sometime later.

    * Search CPAN, probably there is something in support of Koo-Koo
    modules, but I don't have any keywords to start with. Make yourself
    busy.

    * Did I miss something?

    Don't use silly modules implementing unrelated bits of trivial
    functionality just because someone can't imagine using one without the
    other. That's bad software design.

    -----
    package Ahnen;

    sub import
    {
    my ($caller, $isa);

    shift;
    $caller = caller();
    $isa = \@{"${caller}::ISA"};

    for my $class (reverse(@_)) {
    unshift(@$isa, $class) unless eval { $_ eq $class and return 1 for @$isa };
    }
    }

    1;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)