[28981] in Perl-Users-Digest
Perl-Users Digest, Issue: 225 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 15 09:14:15 2007
Date: Thu, 15 Mar 2007 06:14:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 15 Mar 2007 Volume: 11 Number: 225
Today's topics:
Use different modules based on variable <geoffrobinson@gmail.com>
Re: Use different modules based on variable <nilsonj@gmail.com>
Re: Use different modules based on variable <manojkumargupta@gmail.com>
Re: Use different modules based on variable <uri@stemsystems.com>
Re: Use different modules based on variable <manojkumargupta@gmail.com>
Re: Use different modules based on variable anno4000@radom.zrz.tu-berlin.de
Re: Use different modules based on variable <mark.clementsREMOVETHIS@wanadoo.fr>
Re: Use different modules based on variable <tadmc@augustmail.com>
Re: Use different modules based on variable <fiedlert@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 14 Mar 2007 20:23:58 -0700
From: "geoffrobinson" <geoffrobinson@gmail.com>
Subject: Use different modules based on variable
Message-Id: <1173929038.539958.142380@e65g2000hsc.googlegroups.com>
Hi,
I have to do some error-checking on particular files. The routines I
will be calling will have the same name, but they need to be from
different modules based on a variable. (version 1.1, 2, etc.)
I assume that if I write "use Module1" within an if-block it will only
have scope within the if-block.
I'm trying to think of other options as well. "use only" doesn't quite
work because a single run of the script may want to call 1.1 and 2
during the same run, but at different times. In other words if file 1
needs a 1.1 error-checking routine, I want to use that, let it go out
of scope, and then use 2.2 if file 2 needs 2.2.
Please let me know if I am being unclear or confusing.
thanks for any help in advance,
Geoff
------------------------------
Date: 14 Mar 2007 21:25:48 -0700
From: "nilsonj" <nilsonj@gmail.com>
Subject: Re: Use different modules based on variable
Message-Id: <1173932748.838732.173790@e1g2000hsg.googlegroups.com>
On Mar 14, 11:23 pm, "geoffrobinson" <geoffrobin...@gmail.com> wrote:
> Hi,
>
> I have to do some error-checking on particular files. The routines I
> will be calling will have the same name, but they need to be from
> different modules based on a variable. (version 1.1, 2, etc.)
>
> I assume that if I write "use Module1" within an if-block it will only
> have scope within the if-block.
>
> I'm trying to think of other options as well. "use only" doesn't quite
> work because a single run of the script may want to call 1.1 and 2
> during the same run, but at different times. In other words if file 1
> needs a 1.1 error-checking routine, I want to use that, let it go out
> of scope, and then use 2.2 if file 2 needs 2.2.
>
> Please let me know if I am being unclear or confusing.
>
> thanks for any help in advance,
> Geoff
Cleaner to instantiate them in separate classes and call them /
through/ a variable.
ie, Restructure your libs so that you have Checker::v2_2 and
Checker::v1_1 ... then instantiate an object of each ...
$thisChecker is Checker::v1_1;
Then call the member functions accordingly ...
$thisChecker->SomeCheckingFunction();
------------------------------
Date: 14 Mar 2007 23:12:41 -0700
From: "perlguru" <manojkumargupta@gmail.com>
Subject: Re: Use different modules based on variable
Message-Id: <1173939161.933151.87180@n59g2000hsh.googlegroups.com>
On Mar 15, 8:23 am, "geoffrobinson" <geoffrobin...@gmail.com> wrote:
> Hi,
>
> I have to do some error-checking on particular files. The routines I
> will be calling will have the same name, but they need to be from
> different modules based on a variable. (version 1.1, 2, etc.)
>
> I assume that if I write "use Module1" within an if-block it will only
> have scope within the if-block.
>
> I'm trying to think of other options as well. "use only" doesn't quite
> work because a single run of the script may want to call 1.1 and 2
> during the same run, but at different times. In other words if file 1
> needs a 1.1 error-checking routine, I want to use that, let it go out
> of scope, and then use 2.2 if file 2 needs 2.2.
>
> Please let me know if I am being unclear or confusing.
>
> thanks for any help in advance,
> Geoff
Make use of 'require' instead of 'use'. Remember that with 'use'
module is loaded at the compile timeitself, whereas with 'require' it
is loaded at the time when is requested. Below code may help you.
if ( $ver eq '1.1' ){
require "/usr/lib.../1.1/Module.pm";
Module::func();
}else{
require "/usr/lib.../2.2/Module.pm";
Module::func();
}
------------------------------
Date: Thu, 15 Mar 2007 01:39:38 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Use different modules based on variable
Message-Id: <x7ps7bdn85.fsf@mail.sysarch.com>
>>>>> "p" == perlguru <manojkumargupta@gmail.com> writes:
p> On Mar 15, 8:23 am, "geoffrobinson" <geoffrobin...@gmail.com> wrote:
>> Hi,
>>
>> I have to do some error-checking on particular files. The routines I
>> will be calling will have the same name, but they need to be from
>> different modules based on a variable. (version 1.1, 2, etc.)
>>
>> I assume that if I write "use Module1" within an if-block it will only
>> have scope within the if-block.
>>
>> I'm trying to think of other options as well. "use only" doesn't quite
>> work because a single run of the script may want to call 1.1 and 2
>> during the same run, but at different times. In other words if file 1
>> needs a 1.1 error-checking routine, I want to use that, let it go out
>> of scope, and then use 2.2 if file 2 needs 2.2.
p> if ( $ver eq '1.1' ){
p> require "/usr/lib.../1.1/Module.pm";
p> Module::func();
p> }else{
p> require "/usr/lib.../2.2/Module.pm";
p> Module::func();
p> }
that won't work at all. read the actual need which is to call either one
during run time in the same run. require won't reload the module again
so only the last one loaded will ever be called. the OP has to name the
modules differently so they won't collide in the symbol table and both
can be loaded. then a basic dispatch table should do the trick for
calling one or the other.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: 15 Mar 2007 00:03:31 -0700
From: "perlguru" <manojkumargupta@gmail.com>
Subject: Re: Use different modules based on variable
Message-Id: <1173942211.252971.176840@p15g2000hsd.googlegroups.com>
On Mar 15, 11:39 am, Uri Guttman <u...@stemsystems.com> wrote:
> >>>>> "p" == perlguru <manojkumargu...@gmail.com> writes:
>
> p> On Mar 15, 8:23 am, "geoffrobinson" <geoffrobin...@gmail.com> wrote:
> >> Hi,
> >>
> >> I have to do some error-checking on particular files. The routines I
> >> will be calling will have the same name, but they need to be from
> >> different modules based on a variable. (version 1.1, 2, etc.)
> >>
> >> I assume that if I write "use Module1" within an if-block it will only
> >> have scope within the if-block.
> >>
> >> I'm trying to think of other options as well. "use only" doesn't quite
> >> work because a single run of the script may want to call 1.1 and 2
> >> during the same run, but at different times. In other words if file 1
> >> needs a 1.1 error-checking routine, I want to use that, let it go out
> >> of scope, and then use 2.2 if file 2 needs 2.2.
>
> p> if ( $ver eq '1.1' ){
> p> require "/usr/lib.../1.1/Module.pm";
> p> Module::func();
> p> }else{
> p> require "/usr/lib.../2.2/Module.pm";
> p> Module::func();
> p> }
>
> that won't work at all. read the actual need which is to call either one
> during run time in the same run. require won't reload the module again
> so only the last one loaded will ever be called. the OP has to name the
> modules differently so they won't collide in the symbol table and both
> can be loaded. then a basic dispatch table should do the trick for
> calling one or the other.
>
> uri
>
> --
> Uri Guttman ------ u...@stemsystems.com --------http://www.stemsystems.com
> --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
> Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
have you given a try ? ... Give a hit !!, and see the result.
------------------------------
Date: 15 Mar 2007 10:29:27 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Use different modules based on variable
Message-Id: <55slg7F260onrU1@mid.dfncis.de>
geoffrobinson <geoffrobinson@gmail.com> wrote in comp.lang.perl.misc:
> Hi,
>
> I have to do some error-checking on particular files. The routines I
> will be calling will have the same name, but they need to be from
> different modules based on a variable. (version 1.1, 2, etc.)
>
> I assume that if I write "use Module1" within an if-block it will only
> have scope within the if-block.
That assumption is wrong. While the effects of modules *can* be block
scoped, normally they aren't. In particular, the functions imported
by a module are package scoped and are valid outside the current block.
On the other hand, a "use" statement is executed at compile time,
independent of possible run-time conditionals.
> I'm trying to think of other options as well. "use only" doesn't quite
> work because a single run of the script may want to call 1.1 and 2
> during the same run, but at different times. In other words if file 1
> needs a 1.1 error-checking routine, I want to use that, let it go out
> of scope, and then use 2.2 if file 2 needs 2.2.
So you need to switch back and forth between versions during the run
time of your script. Here is one way to do that:
Prepare two modules Module1.pm and Module2.pm that implement the
different versions of your function(s). Load both without importing
anything into your namespace. Then, in your main program, you can
define a function switch_version that imports the necessary functions
at run time. This is how it might look:
The main script:
use Module_1 ();
use Module_2 ();
sub switch_version {
my $version = shift;
my @functions = qw( gick);
for ( @functions ) {
no strict 'refs';
no warnings 'redefine';
*$_ = \ &{ join '::', "Module_$version", $_ };
}
}
switch_version 1;
gick();
switch_version 2;
gick();
__END__
Module1.pm:
package Module_1;
use warnings; use strict;
sub gick {
print "this is version 1\n"
}
1;
Module2.pm
package Module_2;
use warnings; use strict;
sub gick {
print "this is version 2\n"
}
1;
Anno
------------------------------
Date: Thu, 15 Mar 2007 11:52:44 +0100
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: Use different modules based on variable
Message-Id: <45f9257c$0$27379$ba4acef3@news.orange.fr>
geoffrobinson wrote:
> Hi,
>
> I have to do some error-checking on particular files. The routines I
> will be calling will have the same name, but they need to be from
> different modules based on a variable. (version 1.1, 2, etc.)
>
> I assume that if I write "use Module1" within an if-block it will only
> have scope within the if-block.
>
> I'm trying to think of other options as well. "use only" doesn't quite
> work because a single run of the script may want to call 1.1 and 2
> during the same run, but at different times. In other words if file 1
> needs a 1.1 error-checking routine, I want to use that, let it go out
> of scope, and then use 2.2 if file 2 needs 2.2.
>
> Please let me know if I am being unclear or confusing.
>
Uri has already suggested a dispatch table, but you could also try using
Factory and Strategy design patterns.
http://perldesignpatterns.com/?PerlDesignPatterns
should get you started, although its discussion of these particular
patterns is a bit limited.
Mark
------------------------------
Date: Thu, 15 Mar 2007 06:12:14 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Use different modules based on variable
Message-Id: <slrneviage.6tq.tadmc@tadmc30.august.net>
perlguru <manojkumargupta@gmail.com> wrote:
> On Mar 15, 11:39 am, Uri Guttman <u...@stemsystems.com> wrote:
>> >>>>> "p" == perlguru <manojkumargu...@gmail.com> writes:
>>
>> p> On Mar 15, 8:23 am, "geoffrobinson" <geoffrobin...@gmail.com> wrote:
>> >> work because a single run of the script may want to call 1.1 and 2
>> >> during the same run, but at different times.
^^^^^^^^^^^^
>> p> if ( $ver eq '1.1' ){
>> p> require "/usr/lib.../1.1/Module.pm";
>> p> Module::func();
>> p> }else{
>> p> require "/usr/lib.../2.2/Module.pm";
>> p> Module::func();
>> p> }
>>
>> that won't work at all. read the actual need which is to call either one
>> during run time in the same run.
^^^^^^^^^^^^
>> --
>> Uri Guttman ------ u...@stemsystems.com --------http://www.stemsystems.com
>> --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
>> Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
It is bad netiquette to quote .sigs.
> have you given a try ?
Have you read what has already been said (twice!)?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 15 Mar 2007 05:16:27 -0700
From: "fiedlert@gmail.com" <fiedlert@gmail.com>
Subject: Re: Use different modules based on variable
Message-Id: <1173960987.367540.210160@l77g2000hsb.googlegroups.com>
On Mar 14, 11:23 pm, "geoffrobinson" <geoffrobin...@gmail.com> wrote:
> Hi,
>
> I have to do some error-checking on particular files. The routines I
> will be calling will have the same name, but they need to be from
> different modules based on a variable. (version 1.1, 2, etc.)
Im not sure if this helps, but I have used the following on occasion.
BEGIN
{
eval "use x" if ...
eval "use y" if ...
}
regards
Ted
>
> I assume that if I write "use Module1" within an if-block it will only
> have scope within the if-block.
>
> I'm trying to think of other options as well. "use only" doesn't quite
> work because a single run of the script may want to call 1.1 and 2
> during the same run, but at different times. In other words if file 1
> needs a 1.1 error-checking routine, I want to use that, let it go out
> of scope, and then use 2.2 if file 2 needs 2.2.
>
> Please let me know if I am being unclear or confusing.
>
> thanks for any help in advance,
> Geoff
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 225
**************************************