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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |