Quick jump:  What's new

 
Go to:
 
Weblog: Modern Perl Programming Weblog 
in Hackathon Chicago, Nov 10-12, 2006
First Post in Modern Perl Programming Weblog

This Perl programming tutorial is a great scripting guide to help you fully understand Perl script. Find Perl tutorials and programming examples to master your knowledge of Perl Scripting.

Perl programming tutorial

1. Using The Perl interpreter

1.1. Find Perl Interpreter

which perl 

http://linuxconfig.org/images/find-perl-interpreter.gif

1.2. Implicit Execution

NOTE:*Every script starts with *shebang:"#!" which is not read as a comment. First line is also a place where you put your interpreter which in this case is perl.

#!/usr/bin/perl print "Perl Programming\n"; 

Make Perl Script Executable:

chmod +x perl_script.pl 

http://linuxconfig.org/images/perl-implicit_execution.gif

1.3. Explicit Execution

print "Perl Programming\n"; 

Make Perl Script Executable:

chmod +x perl_script.pl 

http://linuxconfig.org/images/perl-explicit_execution.gif

2. Simple Perl script

#!/usr/bin/perl # print "Perl Programming Tutorial\n"; 

http://linuxconfig.org/images/simple-perl-script-example.gif

3. Current path to Perl modules

List all available current paths to perl modules:

perl -e 'print "@INC" . "\n";' 

http://linuxconfig.org/images/perl-modules-path.gif

4. Variables

$ - Scalar Variable
% - Hash Variable
@ - Array
& - Subroutines

4.1. Using Perl default variable $_

#!/usr/bin/perl
$_ = "Perl Programming default variable.\n";
print; 

http://linuxconfig.org/images/perl-default-variable.gif

4.2. Defined Function

#!/usr/bin/perl

# declare perl scalar do but not define value
$perl_scalar;
#we can use conditional operator '?:' to test perl defined funtion
$variable = defined($perl_scalar) ? "Variable \$perl_scalar is Defined!"
 : "Variable \$perl_scalar is NOT Defined!";
print $variable."\n";
# declare perl scalar with value
$perl_scalar="perl";
$variable = defined($perl_scalar) ? "Variable \$perl_scalar is Defined!" 
: "Variable \$perl_scalar is NOT Defined!";
print $variable."\n"; 

http://linuxconfig.org/images/perl-defined-function.gif

4.3. Scalar variable

#!/usr/bin/perl
#Scalars hold just single data type: string, number or perl reference
#Scalars definition in Perl
$scalar_number = -5; 
$scalar_string1 = "In PERL Scalars are always referenced with \x24 in front of each variable name. ";
$scalar_string2 = "5 items";
#Undescore can be use for big numbers 
$scalar_milion = 1_000_000;
#Print scalar values
print $scalar_number."\n";
print $scalar_string1."\n";
print $scalar_string2."\n";
print $scalar_milion."\n";
#perl scalar addition
print $scalar_number + $scalar_milion."\n"; 

http://linuxconfig.org/images/perl-scalar-variable-example.gif

4.3.1. Single-Quoted Strings

#!/usr/bin/perl
 
#Single-Quoted scalar strings
$scalar_string1='perl';
print "String 1: ".$scalar_string1."\n";
$scalar_string2='#!/usr/bin/perl';
print "String 2: ".$scalar_string2."\n";
$scalar_string3='Perl
Programming
Tutorial';
print "String 3: ".$scalar_string3."\n";
$scalar_string4='Perl\n';
print "String 4: ".$scalar_string4."\n";
$scalar_string5='\'\'\\';
print "String 5: ".$scalar_string5."\n";
$scalar_string6='';
print "String 6: ".$scalar_string6."\n";
$scalar_string7='I\'m reading Perl Programming Tutorial';
print "String 7: ".$scalar_string7."\n"; 

http://linuxconfig.org/images/single-quoted-trings-perl.gif

4.3.2. Double-Quoted Strings

#!/usr/bin/perl
#Double-Quoted scalar strings
$scalar_string1="perl";
print "String 1: ".$scalar_string1."\n";
$scalar_string2="#!/usr/bin/perl";
print "String 2: ".$scalar_string2."\n";
$scalar_string3="Perl
Programming
Tutorial";
print "String 3: ".$scalar_string3."\n";
$scalar_string4="Perl\n";
print "String 4: ".$scalar_string4."\n";
$scalar_string5="\'\'\\\"";
print "String 5: ".$scalar_string5."\n";
$scalar_string6="";
print "String 6: ".$scalar_string6."\n";
# add "!" ASCII character in octal form !=041
$scalar_string7="I\'m reading Perl Programming Tutorial \041";
print "String 7: ".$scalar_string7."\n";
# add "@" ASCII character in hexadecimal form @=40
$scalar_string8="Any feedback about this \uperl \uprogramming
 \ututorial to: web\x40\lL\LINUXCONFIG.ORG\E";
