1

First, I'd like to say this isn't a question of design, but a question of compliance. I'm aware there are issues with the current setup.

In a module, there are packages that are named after the servers, which has many of the same variables/functions that pertain to that server. It looks like this was set up so that you could do:

  • PRODUCTION_SERVER_NAME::printer() or
    TEST_SERVER_NAME::printer()

Perhaps a better design might have been something like:

  • CENTRAL_PACKAGE_NAME::printer('production') or CENTRAL_PACKAGE_NAME::printer('test')

Anyhow, it seems the server names have changed, so instead of using the actual servernames, I'd like to rename the packages to just PRODUCTION or TEST, without changing the other code that is still refering to PRODUCTION_SERVER_NAME.

Something like:

package PRODUCTION, PRODUCTION_SERVER_NAME;  # pseudo code

I'm guessing some sort of glob/import might work, but was wondering if there is already something that does something similar. I also realize it's not good practice to saturate namespaces.

3 Answers 3

3

I am not providing any comments on the design or anything that might involve changing client code. Functions in MyTest.pm can be accessed using either MyTest:: or MyExam::. However, you can't use use MyExam because the physical file is not there. You could do clever @INC tricks, but my programs always crash and burn when I try to be clever.

MyTest.pm

package MyTest;

sub hello { 'Hello' }
sub twoplustwo { 4 }

for my $sub (qw( hello twoplustwo)) {
    no strict 'refs';
    *{"MyExam::$sub"} = *{"MyTest::$sub"};
}

1;

test.pl

#!/usr/bin/env perl

use strict; use warnings;
use feature 'say';

use MyTest;

say MyExam::hello();
say MyExam::twoplustwo();

Output:

Hello
4
Sign up to request clarification or add additional context in comments.

1 Comment

This looks promising - nice to see you again Sinan
2

Have you considered using aliased? It sounds like it could work for you.

2 Comments

nick, would you using alias w/in the module? The major point is that I don't want to have to notify users that the package/module has changed and I don't want to change all the old code. If I can't do this unobtrusively, then I'll have to leave it as-is.
the only change you would need to make would be the use line in your existing code - everything else would remain untouched
0

Try Exporter::Auto:

package Foo;

use Exporter::Auto;

sub foo {
    print('foo');
}

package Bar;

use Foo;

package main;

Foo::foo();
Bar::foo();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.