Referring to the calling script from a module

2005-12-02 Thread vmalik

Hi all,

I don't know if the question that I am going the ask fits the beginner level,
but I am certainly a beginner in perl. 

Is it possible for a module to refer to its calling script? For example, if I
create a module called MyModule.pm, and use it in a script MyScript.pl by saying
"use MyModule;", is it possible to refer to the calling script (MyScript.pl in
this case) and get some information about it during the process of writing the
module (MyModule.pm in this case)? This way, I want to make the behaviour of the
module flexible based on where it is called.

If I'm completely nuts and there's no way this can be done, is there anything
else in perl which can help me acheive this sort of "dynamic behaviour
manipulation"?

Thank you.

Vishal



This mail sent through www.mywaterloo.ca

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Referring to the calling script from a module

2005-12-02 Thread Charles K. Clarkson
[EMAIL PROTECTED]  wrote:

: Is it possible for a module to refer to its calling script?

I'm not sure what you mean by "refer to", but you can
find the caller's name with the perl "caller" function. You
can find a practical example of its use here.

http://perl.plover.com/yak/hw1/Hardware-notes.html#Writing_a_Module_that_Exp
orts_a_

HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328

 . . . With Liberty and Justice for all (heterosexuals).


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Referring to the calling script from a module

2005-12-02 Thread Xavier Noria

On Dec 2, 2005, at 21:52, [EMAIL PROTECTED] wrote:



Hi all,

I don't know if the question that I am going the ask fits the  
beginner level,

but I am certainly a beginner in perl.

Is it possible for a module to refer to its calling script? For  
example, if I
create a module called MyModule.pm, and use it in a script  
MyScript.pl by saying
"use MyModule;", is it possible to refer to the calling script  
(MyScript.pl in
this case) and get some information about it during the process of  
writing the

module (MyModule.pm in this case)?


Yes. When the module is used the import() function, if any, is  
called. There caller() returns what you want:


% cat tmp/Foo.pm
package Foo;

sub import {
print((caller)[1]);
}

1;

% cat tmp/foo.pl
use Foo;

% perl -Itmp tmp/foo.pl
tmp/foo.pl

You can assign that to a package variable or whatever.


This way, I want to make the behaviour of the
module flexible based on where it is called.


Nevertheless, this sounds strange. Without more information looks  
like the module is parametrizable and the script needs to make the  
proper calls or proper module set up. Using caller() for that looks  
suspicious.


-- fxn



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Referring to the calling script from a module

2005-12-02 Thread Shawn Corey

[EMAIL PROTECTED] wrote:

Is it possible for a module to refer to its calling script? For example, if I
create a module called MyModule.pm, and use it in a script MyScript.pl by saying
"use MyModule;", is it possible to refer to the calling script (MyScript.pl in
this case) and get some information about it during the process of writing the
module (MyModule.pm in this case)? This way, I want to make the behaviour of the
module flexible based on where it is called.


Modules are not called; they are loaded. Subroutines in modules are 
called. Unless requested, I shan't get into the difference between 
compiling and running, yet alone modules, packages, and objects.


You can refer to the script name with $0. You could also do something 
like this:


# In the script
BEGIN {
  our $ScriptName = 'myscript';
  our @ModuleNames = ();
}

our @ModuleNames;
use mymodule;

__END__


# In each module
our @ModuleNames;
push @ModuleNames, 'this_module_name';

# Alternate
our @ModuleNames;
use File::Basename;
push @ModuleNames, basename( __FILE__ );

1; # All modules must return a non-false value
__END__


--

Just my 0.0002 million dollars worth,
   --- Shawn

"Probability is now one. Any problems that are left are your own."
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]