print "String 8: ".$scalar_string8."\n"; 

http://linuxconfig.org/images/double-quoted-strings-perl.gif

4.3.3. String Operators

#!/usr/bin/perl

#Scalar string Operators
$scalar_string1="pe"."rl";
print "String 1: ".$scalar_string1."\n";
$scalar_string2="Perl Programming Tutorial " x (1+1);
print "String 2: ".$scalar_string2."\n";
$scalar_string3="3"."\ttabs" x 3;
print "String 3: ".$scalar_string3."\n";
$scalar_string4="Perl\x20".'Programming '."Tutorial";
print "String 4: ".$scalar_string4."\n";
$scalar_string5=9x5;
print "String 5: ".$scalar_string5."\n"; 

http://linuxconfig.org/images/perl-string-operators.gif

4.3.4. Non-Decimal Integers

#!/usr/bin/perl
 
#perl binary integer
$hash_binary_integer = 0b10000;
#perl octal integer
$hash_octal_integer = 020;
#perl hexadecimal integer
$hash_hexadecimal_integer1 = 0x10;
$hash_hexadecimal_integer2 = 0x124c_78_aa;
 
print $hash_octal_integer."\n";
print $hash_binary_integer."\n";
print $hash_hexadecimal_integer1."\n";
print $hash_hexadecimal_integer2."\n"; 

http://linuxconfig.org/images/perl-Non-Decimal-integers.gif

4.3.5. Scalar Constant Variable

#!/usr/bin/perl
 
$ordinary_scalar = 5;
$ordinary_scalar = 10; 

print $ordinary_scalar."\n";

#perl constant declaration
*SCALAR_CONSTANT = 5;
$SCALAR_CONSTANT = 10; 

http://linuxconfig.org/images/perl-scalar-constant-variable.gif

4.3.6. String And Numeric comparison Operators

Comparison String Numeric
Equal eq ==
Not Equal ne !=
Less than lt <
Greater than gt >
Less than or equal le <=
Greater than or equal ge >=
#!/usr/bin/perl
# String comparison
if ( 'Perl' eq 'perl' ) {
print "TRUE\n";
} else {
print "FALSE\n";
}

# Numeric comparison
if ( '2.4' != '2.6' ) {
print "TRUE\n";
} else {
print "FALSE\n";
} 

http://linuxconfig.org/images/string-and-numeric-comparison-operators.gif

4.4. Lists

#!/usr/bin/perl

#Lists definition in Perl
print ("Perl ","programming ","Tutorial","\n"); 

http://linuxconfig.org/images/perl-lists.gif

4.5. Arrays

4.5.1. Create and print array

#!/usr/bin/perl
 
#CREATE AN ARRAY
@perl_array1 = qw(Perl Programming Tutorial );
@perl_array2 = ("Perl ", "Programing ", "Tutorial", "\n");
@perl_array3 = (1 .. 3);
$perl_array4[0] = "Perl ";
$perl_array4[1] = "Programming ";
$perl_array4[2] = "Tutorial";
$perl_array4[50] = "\n";

#ADD ELEMENTS TO AN ARRAY
$perl_array1[3] = "\n";

#PRINT ARRAY
print @perl_array1;
print @perl_array2;
print @perl_array3;
print $perl_array1[3];
print @perl_array4;
# What index has a last element of an array
print "Last element of perl_array4 has index: " .  $#perl_array4 ."\n"; 

http://linuxconfig.org/images/create-and-print-perl-array.gif

4.5.2. Push and Pop Arrays

#!/usr/bin/perl

# CREATE AN ARRAY
@perl_array = (1 .. 3);

# PUSH NEW ELEMENT TO THE AND OF AN ARRAY
push(@perl_array, "\n");

# PRINT ARRAY
print  @perl_array;

# POP LAST ELEMENT FROM AN ARRAY
$perl_scalar = pop(@perl_array);
print  @perl_array;

# PRINT NEW LINE
print $perl_scalar; 

http://linuxconfig.org/images/push-and-pop-arrays-in-perl.gif

4.5.3. Determine The Length of an Array

#!/usr/bin/perl
 
