On Mon, Feb 12, 2007 at 10:26:26PM +0100, Ana Saiz García wrote:
> Hello
> 
> First of all, I apologize if this is not the right list to ask my question,
> but as I am a perl beginner, I think it is the most suitable list for me :o)
> 
> So here goes my question:
> 
> I have a main program which will call a subroutine, say "S", belonging to
> another module. The question is that there could be several modules that
> contain a subroutine also named "S", which basically will return equivalent
> values to the main program.
> 
> My problem is that I don't want to harcode the name of those modules (for
> example using an "if"), because I don't know a priori how many there will be
> or which will be needed to call at any moment. The idea is that there will
> be a file in which the main program could read which module is necessary in
> each moment, and so call it.
> 
> So the question is: Is there any way to call a subroutine without explicitly
> writing his name?
> 
> Thanks in advance, and if I haven't been able to explain it well, please ask
> me :o)

Off the top of my head, the only way to approach what you want is to use
eval().  Use perldoc -f eval to learn more about it.  Just populate a
variable with the package-and-subroutine name you want, then stick that
variable name in an eval() in whatever way is convenient to use the
subroutine.  You can thus use whatever control structures are necessary
to populate the variable with the specific package-and-subroutine you
need at a given moment.

There may also be some object oriented magic you can do, or something
else involving coderefs, but nothing leaps to mind right now.  It's
difficult to really figure out how to accomplish what you're doing when
I don't know what criteria you want to use to choose the specific
subroutine you're going to use at a given moment.

Best o' luck.

-- 
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
unix virus: If you're using a unixlike OS, please forward
this to 20 others and erase your system partition.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to