IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Perl
Search Perl Group:

Advanced
-Collapse +Expand Perl To/From
To/FromCODEGuides
-Collapse +Expand Perl Store
PRESTWOODSTORE

Prestwood eMagazine

March Edition
Subscribe now! It's Free!
Enter your email:

   ► KBWebsite Scri...PerlBeginners Co...   Print This     
  From the January 2016 Issue of Prestwood eMag
 
Perl Beginners Corner:
A 10 Minute Perl Quick Start
 
Posted 16 years ago on 3/13/2003 and updated 1/29/2009
Take Away:

A short 10 minute Perl primer. Get started in Perl now!

KB100138



Got 10 minutes? Want to get started with Perl?

This article is part of our series of 10 Minute Quick Starts. Each quick start is step by step, assumes you know very little about the subject, and takes about 10 minutes. You can use them to scratch the service of areas you want to learn and as a quick review when returning to something after a long absence.

Perl

Common Gateway Interface (CGI) lets you run CGI scripts written in various scripting languages such as Perl and PHP. With CGI,  you can add interactive and dynamic content to your Web page. If you are setting up your own webserver, you have to install Perl. If you're website is hosted with a hosting service, ask them if they support Perl and read up on any special rules they may have imlemented. Either way, you'll probably also want to setup a webserver and install Perl on your development computer too so you can easily and quickly develop code.

Install Perl

The first thing you must do is to install Perl or use a hosting service with Perl installed (most do offer Perl). If your web site is hosted with Prestwood, then your web site does have Perl installed. If you're hosting your web site with someone else, check with them to see if it is installed.

Installing Perl

[Optional] If you are developing on your development box, let's start by making sure Perl is installed.

  1. Webserver. First you need a web server supported by a Perl installation.
  2. Test if installed or not. If you're maintaining your own server, go to the server and execute the following command from a command prompt:
     
    perl -v
     
    If Perl is installed, the Perl version information will be displayed.
     
  3. Install Perl if needed. There are many flavors of Perl available today. It is available for Windows, Mac, Unix, Amiga, and Linux to name a few. Click here to download the latest version of ActivePerl for Windows, Linux, or Solaris.

And here, we, go...

Part 1: First DOS Box Scripting Test

[Optional] If you are developing on your development box, let's start by printing Hello World either to a command prompt (DOS or Unix).

  1. Create test.pl script file. If you have Perl installed locally (or you're working on the server), type and save the following Perl script as test.pl:
     
    print("Hello");
     
    Note: Perl is case sensitive so make sure you use all lowercase for the print command. Also, do not forget the statement ending semi-colon.
     
  2. Run simple test.pl script. At the command prompt, type the following to execute the program:
     

    perl test.pl

     
    If all went well, you should have seen "Hello" printed to the screen. This tells you that you have Perl installed correctly.
     

  3. Try a syntax variation. With the print command you can also leave off the () and just use a space to separate the print command and the literal string. Add the following line of code to your short test.pl script:
     
    print "I am a Perl coder!";
     
  4. Run your Perl program again.

Part 2: Browser Example

This next example creates a complete HTML page from Perl.

  1. Create helloworld.pl script file. Type the following Perl script into an editor and save it as text in a file named helloworld.pl.
     
    #!/usr/local/bin/perl -w
    print("Content-type: text/html\n\n");
     
    #Start of HTML page.
    print("");
    print("Perl Tutorial");
    print("");
    print("Hello World");
    print("");

     
  2. Upload to server. Next copy or FTP your short Perl script to your server. Many servers are configured to allow Perl scripts to execute only from a /cgi-bin/ folder. Check with your hosting provider and ask which folder they have configured to run Perl scripts.
     
  3. Execute script. Open a browser and browse to the URL of the script. Something like:
     
    http://www.yourhostedserver.com/cgi-bin/helloworld.pl
     
    Note: Some hosting services use cgi-local for customer scripts and cgi-bin for hoster-provided scripts.
     
    Or, if you are developing locally on your development computer, you will browse to something like:
     
    /cgi-bin/helloworld.pl

    Here is sample output in the Google Chrome browser:

Code Explanation

#!/usr/local/bin/perl -w

The first line of this script is a special comment that indicates the location of the Perl program. Depending on your setup, you may be able to leave this line off.  Unix users must include this line with the correct full path to the Perl program.

The -w Command Line Option

The command line optional option -w on line 1 tells Perl to syntax check your code for errors and if any are found, display them. This command line option is often removed just prior to deploying your script.

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

This line tells Perl to write out a standard HTML text-based page. It sets the mime type.
 
If you view the source code for the returned browser page, you'll see it's HTML code:

 

#Start of HTML page.

This is a regular comment. Perl uses a # for comments and you should get in the habit of commenting your code liberally.

print("");
print("Perl Tutorial");
print("");
print("Hello World");
print("");

These print commands write out a standard typical HTML page. Notice Perl uses semi-colon to mark the end of statements. Leave off a semi-colon and refresh your browser to see what type of error is generated.

More Info

Download:  ActivePerl Distribution (Install Perl)
Code:  Perl Development Tools
Code:  Perl Overview and History

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1263A1
Enter key:
Article Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #100138 Counter
20203
Since 4/2/2008
-
   Contact Us!
 
Have a question? Need our services? Contact us now.
--Mike Prestwood

Call: 916-726-5675

email: info@prestwood.com


Go ahead!   Use Us! Call: 916-726-5675 


©1995-2019 Prestwood IT Solutions.   [Security & Privacy]