#CREATE AN ARRAY
@perl_array = (1 .. 3);
$number_of_elements = @perl_array;
print "\@perl_array has: " . $number_of_elements  . " elements.\n";
print "\@perl_array has: " . scalar(@perl_array)  . " elements.\n"; 

http://linuxconfig.org/images/determine-the-length-of-an-array-in-perl.gif

4.5.4. Merge and Append Arrrays

#!/usr/bin/perl
 
#CREATE AN ARRAY

@perl_array1 = (".\n", "easy", "very ") ;
@perl_array2 = ("is ", "Programming ", "Perl ");
@perl_array3 = (@perl_array1, @perl_array2);
# REVERSING ELEMENTS 
print reverse @perl_array3; 

http://linuxconfig.org/images/merge-and-append-arrrays-in-perl.gif

4.5.5. Sort Arrays

#!/usr/bin/perl
 
#CREATE AN ARRAY
 
@perl_array = (3, 4, 1, 2);
@sorted_array1 = sort @perl_array;
@sorted_array2 = sort {$b <=> $a} @perl_array;
 
print "@sorted_array1 \n";
print "@sorted_array2 \n"; 

http://linuxconfig.org/images/sort-arrays-in-perl.gif

4.5.6. Delete Element from an Array

#!/usr/bin/perl
 
#CREATE AN ARRAY
@perl_array = (1, 2, 3, 4);
# CHECK IF THE ARRAY ELEMENT EXISTS
if (exists($perl_array[2])) {
	delete $perl_array[2];
} else {
	print "Array element is mising!\n"
}
print @perl_array, "\n"; 

http://linuxconfig.org/images/delete-element-from-array-in-perl.gif

4.6. Hash

4.6.1. Create Hash

#!/usr/bin/perl
 
# CREATE HASH
%perl_hash = ( 
	browser => iceweasel,
# you can also use comma instead of arrow operator
	os , linux,
);
# PRINT HASH ELEMENT
print "$perl_hash{'browser'}\n"; 

http://linuxconfig.org/images/create-hash-in-perl.gif

4.6.2. Add Element to a Hash

#!/usr/bin/perl

# CREATE HASH
%perl_hash = ( 
	browser => iceweasel,
# you can also use comma instead of arrow operator
	os , linux,
);
# PRINT HASH ELEMENT
print "$perl_hash{'browser'}\n";

# ADD ELEMENTS TO A HASH
%perl_hash = (%perl_hash, programming, perl);

# PRINT ALL ELEMENTS
print join(" ", %perl_hash). "\n"; 

http://linuxconfig.org/images/add-element-to-hash-in-perl.gif

4.6.3. Print Hash

#!/usr/bin/perl

# CREATE HASH
%perl_hash = qw( 
	ssh 22
	http 80
	https 443
	telnet 23
	postgres 5432
);
 
while (($hash_key, $hash_value) = each %perl_hash ){
	print "$hash_key uses port $hash_value\n";
} 

  • Teach Yourself Web Publishing with HTML 3.2 in 14 Days, _ Laura Lemay_, Sams.Net Publishing.
  • The Internet Unleashed, J. Ellsworth, B. Baron, et al., Sams.Net Publishing
  • The Internet Complete Reference(2nd Ed.), H. Hahn, McGraw-Hill
  • Webmaster in a Nutshell, S. Spainhour and V. Quercia, O'Reilly and Associates Inc.
  • Every Student's Guide to the World Wide Web, K. Pitter and R. Minato, McGraw Hill
  • HTML Sourcebook, I.S. Graham, Wiley and Sons
  • Definitive Guides, tutorials & How-to's, C. Musciano and B. Kennedy, Fixithere. Inc.
permalink
Weblog Navigation
Loading...
Weblog Archives
  • Loading...

Upload Files

Click "Browse" to find the file you want to upload. When you click "Upload file" your file will be uploaded and added to the list of attachments for this page.

Maximum file size: 50MB

 
 
 
File Name Author Date Uploaded Size

Save Page As

Enter a meaningful and distinctive title for your page.

Page Title:

Tip: You'll be able to find this page later by using the title you choose.

Page Already Exists

There is already a page named XXX. Would you like to:

Save with a different name:

Save the page with the name "XXX"

Append your text to the bottom of the existing page named: "XXX"

Upload Files

Click "Browse" to find the file you want to upload. When you click "Add file" this file will be added to the list of attachments for this page, and uploaded when you save the page.

 
 
 
Add Tags

Enter a tag and click "Add tag". The tag will be saved when you save the page.

Tag: 

Suggestions: