Use different modules based on variable

P

perlguru

This is with respect to a thread "Use different modules based on
variable" <http://groups.google.com/group/comp.lang.perl.misc/
browse_thread/thread/00b9efe4f4416943?tvc=2> .

In this I had suggested for making use of 'require', but that
suggestion was bounced back very badly.
Recently I gave a try, and I see that my suggestion is working :

I created 1st Module 1.1/Test.pm :-
----------------------------------------------------
package Test;

sub func{
print "In 1.1/ \n";
return 1;
}

1;

Created another Module 2.2/Test.pm :-
-----------------------------------------------------------
package Test;

sub func{
print "In 2.2 \n";
return 1;
}

1;


The Main program :-
--------------------------------
#!/usr/bin/perl

$ver=$ARGV[0];

if ( $ver eq '1.1' ){
require "./1.1/Test.pm";
Test::func();
require "./2.2/Test.pm";
Test::func();
}else{
require "./2.2/Test.pm";
Test::func();
require "./1.1/Test.pm";
Test::func();
}

Test::func();
exit 0;


After I executed :-
--------------------------------
$ perl test.pl 1.1
In 1.1/
In 2.2
In 2.2

$ perl test.pl 2.2
In 2.2
In 1.1/
In 1.1/

$ perl test.pl
In 2.2
In 1.1/
In 1.1/


------------End----------------------

I am just a user of Perl, and don't have indepth idea about its
design, And the way its interpreter works.

Would like to know your views, on how is above thing working?
What is dispatch table? And
Why as the per discussion thread
http://groups.google.com/group/comp.lang.perl.misc/browse_thread/thread/00b9efe4f4416943?tvc=2
it shouldnt have been working ?
 
P

Paul Lalli

This is with respect to a thread "Use different modules based on
variable" <http://groups.google.com/group/comp.lang.perl.misc/
browse_thread/thread/00b9efe4f4416943?tvc=2> .

In this I had suggested for making use of 'require', but that
suggestion was bounced back very badly.
Recently I gave a try, and I see that my suggestion is working :

I created 1st Module 1.1/Test.pm :-
----------------------------------------------------
package Test;

sub func{
print "In 1.1/ \n";
return 1;

}

1;

Created another Module 2.2/Test.pm :-
-----------------------------------------------------------
package Test;

sub func{
print "In 2.2 \n";
return 1;

}

1;

The Main program :-
--------------------------------
#!/usr/bin/perl

$ver=$ARGV[0];

if ( $ver eq '1.1' ){
require "./1.1/Test.pm";
Test::func();
require "./2.2/Test.pm";
Test::func();}else{

require "./2.2/Test.pm";
Test::func();
require "./1.1/Test.pm";
Test::func();

}

Test::func();
exit 0;

After I executed :-
--------------------------------
$ perl test.pl 1.1
In 1.1/
In 2.2
In 2.2

$ perl test.pl 2.2
In 2.2
In 1.1/
In 1.1/

$ perl test.pl
In 2.2
In 1.1/
In 1.1/

------------End----------------------

I am just a user of Perl, and don't have indepth idea about its
design, And the way its interpreter works.

Would like to know your views, on how is above thing working?
What is dispatch table? And
Why as the per discussion threadhttp://groups.google.com/group/comp.lang.perl.misc/browse_thread/thre...
it shouldnt have been working ?

As was said to you in that thread - the same module will NOT be loaded
again. The original requirement was that, at run time, either module
will be needed at any given moment. To illustrate, alter your above
example as follows:

#!/usr/bin/perl
$ver=$ARGV[0];
if ( $ver eq '1.1' ){
require "./1.1/Test.pm";
Test::func();
require "./2.2/Test.pm";
Test::func();
require "./1.1/Test.pm";
Test::func();
}else{
require "./2.2/Test.pm";
Test::func();
require "./1.1/Test.pm";
Test::func();
require "./2.2/Test.pm";
Test::func();
}
Test::func();
exit 0;

$ ./test.pl 1.1
In 1.1/
In 2.2
In 2.2
In 2.2

$ ./test.pl 2.2
In 2.2
In 1.1/
In 1.1/
In 1.1/


Do you see? require() stores the modules that have already been
loaded, and does not load them a second time. The second instance of
either module being require()'d is completely ignored.

That is why your solution was not applicable to the OP's stated goal.

Paul Lalli
 
T

Tad McClellan

perlguru said:
This is with respect to a thread "Use different modules based on
variable" <http://groups.google.com/group/comp.lang.perl.misc/
browse_thread/thread/00b9efe4f4416943?tvc=2> .

In this I had suggested for making use of 'require', but that
suggestion was bounced back very badly.


Because it did not do what the original poster asked for.

I am just a user of Perl, and don't have indepth idea about its
design, And the way its interpreter works.


Then "perlguru" is a rather pretentious nickname to choose...

What is dispatch table?


The %chooser hash in the thread you reference is an example
of a dispatch table.
 
P

perlguru

This is with respect to a thread "Use different modules based on
variable" <http://groups.google.com/group/comp.lang.perl.misc/
browse_thread/thread/00b9efe4f4416943?tvc=2> .
In this I had suggested for making use of 'require', but that
suggestion was bounced back very badly.
Recently I gave a try, and I see that my suggestion is working :
I created 1st Module 1.1/Test.pm :-
sub func{
print "In 1.1/ \n";
return 1;


Created another Module 2.2/Test.pm :-
sub func{
print "In 2.2 \n";
return 1;

The Main program :-
--------------------------------
#!/usr/bin/perl
$ver=$ARGV[0];

if ( $ver eq '1.1' ){
require "./1.1/Test.pm";
Test::func();
require "./2.2/Test.pm";
Test::func();}else{
require "./2.2/Test.pm";
Test::func();
require "./1.1/Test.pm";
Test::func();

Test::func();
exit 0;
After I executed :-
--------------------------------
$ perl test.pl 1.1
In 1.1/
In 2.2
In 2.2
$ perl test.pl 2.2
In 2.2
In 1.1/
In 1.1/
$ perl test.pl
In 2.2
In 1.1/
In 1.1/

I am just a user of Perl, and don't have indepth idea about its
design, And the way its interpreter works.
Would like to know your views, on how is above thing working?
What is dispatch table? And
Why as the per discussion threadhttp://groups.google.com/group/comp.lang.perl.misc/browse_thread/thre...
it shouldnt have been working ?

As was said to you in that thread - the same module will NOT be loaded
again. The original requirement was that, at run time, either module
will be needed at any given moment. To illustrate, alter your above
example as follows:

#!/usr/bin/perl
$ver=$ARGV[0];
if ( $ver eq '1.1' ){
require "./1.1/Test.pm";
Test::func();
require "./2.2/Test.pm";
Test::func();
require "./1.1/Test.pm";
Test::func();}else{

require "./2.2/Test.pm";
Test::func();
require "./1.1/Test.pm";
Test::func();
require "./2.2/Test.pm";
Test::func();}

Test::func();
exit 0;

$ ./test.pl 1.1
In 1.1/
In 2.2
In 2.2
In 2.2

$ ./test.pl 2.2
In 2.2
In 1.1/
In 1.1/
In 1.1/

Do you see? require() stores the modules that have already been
loaded, and does not load them a second time. The second instance of
either module being require()'d is completely ignored.

That is why your solution was not applicable to the OP's stated goal.

Paul Lalli


Thanks Paul.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top