article,defines,subroutines,libraries,will,te
Quick Search for:  in language:    
article,defines,subroutines,libraries,will,te
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Perl Stats

 Code: 56,787 lines
 Jobs: 86 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for Perl.
Even Odd Guessing Game
By Jason DeLuca on 10/15


Include Function
By -Oz on 10/13


A New Quote Script
By Sean Murphy on 10/8


Click here to see a screenshot of this code!Get user input via form text box
By Tony G on 10/7

(Screen Shot)

Guessing Game
By Jason DeLuca on 10/4


Master P's Messenger
By P. Med on 10/4


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Simple Mailing List Subscribe Script

Print
Email
 

Submitted on: 10/19/2000 6:39:28 PM
By: Troy McKenna  
Level: Beginner
User Rating: Unrated
Compatibility:5.0 (all versions)

Users have accessed this article 5095 times.
 
 
     This article defines subroutines and libraries. It will teach you how to use a Perl library to create a simple mailing list subscribe script.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
"Simple Mailing List Subscribe Script"
by Troy McKenna
http://www.webprogroup.com/

One of the easiest ways to program with Perl is to use someone else's library. A library is a collection of subroutines. A subroutine is a little section or snippet of code or a mini program that does a specific task.

Subroutines are great for handling simple common tasks that you might need to do more than once in your script. For example, if you know that you're going to read data from a file several times in your script, instead of having basically the same code in several different places in your script, you can create one subroutine and use it each time you need to read information from a file.

As I mentioned before, a library is a collection of subroutines, but the great part is that it's a collection of subroutines already written by someone else! You don't have to know and understand exactly what they do and how they do it to use them. All you need to know is how to use them. I don't know exactly how my laptop works, but I know how to turn it on and use it!

Now let's get started...

First let's learn how to write a Perl script that will take a user's e-mail address from an HTML form and write that address to a mailing list file. I'll only include the most basic elements needed to do the job. In future tutorials, I'll build upon and improve this basic script.

I'll list the script first and then include a line-by-line explanation.

By the way, this tutorial is geared towards programming in a Unix environment with an Apache web server.

Here's the script:
--------------------------------------------------
#!/usr/bin/perl

# Full path to your mailing list file
$mail_list = '/usr/www/yourserver/mail_list.txt';

# URL to a "Thank You" page to send the user to after they
# submit the form
$thank_you = "http://www.yourserver.com/thankyou.html";

##################
# Actual script starts here

require "subs.pl";

# Parse the data from the input form
&p;

# Add the e-mail address to the list
&op_ap($mail_list, $FM{email}, e2, op_ap);

# Print the "Thank You" page
&pr_lc($thank_you, e2, pr_lc);

exit;
--------------------------------------------------

Now let's take a look at what each line does:

#!/usr/bin/perl -

Is the path to Perl on your server. If a script doesn't work right, this is one of the first places to look for problems.

You can find the path to perl for your server by typing "whereis perl" from a telnet prompt if you have telnet access to your server. Otherwise, if you don't know the path, you'll need to contact your host support to find out.

# Full path to your mailing list file -

This line is a comment. All comment lines begin with the pound sign '#'. This line is a comment describing the line below it.

$mail_list = '/usr/www/yourserver/mail_list.txt'; -

This line sets the variable, '$mail_list', to hold the value of the path to your mailing list file. You would need to change the path to match the actual path to the file on your server. Be careful to leave all punctuation exactly as you see it!

