Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
more useful options
 
PerlMonks  

both base and sub class in same file

by pvncad (Initiate)
on Oct 11, 2007 at 13:21 UTC ( #644217=perlquestion: print w/ replies, xml ) Need Help??
pvncad has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to do a syntax check of a module which has both base and sub class definition. I am getting redefinition warning.
package Foo; sub test { } 1; package Bar; use base Foo; sub test2 { } 1;
perl -wc Foo.pm is giving following output
Subroutine test2 redefined at Foo.pm line 4. Subroutine test redefined at Foo.pm line 14. Foo.pm syntax OK
Why is this happening ?

Comment on both base and sub class in same file
Select or Download Code
Re: both base and sub class in same file
by ikegami (Pope) on Oct 11, 2007 at 13:28 UTC
    Foo.pm is loaded both as a script and as a module (by use base). Try perl -c -we "use Foo"
      Thanks. But is this the best way to do syntax check of a module ?

        What metric should I use to judge whether it's the best besides "It works"?

Re: both base and sub class in same file
by Sidhekin (Priest) on Oct 11, 2007 at 13:29 UTC

    This is happening because base ends up requireing Foo, which in turns ends up doing Foo.pm, since it hasn't been donerequired yet ...

    Update 2: The safeguard is in require, of course. ikegami++. The workaround below is still useful for those "executable modules", but unnecessary for most purposes.

    (Oh, and for completeness, use happens at compile time, and so is run even with the -c option.)

    Update: A workaround: Add a $VERSION variable to Foo, as this prevents base from trying to load it again:

    package Foo; our $VERSION = 0.001; sub test { } 1; package Bar; use base Foo; sub test2 { } 1;

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Re: both base and sub class in same file
by Fletch (Chancellor) on Oct 11, 2007 at 13:30 UTC

    base does a require of the module in question itself, so basically you're pulling in the same file in the middle of parsing itself. Either move Bar to its own file, or eschew base and just set @ISA yourself.

    Update: Nevermind me, ikegami's got it.

Re: both base and sub class in same file
by naikonta (Curate) on Oct 12, 2007 at 00:51 UTC
    You don't actually need the first 1; line there, before the package Bar; statement, though it's harmless. And number 1 is apperantly special as it doesn't generate useless constant in void warning, as other constants.
    $ perl -c -we '1' -e syntax OK $ perl -c -we '3' Useless use of a constant in void context at -e line 1. -e syntax OK $ perl -c -we '"OK"' Useless use of a constant in void context at -e line 1. -e syntax OK
    I frankly wondered at first, but, fortunately diagnostics has the fact:
    This warning will not be issued for numerical constants equal to 0 or 1 since they are often used in statements like
    1 while sub_with_side_effects();
    String constants that would normally evaluate to 0 or 1 are warned about.
    One more thing, if you really need Foo from Bar, you might consider to use @ISA for inheritance or Exporter for importing Foo's symbols.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://644217]
Approved by Sidhekin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-04-24 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (557 votes), past polls