Gossamer Forum
Home : General : Perl Programming :

calling a sub from external file

Quote Reply
calling a sub from external file
I am trying to call a sub from an external file. I have tried putting a require at the top of the main file, but when I access the main file from the browser it opens the required file instead of the one that I am calling.

The way I currently have the main file set up, the user adds the info into the form it then adds the users information to the data files, then sends an email, then I am wanting it to go to the sub in the external file.

How do I call the sub?

Thanks

Jimmy Crow
http://www.homewithgod.com/
Quote Reply
Re: calling a sub from external file In reply to
when we call a subroutine from an external file, its just the following:

at or near the top of our main script
require "subroutine.ext"; < where subroutine.ext is your external file.

Then wherever we are going to call the subroutine, we put the following into our script
&subroutinename; < where subroutinename is the name of the subroutine needed from the external file.

Harrison


"I've got if's pretty good, but that's about it"
Quote Reply
Re: calling a sub from external file In reply to
Thanks for the quick reply Harrison. That is what I originally tried, but when I try to access the main file it opens the partner.pl instead.

This is at the top of the add.pl :

require "config.pl";
require "partner.pl";

print "Content-type: text/html\n\n";

&form_info;

if ($form{'add'}) {&add; }
elsif ($form{'preview'}) {&preview; }
elsif ($form{'info_team}) {&info_team; }
elsif ($form{'signed'}) {&add; }
else { &sign; }


then at the end of the file I call it like this:

if ($form{'isMember'} == "1") {
&info_team;
}

The main file is add.pl, I am calling it like this http://www.mydomain.com/cgi-bin/add.pl which should go to the sub sign. The after all the work in this file is finished I am wanting to call &info_team which is in the partner.pl. But instead of going to the &sign it goes directly to &info_team in the partner.pl

Can you tell what I am doing wrong from the above codes?

Thanks again.

Jimmy Crow
http://www.homewithgod.com/
Quote Reply
Re: calling a sub from external file In reply to
when you use require it executes the code within the included file. generally, when you want to have an external file for specific functions, you don't use stand-alone scripts. rather, you have a file full of useful subs that can be called.
at any rate, using 'use' with packages is always a safer and more solid technique for this kind of thing.

-- Gordon


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: calling a sub from external file In reply to
Thanks Gordon. I'm still a newbie, how would I use "use" to make this work. The reason I was wanting to split this file up is because is getting so large it is hard to read and keep up with what is going on in it.

Jimmy Crow
http://www.homewithgod.com/
Quote Reply
Re: calling a sub from external file In reply to
there are various ways to use 'use' from the standard Exporter module to type globbing and overriding the standard name space normalities.
to start, i would suggest using
perldoc Exporter
to get more details on using the Exporter module or go to
http://www.perl.com/...ml/lib/Exporter.html

-- Gordon


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: calling a sub from external file In reply to
Thanks Gordon for all your help and suggestions. It's great to have someone to help shorten the learning curve!



Jimmy Crow
http://www.homewithgod.com/
Quote Reply
Re: calling a sub from external file In reply to
no problem. i know it can seem insurmountable at first.
be sure to look at the link to
http://www.perl.com/...tml/pod/perlmod.html
on the Exporter man page. the online docs are pretty brief when it comes to first learning things so i would really suggest getting Programming Perl (the 3rd edition is now out) to have a solid reference to answer all questions you might have. You can get it at an incredible discount at
http://www.bookpool.com/...re6734/sm/0596000278
if you want to really start growing with Perl.

-- Gordon


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';