FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


executing a perl script from within a script





giovle
Hi,

I'm fairly new to Perl programming and have just finished writing some Perl script for my site. I also made a subscription module for a mailing list. Besides that module I also have a contact form to email me comments and suggestions. Now I'd like to add a checkbox to the contact form to enable users to subscribe to the mailing list when sending a message. That way, people can subscribe when they send a message in stead of sending the message and then still having to send a request to enter the mailing list.
Now, the subscription script is already written and operative (cause I still want the seperate module to remain on the site) And I also have a script that handles the messages sent by the contact form (with FormMail).

But to add the extra functionality of the checkbox to subsribe, I was wondering if it was possible to execute the small subscription script from within the FormMail script ... Kinda like a module (but it's not, since you cannot install your own modules on the frihost server)

Anyone got any ideas ...

Grtz

Gio
MrBlueSky
I am not totally sure what you mean, but you actually can use a Perl module. If you put your Perl-module (for example "YourModule.pm" in the same directory as your perl-script, you can use it like this:

Code:

use lib '.';
use YourModule;


This way you can also install aditional modules from CPAN at frihost: you can copy them to some of your own directories, and then use the 'use lib' option to include them.
dayveday
You definitely can use your own perl modules on frihost - I know I am! I don't even have to add the
Code:
use lib '.';


Another way you can get around this, if you don't want to rewrite the subscription script as a module. The way you can do this is to execute the script directly using backticks.
Code:
my $resp = `/usr/bin/perl ./subscript.pl MYARGS`;

Or something like that... let me know if you have any problems getting that to work. It will depend on how you're getting the CGI variables (are you using CGI.pm?) for the exact details.

Make sure you're really, really careful whenever you use backticks. If someone posts some malicious data, it is possible that it will subvert what you meant when you pass it through as arguments to the script. For example, you might need to pass the username to the script. But a user could send a pipe character with a 'delete everything' command - which will really mess with your system. To help avoid this, turn on taint checking and use regular expressions to make sure the user can only pass through alphanumeric strings.

Hope that makes some sense![/code]
dayveday
btw. here's a little info on taint checking and why its important
http://gunther.web66.com/FAQS/taintmode.html

You should always enable it, even if you don't think you'll need it. That way, you're less likely to get yourself in trouble.
giovle
Thx,

Well I wrote a script to handle our 'calendar' of the band. The event-page on our homepage lists all our past and future gigs. I created the site myself and the other members wanted to contribute to the site to keep it updated. Therefor I created a 'user-only' page where he can fill out a form to enter a new event. Those forminputs are used in a script that first of all adds those inputs to a database file and then remakes the event-pages (in three languages) This way I don't have to do it all myself or have to teach the others how to add it in html language themselves...

Now when I discover a typo or anything wrong in the texts, I can open and edit the database file. But in order to rewrite the pages automatically, I needed to copy the html-rewrite part form the other script, to a new one, that just needs to update the pages with the corrected database.

So now I have two scripts, of which one of them consists entirely of things that are in the other one as well. Therefor I thought it would be nice if I could just tell the 'entry-and-update script' to use the other 'just-update script'. The 'just-update script' doesn't need any variables so how do I make that work then?
giovle
I just read a small article about three ways to execute a script: backticks, system() and exec(). Can anyone tell me which one to use. It might help knowing that the first script has an append-to-database-method and then just does the same the update script does.

Thx
dayveday
Hey, as I said, I probably would avoid trying to 'execute' the scripts - you can do it all in Perl using perl modules.

To do this, copy all the common methods into a new file which you should name MyModule.pm (or something similar). Then, you simply type
Code:
use MyModule;
to actually get that code into your perl script. There's a little more to creating the module than I discribed here, but most of what you'll need to do is just cut and pasting. Let me know if you want details.

Otherwise, if you really can't be talked out of executing the other script, backticks is probably what you're after. system() and exec() will also work, but they're more complicated than you need. When you use backtick strings, what is inside the backticks will behave exactly as if you had typed it on the shell. The value of the string will become what that command returns on its standard out. So
Code:
my $dir = `ls .`;
would return that output of ls when called on the current directory.

Like I said before, make sure you're very careful when doing this. If you pass any CGI variables to the script in the backticks you need to make sure that a malicious user couldn't rework your call to do something more sinister (like delete all files).
giovle
dayveday wrote:
Hey, as I said, I probably would avoid trying to 'execute' the scripts - you can do it all in Perl using perl modules.

To do this, copy all the common methods into a new file which you should name MyModule.pm (or something similar). Then, you simply type
Code:
use MyModule;
to actually get that code into your perl script. There's a little more to creating the module than I discribed here, but most of what you'll need to do is just cut and pasting. Let me know if you want details.

Otherwise, if you really can't be talked out of executing the other script, backticks is probably what you're after. system() and exec() will also work, but they're more complicated than you need. When you use backtick strings, what is inside the backticks will behave exactly as if you had typed it on the shell. The value of the string will become what that command returns on its standard out. So
Code:
my $dir = `ls .`;
would return that output of ls when called on the current directory.

Like I said before, make sure you're very careful when doing this. If you pass any CGI variables to the script in the backticks you need to make sure that a malicious user couldn't rework your call to do something more sinister (like delete all files).


Hi,

well, I'd prefer using backticks then, to spare myself the extra work ... (yes I'm lazy, but what do you expect from an average student: they're as lazy as they can be, and they never have any money on them Wink ). Since I'm not passing anything to the script that has to be executed, it's not a real safety issue then, is it?

But just for the sake of knowing it and absorbing as much knowledge while I can: does anyone have a tutorial or an explanation in common words about the workings of system() and exec()...

I thought I read somewhere that the disadvantage of backticks is, that you store whatever the script outputs ... That's not necessary at all. On the contrary, if it was up to me, I'd be even more happy if the script were to execute another one and hereby terminating itself.
MrBlueSky
giovle wrote:


But just for the sake of knowing it and absorbing as much knowledge while I can: does anyone have a tutorial or an explanation in common words about the workings of system() and exec()...



System and exec: http://www.unix.org.ua/orelly/perl/learn/ch14_01.htm#ch14-23962
Backquotes: http://www.unix.org.ua/orelly/perl/learn/ch14_02.htm

From Learning Perl by Schwartz, Christiansen and Wall.

And a tutorial from Schwarz on system, exec and backquotes can be found here: http://www.stonehenge.com/merlyn/LinuxMag/col05.html
dayveday
If you don't want control to return to your program, then you probably want to use exec(), 'cause that's exactly what it does.

I always find the normal Perl docs more than helpful:
http://perldoc.perl.org/functions/exec.html
(you will probably have a local copy on your computer that was installed alongside Perl).
giovle
Thx for all the reactions, I used the system() tags though, cause I realised that I had to return to the first program in order to be able to display a confirmation page ... But everything worked out alright. Now I reduced the size of one of the two programs to a mere 20 lines in stead of 200. Not that it's that important, but I'm always learning, no?

Thx for the effort everyone, I appreciate it!

See u for my next problem Razz
Related topics
writing COM components in Perl
Perl Script - Internal Error 404 File not found
Perl Script - checking if feilds are complete
CGI FormMail Script Part II
executing a perl script
Show ip adress from perl -script need help
CGI/Perl Script to backup a website.
PERL - post data from one script to another (Without LWP)
I have a perl Script.. but i dont know how to use.
im installing a perl script in my website and..
Running a Perl script in the background
HELP needed with basic Email perl script.
Problem with perl script
Executing a Perl CGI script
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.