If old programs use Old::MyModule; and new programs use New::MyModule; then you will have to have two .pm files if they are to work without modification and you don't want links.

One option is to move your code into a third file and use do to load it into both modules.

test.pl

#!/usr/bin/perl # use strict; use warnings; use New::MyModule; use Old::MyModule; New::MyModule::printer(); Old::MyModule::printer();

New/MyModule.pm

package New::MyModule; do 'New/MyModule.pl' or die "New/MyModule.pl: $!"; 1;

Old/MyModule.pm

package Old::MyModule; do 'New/MyModule.pl' or die "New/MyModule.pl: $!"; 1;

And the common code in New/MyModule.pl

sub printer { print "here we are in " . __PACKAGE__ . "\n"; } 1;

Another option is to write one module normally and the other to read the first, changing the package name as it loads, then evaluate it.

test.pl is as above.

New/MyModule.pm is a regular module.

package New::MyModule; sub printer { print "here we are in " . __PACKAGE__ . "\n"; } 1;

And Old/MyModule.pm loads New/MyModule.pm, changing the package name as it loads.

package Old::MyModule; my $contents; foreach my $prefix (@INC) { my $file = "$prefix/New/MyModule.pm"; if(-f $file) { open(FILE, "<", "$file") or die "$file: $!"; $contents = do { local $/; <FILE> }; close(FILE); last; } } die "Can't locate New::MyModule in \@INC (\@INC contains: @INC)." unle +ss($contents); $contents =~ s/package New::MyModule/package Old::MyModule/; my $result = eval $contents; die "$file: $@" if($@); die "$file: did not end with a true value" unless($result); 1;

In reply to Re: Rename/Relocate A Module by ig
in thread Rename/Relocate A Module by rajib_rashid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.