Home Lesson 9: Hints when writing scripts (Shhhhh! Top Secret!)
Home
FAQ
Uses of Perl
Perl Lessons
The Experience
Testing Area
Perl Links
Guestbook
About the Site

Sometimes, you may want to link two or more scripts together through an html page.

script -> html -> script2

There are a few ways to pass information from the first script to the second one. If you are using a form on the html page, then you can use a special type of input called hidden. It simply acts as a text box, except that the user can't see it, and he can't edit it. When the form is submitted, the contents of the hidden box are sent along with what ever else information the user supplied.

Be aware that the user can look at the source for the page that you send him and see the value of the hidden box. Therefore, you may want to be careful if you are sending sensitive information.

You could also save what you want to send to the second script to a file, and then the second script could read the file. Or, you could combine these two methods, writing to the file and sending the name of the file through the hidden box and then the second script knows where the information is stored.


Another thing you can do instead of creating two separate scripts is put it all in one script. Each action should be in a large if statement. What you would do is somewhere on the form, put a hidden box, and in the value put something that will signify what you want the script to do with this information. So here is what the script would look like:

...headers...
$action=$in{'action'}; #the hidden box is called action

if ($action eq "dostuff") {
...
#stuff to do
...
}
elsif ($action eq "dootherstuff") {
...
#different stuff to do
...
}

So now you can consolidate all of your scripts into one humongous script which will take over the world for you while you sleep.


You may be wondering about how you would go about copying files or other things that might be easy to do in UNIX or your OS of preference from within your perl script. There is a very simple command that does this:

system("cmd");

Where cmd is whatever you want to do. For example, if you had a template file, named template.txt and you wanted to copy it, then do something to it, then all you would have to do is this:

system("cp template.txt new.txt");

open (OUT, ">>template.txt");

print OUT "dostuff";

close OUT;

and so on...


If you get in the habit of making many scripts, then you might want to make a template file, which would have the headers that you start off every script with. When you want to start a new script, then just copy the template with a new file name and edit that. Then you don't have to remember the annoying headers.


There are already many versions of almost any type of script you can think of on different perl archives. Here's a good example:

http://www.perl.com/CPAN-local//CPAN.html

You can go to them and look for what ever you need, then download it and customize it if you need to. There's no need to reinvent the wheel. Just modify what already exists, as long as you don't illegally copy some one else's work.

Chapter 8: Writing your script-will it turn into a movie? Home The Experience


<<< Chapter 8 | Home | The Experience >>>