Academic Tutorials



English | French | Portugese | German | Italian
Home Advertise Payments Recommended Websites Interview Questions FAQs
News Source Codes E-Books Downloads Jobs Web Hosting
Chats

CGI Programming
CGI Introduction
CGI Getting Started
CGI PERL Variables
CGI Environment Variables
CGI Processing Forms
CGI Advanced Forms
CGI Reading and Writing Data Files
CGI Working with Strings
CGI Server-Side Includes
CGI Working with Numbers
CGI Redirection
CGI Multi-Script Forms
CGI Searching Sorting
CGI Regular Expressions
CGI Perl Modules
CGI Date and Time
CGI Database Programming
CGI HTTP Cookies
CGI Modules
CGI Password Protection

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
CSS 1.0
CSS 2.0
HLML
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
CVS
Python
Apple Script
PL/SQL Tutorial
SQL Server
PHP
.NET (dotnet)
Microsoft.Net
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
VC++
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Photoshop Tutorial
Gimp Tutorial
Matlab
Gnuplot Programming
GIF Animation Tutorial
Scientific Visualization Tutorial
Graphics
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Weblogic Tutorial
SEO
Web Site Hosting
Domain Name
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Eclipse
J2ME
JBOSS
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Cobol
Assembly Language
Mainframe
Forth Programming
Lisp Programming
Pascal
Delphi
Fortran
OOPs
Data Warehousing
CGI Programming
Emacs Tutorial
Gnome
ILU
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills
Database Tutorials
Oracle
MySQL
Operating System
BSD
Symbian
Unix
Internet
IP-Masquerading
IPC
MIDI
Software Testing
Testing
Firewalls
SAP Module
ERP
ABAP
Business Warehousing
SAP Basis
Material Management
Sales & Distribution
Human Resource
Netweaver
Customer Relationship Management
Production and Planning
Networking Programming
Corba Tutorial
Networking Tutorial
Microsoft Office
Microsoft Word
Microsoft Outlook
Microsoft PowerPoint
Microsoft Publisher
Microsoft Excel
Microsoft Front Page
Microsoft InfoPath
Microsoft Access
Accounting
Financial Accounting
Managerial Accounting
Network Sites


PERL Variables


Previoushome Next






Chapter 2: Perl Variables

Before you can proceed much further with CGI programming, you'll need some understanding of Perl variables and data types. A variable is a place to store a value, so you can refer to it or manipulate it throughout your program. Perl has three types of variables: scalars, arrays, and hashes.

A D V E R T I S E M E N T

Scalars

A scalar variable stores a single (scalar) value. Perl scalar names are prefixed with a dollar sign ($), so for example, $x, $y, $z, $username, and $url are all examples of scalar variable names. Here's how variables are set:

    $foo = 1;
    $name = "Fred";
    $pi = 3.141592;
    

In this example $foo, $name, and $pi are scalars. You do not have to declare a variable before using it, but its considered good programming style to do so. There are several different ways to declare variables, but the most common way is with the my function:

    my $foo = 1;
    my ($name) = "Fred";
    my ($pi) = 3.141592;
    

