Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing PERL functions from another PERL file? 1

Status
Not open for further replies.

naq2

Programmer
Aug 3, 2004
74
FR
Hi there,

Just a simple question: How do I access a function that is in FILE2.pl when I'm in FILE1.pl?

Thanks for your help.
 
What are you trying to access in file2.pl? Is it a sub routine, a variable? Normally it is just better to incorporate it into one file and seperate the two scrips with a header and refer to each one by a input value such as sending a variable such as true or false (0,1) to the script and haveing a header to direct where to go. That way you can manage everything together. If the script needs to die at a certain point just tell it to.
 
I'm sorry I didn't get you properly. Can you explain me again?

And yes, I'm trying to access a subroutine.

Thanks anyway.
 
Is there any reason you can't just copy the subroutine from file2.pl to file1.pl? Example of what I call a perl header:

Code:
$input # something sent to the script to direct it's path such as a hidden form element with a value assigned

if($input == 0){&sub1}
elsif($input == 1){&sub2}

########Sub 1#######
sub sub1
# your sub here



#######Sub 2########
sub sub2

#your sub here
 
Let me explain you a little bit more what I need to do:
I need to call a function (subroutine) in a separated file; and I'm only able to know which file at runtime.
Here is an example of what my code could look like:
Code:
if ($extractedString eq "TOTO") {
    # call function "run" in file toto.pl
} elsif ($extractedString eq "TATA") {
    # call function "run" in file tata.pl
} else {
    # call function "run" in garbadge.pl
}

Thanks for your help.
 
Your idea is very good but I can't use it as I can't stock all the possibilities before executing the script.

Do you have any other ideas?

Thanks anyway.
 
So let me get something straight. When you want to call the other perl scripts you want to use only a sub routine of the "called" script, and do not want it to execute completely?
 
Only one precise function.
 
Ok first in the alternate scripts you will need to note where to begin and end the "Percise Function". In the script where I have value = "Call Variable" you can use CGI; and Get->param('FormType') to get the variable. This will help you assign the start and end of the alternate files.

Code:
#!/usr/bin/perl
print "Content-type: text/html\n\n";



print <<header;
<html>
<head>
<title>Page Auto Forward</title>
</head>

<body onLoad="send()">
<SCRIPT LANGUAGE="javascript">
function send()
{document.theForm.submit()}
</SCRIPT>
header

if ($extractedString eq "TOTO") {     # call function "run" in file toto.pl
print <<TOTO;
<form name="theForm" method="post" Action='toto.pl'>    
<input type="Hidden" Value = "Call Variable" name="FormType">
</form>
TOTO

} elsif ($extractedString eq "TATA") {   # call function "run" in file tata.pl
print <<TATA;
<form name="theForm" method="post" Action='tata.pl'>    
<input type="Hidden" Value = "Call Variable" name="FormType">
</form>
TOTO
} 
else {   # call function "run" in garbadge.pl
print <<garbadge;
<form name="theForm" method="post" Action='garbadge.pl'>    
<input type="Hidden" Value = "Call Variable" name="FormType">
</form>
garbadge
}

print "</body></html>";


What this does: It wil write a HTML page that has a javascript function that submits the form automatically (Auto-forwarder). The first CGI script will denot what the ACTION of the form is. Hope this is what your looking for.
 
I really appreciate all the effort you're doing in helping me.

If I wanted to do this: I would first use HTTP redirections. I don't know if you're aware of this:
Code:
print $CGIHandle->redirect(-url => "toto.pl") ;
It would have been much faster.

The problem with your solution is that the source code is interupted when calling the second function and I CANNOT return and continue the previous one at the end of the function.

I'm more looking for something like ;
Code:
if ($extractedString eq "TOTO") {
    import("TOTO.pl") ; $exe = "run()" ; eval($exe) ;
}

I feel sorry for all the pain I caused you! :S
Hope you won't be to mad against me! :)

Thanks anyway.
 
Have you looked at the require function in Perl

This allows you to import the functions from another script into the namespace of the current script.

very useful for setting config details

Code:
require "config.pl";

You'll have to watch how you name your subs though

HTH
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top