Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: help with versioning modules

by afoken (Chancellor)
on Nov 20, 2020 at 19:20 UTC ( [id://11123922]=note: print w/replies, xml ) Need Help??


in reply to help with versioning modules

On a related note, if I have modules that will only be used internally and that I want to version (as opposed to modules I download from CPAN, in which subsequent module updates overwrite the previous module version), suppose I have the following directory structure:

/home/user/perl_modules/lib/perl5/My/FooVersion 1.0/ Foo.pm 1.1/ Foo.pm 1.2/ Foo.pm Devel/ Foo.pm

How would I use a particular version of Foo.pm in my script? Perl is fine with:

use My::FooVersion::Devel::Foo;

but trying to reference any of the numbered versions in the same manner, even when I quote either the numerical portion or the entire string, produces syntax errors. For example, none of the following work:

use My::FooVersion::1.1::Foo; use "My::FooVersion::1.1::Foo"; use My::FooVersion::"1.1"::Foo; use My::FooVersion::'1.1'::Foo; use 'My::FooVersion::1.1::Foo';

use expect a bareword, not a string. Period. If you want module names that are not barewords, you are already begging for trouble, as package expects a namespace, not a string.

Anyway, it is possible to load a module from such "unperlish" names. This is slightly hidden in use:

Imports some semantics into the current package from the named module, generally by aliasing certain subroutine or variable names into your package. It is exactly equivalent to

BEGIN { require Module; Module->import( LIST ); }

except that Module must be a bareword.

require allows to use a bareword, but you can alternatively provide a filename to be loaded. To generate a filename from a bareword, convert all :: and ' to / and append .pm.

BUT

I think the idea to load several versions of the same module into the same namespace is really, really begging for trouble.

There may be reasons for having versioned module name spaces, for example to support APIs or protocols with different versions. Consider an imaginary set of HTTP protocol modules:

  • My::Protocol::HTTP for the user interface
  • My::Protocol::HTTP::v0_9 for implementing the first public version
  • My::Protocol::HTTP::v1_0 for implementing HTTP/1.0
  • My::Protocol::HTTP::v1_1 for implementing HTTP/1.1
  • My::Protocol::HTTP::v2_0 for implementing HTTP/2.0

Note that version numbers were changed to allow their use in barewords. Also note that those modules would load into different namespaces.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^2: help with versioning modules
by Special_K (Monk) on Nov 23, 2020 at 01:25 UTC

    > I think the idea to load several versions of the same module into the same namespace is really, really begging for trouble.

    The intent is to have multiple versions of the module so that when a new version of the module is developed, any other scripts that called a previous version can continue calling it without automatically having to start using the new version and risk something breaking. There would never be a case in which multiple versions of the same module would be loaded into the same namespace.

    > Note that version numbers were changed to allow their use in barewords. Also note that those modules would load into different namespaces.

    Something like that would probably work for what I'm doing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11123922]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-19 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found