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

Using a base module with a required version

by talexb (Canon)
on Aug 02, 2019 at 16:25 UTC ( #11103770=perlquestion: print w/replies, xml ) Need Help??

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

I'm updating a module Juliet that's the base for another module November. So in November, I have

use base Juliet;
Since I'm updating Juliet, I'm going to add a $VERSION so that we can start requiring certain versions of modules going forward. But I'm uncertain about how to add a version requirement to the use base statement. Just putting
use base Juliet 1.1;
produces a 'Number found where operator expected ..' error when I compile.

So I guess the question is, how do I combine a version requirement with a use base statement?

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Replies are listed 'Best First'.
Re: Using a base module with a required version
by Corion (Pope) on Aug 02, 2019 at 16:42 UTC

    I don't think you can do it in one statement. You can do it in two statements though:

    package November; use base 'Juliet'; Juliet->VERSION(1.1);

      Excellent! -- thank you. I tried testing this, but couldn't get a version mis-match to cause a compile time error.

      Then I decided to get into the debugger to have a better look at what was going on -- and the debugger issued the expected error right away. I'm not sure why, but I suspect it's the difference between what stage of the preparation the Perl internals got to in each situation. The debugger gets much closer to run-time, hence it triggered the error.

      Alex / talexb / Toronto

      Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

        Yes, the two statements run at different times, as you found out:

        package November; use base 'Juliet'; # runs at compile time Juliet->VERSION(1.1); # runs at runtime, after November.pm has been pa +rsed completely

        This usually makes little difference as long as November.pm is its own file. If November.pm is loaded later, code from earlier modules will already have run by the time the ->VERSION() call is made.

Re: Using a base module with a required version
by ikegami (Pope) on Aug 02, 2019 at 21:35 UTC
    use Juliet 1.1; use base 'Juliet';

    But base has issues avoided by using parent.

    use Juliet 1.1; use parent 'Juliet';

    But I don't see the point of using either.

    use Juliet 1.1; our @ISA = 'Juliet';

      Saying it this way

      use Juliet 1.1; our @ISA = 'Juliet';
      actually makes the most sense to me -- but I'm uneasy about twiddling with @ISA since that's how Perl gets told about inheritance.

      Hiding @ISA with your middle example

      use Juliet 1.1; use parent 'Juliet';
      is less alarming -- but it seems like it does the same thing.

      Alex / talexb / Toronto

      Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

        I'm uneasy about twiddling with @ISA since that's how Perl gets told about inheritance.

        I've very confused. Isn't that exactly what you want to do?

        If not, you shouldn't be using base.pm or parent.pm or @ISA, just use Juliet 1.1;.

Re: Using a base module with a required version
by stevieb (Canon) on Aug 02, 2019 at 16:43 UTC

    It doesn't appear as though that functionality is available in either base or parent. Here's a potential q&d workaround that may be sufficient if it turns out what you're trying to do isn't possible inherently:

    use warnings; use strict; use version; BEGIN { use base 'Mock::Sub'; my $v = version->parse($Mock::Sub::VERSION); if ($v < version->parse(1.07)){ die "Version '$v' of Mock::Sub isn't 1.07 or greater"; } }

    The output, first with version 1.06 of the distribution, then another run after updating the dist:

    $ perl base.pl Version '1.06' of Mock::Sub isn't 1.07 or greater at base.pl line 10. BEGIN failed--compilation aborted at base.pl line 12. $ cpanm Mock::Sub --> Working on Mock::Sub Fetching http://www.cpan.org/authors/id/S/ST/STEVEB/Mock-Sub-1.09.tar. +gz ... OK Configuring Mock-Sub-1.09 ... OK Building and testing Mock-Sub-1.09 ... OK Successfully installed Mock-Sub-1.09 (upgraded from 1.06) 1 distribution installed $ perl base.pl

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2021-03-03 03:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (69 votes). Check out past polls.

    Notices?