my simultaneously declares the variables and limits their scope (the area of code that can see these variables) to the enclosing code block. (We'll talk more about scope later.) You can declare a variable without giving it a value:

    my $foo;
    

You can also declare several variables with the same my statement:

    my ($foo, $bar, $blee);
    

You can omit the parentheses if you are declaring a single variable, however a list of variables must be enclosed in parentheses.

A scalar can hold data of any type, be it a string, a number, or whatnot. You can also use scalars in double-quoted strings:

    my $fnord = 23;
    my $blee = "The magic number is $fnord.";
    

Now if you print $blee, you will get "The magic number is 23." Perl interpolates the variables in the string, replacing the variable name with the value of that variable.

Let's try it out in a CGI program. Start a new program called scalar.cgi:

Program 2-1: scalar.cgi Print Scalar Variables Program

    #!/usr/bin/perl -wT
    use CGI qw(:standard);
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    use strict;
    
    my $email = "fnord\@cgi101.com";
    my $url = "http://www.cgi101.com";
    
    print header;
    print start_html("Scalars");
    print <<EndHTML;
    <h2>Hello</h2>
    <p>
    My e-mail address is $email, and my web url is
    <a href="$url">$url</a>.
    </p>
    EndHTML
    
    print end_html;
    

You may change the $email and $url variables to show your own e-mail address* and website URL. Save the program, chmod 755 scalar.cgi, and test it in your browser.

You'll notice a few new things in this program. First, there's use strict. This is a standard Perl module that requires you to declare all variables. You don't have to use the strict module, but it's considered good programming style, so it's good to get in the habit of using it.

You'll also notice the variable declarations:

    my $email = "fnord\@cgi101.com";
    my $url = "http://www.cgi101.com";
    

Notice that the @-sign in the e-mail address is escaped with (preceded by) a backslash. This is because the @-sign means something special to Perl — just as the dollar sign indicates a scalar variable, the @-sign indicates an array, so if you want to actually use special characters like @, $, and % inside a double-quoted string, you have to precede them with a backslash (\).

A better way to do this would be to use a single-quoted string for the e-mail address:

    my $email = 'fnord@cgi101.com';
    

Single-quoted strings are not interpolated the way double-quoted strings are, so you can freely use the special characters $, @ and % in them. However this also means you can't use a single-quoted string to print out a variable, because

    print '$fnord';
    

will print the actual string "$fnord" . . . not the value stored in the variable named $fnord.


Arrays

An array stores an ordered list of values. While a scalar variable can only store one value, an array can store many. Perl array names are prefixed with an @-sign. Here is an example:

    my @colors = ("red","green","blue");
    

Each individual item (or element) of an array may be referred to by its index number. Array indices start with 0, so to access the first element of the array @colors, you use $colors[0]. Notice that when you're referring to a single element of an array, you prefix the name with $ instead of @. The $-sign again indicates that it's a single (scalar) value; the @-sign means you're talking about the entire array.

If you want to loop through an array, printing out all of the values, you could print each element one at a time:

    my @colors = ("red","green","blue");
       
    print "$colors[0]\n";		# prints "red"	
    print "$colors[1]\n";		# prints "green"
    print "$colors[2]\n";		# prints "blue"
    

A much easier way to do this is to use a foreach loop:

    my @colors = ("red","green","blue");
    foreach my $i (@colors) {
        print "$i\n";
    }
    

For each iteration of the foreach loop, $i is set to an element of the @colors array. In this example, $i is "red" the first time through the loop. The braces {} define where the loop begins and ends, so for any code appearing between the braces, $i is set to the current loop iterator.

Notice we've used my again here to declare the variables. In the foreach loop, my $i declares the loop iterator ($i) and also limits its scope to the foreach loop itself. After the loop completes, $i no longer exists.

We'll cover loops more in Chapter 5.


Getting Data Into And Out Of Arrays

An array is an ordered list of elements. You can think of it like a group of people standing in line waiting to buy tickets. Before the line forms, the array is empty:

    my @people = ();
    

Then Howard walks up. He's the first person in line. To add him to the @people array, use the push function:

    push(@people, "Howard");
    

Now Sara, Ken, and Josh get in line. Again they are added to the array using the push function. You can push a list of values onto the array:

    push(@people, ("Sara", "Ken", "Josh"));
    

This pushes the list containing "Sara", "Ken" and "Josh" onto the end of the @people array, so that @people now looks like this: ("Howard", "Sara", "Ken", "Josh")

Now the ticket office opens, and Howard buys his ticket and leaves the line. To remove the first item from the array, use the shift function:

    my $who = shift(@people);
    

This sets $who to "Howard", and also removes "Howard" from the @people array, so @people now looks like this: ("Sara", "Ken", "Josh")

Suppose Josh gets paged, and has to leave. To remove the last item from the array, use the pop function:

    my $who = pop(@people);
    

This sets $who to "Josh", and @people is now ("Sara", "Ken")

Both shift and pop change the array itself, by removing an element from the array.


Finding the Length of Arrays

If you want to find out how many elements are in a given array, you can use the scalar function:

    my @people = ("Howard", "Sara", "Ken", "Josh");
    my $linelen = scalar(@people);
    print "There are $linelen people in line.\n"; 
    

This prints "There are 4 people in line." Of course, there's always more than one way to do things in Perl, and that's true here — the scalar function is not actually needed. All you have to do is evaluate the array in a scalar context. You can do this by assigning it to a scalar variable:

    my $linelen = @people;
    

This sets $linelen to 4.

What if you want to print the name of the last person in line? Remember that Perl array indices start with 0, so the index of the last element in the array is actually length-1:

    print "The last person in line is $people[$linelen-1].\n";
    

Perl also has a handy shortcut for finding the index of the last element of an array, the $# shortcut:

    print "The last person in line is $people[$#people].\n";
    

$#arrayname is equivalent to scalar(@arrayname)-1. This is often used in foreach loops where you loop through an array by its index number:

    my @colors = ("cyan", "magenta", "yellow", "black");
    foreach my $i (0..$#colors) {
       print "color $i is $colors[$i]\n";
    }
    

This will print out "color 0 is cyan, color 1 is magenta", etc.

The $#arrayname syntax is one example where an #-sign does not indicate a comment.


Array Slices

You can retrieve part of an array by specifying the range of indices to retrieve:

    my @colors = ("cyan", "magenta", "yellow", "black");
    my @slice = @colors[1..2];
    

This example sets @slice to ("magenta", "yellow").


Finding An Item In An Array

If you want to find out if a particular element exists in an array, you can use the grep function:

    my @results = grep(/pattern/,@listname);
    

/pattern/ is a regular expression for the pattern you're looking for. It can be a plain string, such as /Box kite/, or a complex regular expression pattern.

/pattern/ will match partial strings inside each array element. To match the entire array element, use /^pattern$/, which anchors the pattern match to the beginning (^) and end ($) of the string. We'll look more at regular expressions in Chapter 13.

grep returns a list of the elements that matched the pattern.


Sorting Arrays

You can do an alphabetical (ASCII) sort on an array of strings using the sort function:

    my @colors = ("cyan", "magenta", "yellow", "black");
    my @colors2 = sort(@colors);
    

@colors2 becomes the @colors array in alphabetically sorted order ("black", "cyan", "magenta", "yellow" ). Note that the sort function, unlike push and pop, does not change the original array. If you want to save the sorted array, you have to assign it to a variable. If you want to save it back to the original array variable, you'd do:

    @colors = sort @colors;
    

You can invert the order of the array with the reverse function:

    my @colors = ("cyan", "magenta", "yellow", "black");
    @colors = reverse(@colors);
    

@colors is now ("black", "yellow", "magenta", "cyan").

To do a reverse sort, use both functions:

    my @colors = ("cyan", "magenta", "yellow", "black");
    @colors = reverse(sort(@colors));
    

@colors is now ("yellow", "magenta", "cyan", "black").

The sort function, by default, compares the ASCII values of the array elements This means if you try to sort a list of numbers, you get "12" before "2". You can do a true numeric sort like so:

    my @numberlist = (8, 4, 3, 12, 7, 15, 5);
    my @sortednumberlist = sort( {$a <=> $b;} @numberlist);
    

{ $a <=> $b; } is actually a small subroutine, embedded right in your code, that gets called for each pair of items in the array. It compares the first number ($a) to the second number ($b) and returns a number indicating whether $a is greater than, equal to, or less than $b. This is done repeatedly with all the numbers in the array until the array is completely sorted.

We'll talk more about custom sorting subroutines in Chapter 12.


Joining Array Elements Into A String

You can merge an array into a single string using the join function:

    my @colors = ("cyan", "magenta", "yellow", "black");
    my $colorstring = join(", ",@colors);
    

This joins @colors into a single string variable ($colorstring), with each element of the @colors array combined and separated by a comma and a space. In this example $colorstring becomes "cyan, magenta, yellow, black".

You can use any string (including the empty string) as the separator. The separator is the first argument to the join function:

    join(separator, list);
    

The opposite of join is split, which splits a string into a list of values. See Chapter 7 for more on split.


Array or List?

In general, any function or syntax that works for arrays will also work for a list of values:

    my $color = ("red", "green", "blue")[1];	
    # $color is "green"
    
    my $colorstring = join(", ", ("red", "green", "blue"));
    # $colorstring is now "red, green, blue"
    
    my ($first, $second, $third) = sort("red", "green", "blue");
    # $first is "blue", $second is "green", $third is "red"
    

Hashes

A hash is a special kind of array — an associative array, or paired list of elements. Each pair consists of a string key and a data value.

Perl hash names are prefixed with a percent sign (%). Here's how they're defined:

        Hash Name        key     value
    
        my %colors = (  "red",   "#ff0000",
                        "green", "#00ff00",
                        "blue",  "#0000ff",
                        "black", "#000000",
                        "white", "#ffffff" );
    

This particular example creates a hash named %colors which stores the RGB HEX values for the named colors. The color names are the hash keys; the hex codes are the hash values.

Remember that there's more than one way to do things in Perl, and here's the other way to define the same hash:

       Hash Name        key       value
       my %colors = (   red   => "#ff0000",
                        green => "#00ff00",
                        blue  => "#0000ff",
                        black => "#000000",
                        white => "#ffffff" );
    

The => operator automatically quotes the left side of the argument, so enclosing quotes around the key names are not needed.

To refer to the individual elements of the hash, you'll do:

    $colors{'red'}
    

Here, "red" is the key, and $colors{'red'} is the value associated with that key. In this case, the value is "#ff0000".

You don't usually need the enclosing quotes around the value, either; $colors{red} also works if the key name doesn't contain characters that are also Perl operators (things like +, -, =, * and /).

To print out all the values in a hash, you can use a foreach loop:

    foreach my $color (keys %colors) {
    	print "$colors{$color}=$color\n";   
    }
    

This example uses the keys function, which returns a list of the keys of the named hash. One drawback is that keys %hashname will return the keys in unpredictable order — in this example, keys %colors could return ("red", "blue", "green", "black", "white") or ("red", "white", "green", "black", "blue") or any combination thereof. If you want to print out the hash in exact order, you have to specify the keys in the foreach loop:

    foreach my $color ("red","green","blue","black","white") {
    	print "$colors{$color}=$color\n";   
    }
    

Let's write a CGI program using the colors hash. Start a new file called colors.cgi:

Program 2-2: colors.cgi - Print Hash Variables Program

    #!/usr/bin/perl -wT
    use CGI qw(:standard);
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    use strict;
    
    # declare the colors hash:
    my %colors = (	red => "#ff0000", green=> "#00ff00",
        blue => "#0000ff",	black => "#000000",
        white => "#ffffff" );
    
    # print the html headers
    print header;
    print start_html("Colors");
    
    foreach my $color (keys %colors) {
        print "<font color=\"$colors{$color}\">$color</font>\n";   
    }
    print end_html;
    

Save it and chmod 755 colors.cgi, then test it in your web browser.

Notice we've had to add backslashes to escape the quotes in this double-quoted string:

    print "<font color=\"$colors{$color}\">$color</font>\n";   
    

A better way to do this is to use Perl's qq operator:

    print qq(<font color="$colors{$colors}">$color</font>\n);
    

qq creates a double-quoted string for you. And it's much easier to read without all those backslashes in there.


Adding Items to a Hash

To add a new value to a hash, you simply do:

    $hashname{newkey} = newvalue;
    

Using our colors example again, here's how to add a new value with the key "purple":

    $colors{purple} = "#ff00ff";
    

If the named key already exists in the hash, then an assignment like this overwrites the previous value associated with that key.


Determining Whether an Item Exists in a Hash

You can use the exists function to see if a particular key/value pair exists in the hash:

    exists $hashname{key}
    

This returns a true or false value. Here's an example of it in use:

    if (exists $colors{purple}) {
        print "Sorry, the color purple is already in the hash.<br>\n";
    } else {
        $colors{purple} = "#ff00ff";
    }
    

This checks to see if the key "purple" is already in the hash; if not, it adds it.


Deleting Items From a Hash

You can delete an individual key/value pair from a hash with the delete function:

    delete $hashname{key};
    

If you want to empty out the entire hash, do:

    %hashname = ();
    

Values

We've already seen that the keys function returns a list of the keys of a given hash. Similarly, the values function returns a list of the hash values:

    my %colors = (red => "#ff0000", green=> "#00ff00",
                  blue => "#0000ff", black => "#000000",
                  white => "#ffffff" );
    
    my @keyslice = keys %colors;
    # @keyslice now equals a randomly ordered list of
    # the hash keys:
    # ("red", "green", "blue", "black", "white")
    
    my @valueslice = values %colors; 
    # @valueslice now equals a randomly ordered list of
    # the hash values:
    # ("ff0000", "#00ff00", "#0000ff", "#000000", "#ffffff")
    

As with keys, values returns the values in unpredictable order.


Determining Whether a Hash is Empty

You can use the scalar function on hashes as well:

    scalar($hashname);
    

This returns true or false value — true if the hash contains any key/value pairs. The value returned does not indicate how many pairs are in the hash, however. If you want to find that number, use:

    scalar keys(%hashname);
    

Here's an example:

    my %colors = (red => "#ff0000", green=> "#00ff00",
                  blue => "#0000ff",	black => "#000000",
                  white => "#ffffff" );
    
    my $numcolors = scalar(keys(%colors));
    print "There are $numcolors in this hash.\n";
    
    

This will print out "There are 5 colors in this hash."



Be the first one to comment on this page.




  CGI Programming eBooks
More Links » »
 
 CGI Programming FAQs
More Links » »
 
 CGI Programming Interview Questions
More Links » »
 
 CGI Programming Articles

No CGI Programming Articles could be found as of now.

 
 CGI Programming News

No News on CGI Programming could be found as of now.

 
 CGI Programming Jobs

No CGI Programming Articles could be found as of now.


Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previoushome Next

Keywords: PERL Variables, cgi tutorial, cgi tutorial pdf, history of cgi, basic cgi, syntax use in cgi, cgi training courses, cgi Download.

HTML Quizzes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
CSS 1.0 Quiz
CSS 2.0 Quiz
HLML Quiz
XML Quizzes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizzes
JavaScript Quiz
VBScript Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizzes
ASP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
CVS Quiz
Python Quiz
Apple Script Quiz
PL/SQL Quiz
SQL Server Quiz
PHP Quiz
.NET (dotnet) Quizzes
Microsoft.Net Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
VC++ Quiz
Multimedia Quizzes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Photoshop Quiz
Gimp Quiz
Matlab Quiz
Gnuplot Programming Quiz
GIF Animation Quiz
Scientific Visualization Quiz
Graphics Quiz
Web Building Quizzes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Weblogic Quiz
SEO Quiz
Web Site Hosting Quiz
Domain Name Quiz
Java Quizzes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Eclipse Quiz
J2ME Quiz
JBOSS Quiz
Programming Langauges Quizzes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Cobol Quiz
Assembly Language Quiz
Mainframe Quiz
Forth Programming Quiz
Lisp Programming Quiz
Pascal Quiz
Delphi Quiz
Fortran Quiz
OOPs Quiz
Data Warehousing Quiz
CGI Programming Quiz
Emacs Quiz
Gnome Quiz
ILU Quiz
Soft Skills Quizzes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz
Database Quizzes
Oracle Quiz
MySQL Quiz
Operating System Quizzes
BSD Quiz
Symbian Quiz
Unix Quiz
Internet Quiz
IP-Masquerading Quiz
IPC Quiz
MIDI Quiz
Software Testing Quizzes
Testing Quiz
Firewalls Quiz
SAP Module Quizzes
ERP Quiz
ABAP Quiz
Business Warehousing Quiz
SAP Basis Quiz
Material Management Quiz
Sales & Distribution Quiz
Human Resource Quiz
Netweaver Quiz
Customer Relationship Management Quiz
Production and Planning Quiz
Networking Programming Quizzes
Corba Quiz
Networking Quiz
Microsoft Office Quizzes
Microsoft Word Quiz
Microsoft Outlook Quiz
Microsoft PowerPoint Quiz
Microsoft Publisher Quiz
Microsoft Excel Quiz
Microsoft Front Page Quiz
Microsoft InfoPath Quiz
Microsoft Access Quiz
Accounting Quizzes
Financial Accounting Quiz
Managerial Accounting Quiz
Testimonials | Contact Us | Link to Us | Site Map
Copyright ? 2008. Academic Tutorials.com. All rights reserved Privacy Policies | About Us
Our Portals : Academic Tutorials | Best eBooksworld | Beyond Stats | City Details | Interview Questions | Discussions World | Excellent Mobiles | Free Bangalore | Give Me The Code | Gog Logo | Indian Free Ads | Jobs Assist | New Interview Questions | One Stop FAQs | One Stop GATE | One Stop GRE | One Stop IAS | One Stop MBA | One Stop SAP | One Stop Testing | Webhosting in India | Dedicated Server in India | Sirf Dosti | Source Codes World | Tasty Food | Tech Archive | Testing Interview Questions | Tests World | The Galz | Top Masala | Vyom | Vyom eBooks | Vyom International | Vyom Links | Vyoms | Vyom World | Important Websites
Copyright ? 2003-2024 Vyom Technosoft Pvt. Ltd., All Rights Reserved.