(For those of you who might be asking "What's a variable?", here's an explanation. If you have information that you want to use in several places in your script, like the path to your mailing list, instead of typing the whole thing several times, you can assign a placeholder with a shorter name to hold or equal the value of the path.)

They're called variables because although the name of the variable stays the same, you can change the value, the information stored in the variable, anywhere in the script. This type of variable is called a scalar variable. They can hold numbers, strings (letters), or references. They begin with a dollar sign followed by one or more letters, numbers or underscores.) Although we don't use the variables in this script more than once, it's a good idea to learn how to start using them.

# URL to a "Thank You" page to send the user to after they # submit the form -

These lines are more comments describing the line below them.

$thank_you = "http://www.yourserver.com/thankyou.html"; -

This line sets the variable '$thank_you' to equal the path to an HTML thank you page you would like users to see after they subscribe to your mailing list.

##################
# Actual script starts here -

Just another comment line.

require "subs.pl"; -

This line makes the script require the library of subroutines. All the subroutines are contained in the file named 'subs.pl'. This line allows this script to use the subroutines without actually having to include them in the script. The 'subs.pl' file must be in the same directory that you put the script in. (I'll let you know at the end of my article how to obtain the 'subs.pl' file.)

# Parse the data from the input form -

Another commenting letting you know what the next line does...you can never have too many comments!

&p; -

This line is where we actually start using the subroutines I've been talking about. This line calls or runs the subroutine named 'p'. Note perl is case sensitive. This means that '&p;' and '&P;' are NOT the same thing. Also $mail_list and $Mail_List are entirely different variables.

# Add the e-mail address to the list -

Yep...another comment.

&op_ap($mail_list, $FM{email}, e2, op_ap); -

This line calls the subroutine 'op_ap' which stands for open_append. We want to open your mailing list file and append the user's e-mail address to it. This time we need to send the subroutine some information to work with.

As I said earlier, the great thing is that you don't need to understand how the subroutine works, you just need to know which information it needs and what order to send it in. If the subroutine is sent the right info in the correct order, it knows what to do with it. The information or data is sent in the parenthesis ($mail_list, $FM{email}, e2, op_ap).

The first part '$mail_list' is the variable which contains the path to your mailing list file. This tells the subroutine which file to open and append.

The second part is another special variable which holds the e-mail address of the user. This variable was created by the subroutine 'p' which we called earlier. This line assumes that there is a text box in your submittal form named 'email' for users to enter their e-mail address in. If your form used a different name like 'address' for this field, you would use '$FM{address}' in place of '$FM{email}'. (Remember - $fm{email} is NOT the same as $FM{email}!).

The next part 'e2' is the name of another subroutine, it's an error subroutine that returns an error message if the script runs into any problems. I'll explain more about the error subroutines in future tutorials.

The last part 'op_ap' is just the name of the subroutine that you're calling. This is also used in error reporting to help track down where the error is at.

# Print the "Thank You" page -

The last comment line!

&pr_lc($thank_you, e2, pr_lc); -

Here we call another subroutine. Yep, you guessed it, it stands for 'print_location'. Again, we send it some information to work with. '$thank_you' tells it the URL to print, 'e2' is the name of the error subroutine to use if it needs to and 'pr_lc' is just the name of the sub.

exit; -

We're done with the script so we just tell it to 'exit'!

__________

That's it! That's the entire script!

Enjoy!

Troy McKenna

To download a copy of the "subs.pl" file, click here.  Remember, both the script above and the "subs.pl" file should be in the same directory and should be chmoded 0755 or rwx-rx-rx and your mailing list file should be chmoded 0777 or rwx-rwx-rwx.


Other 2 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
5/24/2001 2:18:34 PM:Chad
this is a very helpful article. I am trying to download the subs.pl file and the server that I am on will not let me access the file. Could someone send the file to me at vipergtsrz@hotmail.com please? Also, I was wondering if you could show how to do an unsubscribe script in case users wanted to remove themselves from the mailer. Thanks a lot. ~Chad
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/24/2001 2:21:09 PM:Chad
I just wanted to say that this article helps me a lot. I was also wondering if anyone has that 'subs.pl' file and can e-mail it to me at vipergtsrz@hotmail.com because the server that I am on (for some reason) doesn't allow me to access the file to dl it. Also, I was wondering if someone could tell me how to go about making an unsubscribe Perl script in case a user doesn't want to be a part of your list anymore. Thank you very much.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/29/2001 11:10:16 AM:JA
When you give a path of - $mail_list = '/usr/www/yourserver/mail_list.txt'; - - could you elaborate more on what you mean by
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/29/2001 11:12:19 AM:JA
[ctd from above] 'yourserver' - do you mean www.mysite.com, mysite.com, or myserver.myhost.net, or 213.239.5x.xx (my IP address) - a bit confused on this Thx Ja
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/28/2002 2:39:12 PM:Dave
The link to the subs.pl is not working please help. This is exactly what i am looking for. But i need the subs.pl file. Thanks
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Perl Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.