I.T. Discussion Community!
-Collapse +Expand
Perl
Search Perl Group:

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

Prestwood eMagazine

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

   ► KBPerl Knowledge Base  Print This    Code Snippet DB All Groups  

Perl Code Snippets Page

These Code Snippets are contributed by you (our online community members). They are organized by our knowledge base topics. Specifically, by the Perl sub-topics.

Contribute a Code Snippet
Expand All

21 Perl Code Snippets

Group: Perl


Topic: Beginners Corner

-Collapse +Expand 1. Perl Case Sensitivity (Yes)
 

Perl is case sensitive.

print "hello"; //This works.
Print "hello"; //This does not.
Posted By Mike Prestwood, Post #101539, KB Topic: Beginners Corner
-Collapse +Expand 2. Perl Code Blocks
 

In Perl, you create the entire HTML page within your .PL script file using print commands.

For Perl, PHP, JavaScript, Java,and C++, I prefer to put the first { at the end of the first line of the code block as in this example because I see morePeal codeformatted that way.

$x = "Yes";
 
If ($x == "Yes") {
print "Hello world";
  print "I am a Perl coder.";
}
Posted By Mike Prestwood, Post #101542, KB Topic: Beginners Corner
-Collapse +Expand 3. Perl Comparison Operators (==, !=)
 

Common comparison operators:

== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

#Does Perl evaluate the math correctly? No!
if ((.1 + .1 + .1) == .3) {
print("Correct<br>");
} else {
print("Not correct<br>");
}
Posted By Mike Prestwood, Post #101876, KB Topic: Beginners Corner
-Collapse +Expand 4. Perl Deployment Overview
 

With Perl, you simply copy your files to a web server that is capable of running Perl pages.

Posted By Mike Prestwood, Post #101950, KB Topic: Beginners Corner
-Collapse +Expand 5. Perl File Extensions (.pl, .plex, and .aspl)
 

.pl is the traditonal default extension for Perl although some developers will change the default extension in an effort to add an additional security level and .cgi is still popular as a Perl associated extension as well as .plex and .aspl.

  • .pl - Perl
  • .cgi - Common Gateway Interface
  • .plex - Perl Executable
  • .aspl - Active Server Perl
Posted By Mike Prestwood, Post #101510, KB Topic: Beginners Corner
-Collapse +Expand 6. Perl If Statement (if..elsif..else)
 

Notice Perl is different from most other languages in it's spelling of elsif (else is not spelled correctly).

$x = 8;
  
if ($x == 10) {
 print "X is 10.";
} elsif ($x < 10) {
 print "X is less than 10.";
} else {
 print "X must be greater than 10.";
}
Posted By Mike Prestwood, Post #101879, KB Topic: Beginners Corner
-Collapse +Expand 7. Perl Literals (quote)
 

Literals are quoted as in "Prestwood".  If you need to embed a quote use a slash in front of the quote as in \"

print "Hello";
print "Hello \"Mike\".";
  
#Does Perl evaluate this simple
#floating point math correctly? No! 
if ((.1 + .1 + .1) == .3) {
  print("Correct");
} else {
  print("Not correct");
}
Posted By Mike Prestwood, Post #101543, KB Topic: Beginners Corner
-Collapse +Expand 8. Perl Logical Operators
 

Perl logical operators:

&& or and and, as in this and that
|| or or or, as in this or that
! Not, as in Not This

#Given expressions a, b, c, and d:
if !((a && b) && (c || d)) {
  #Do something.
}
Posted By Mike Prestwood, Post #101887, KB Topic: Beginners Corner
-Collapse +Expand 9. Perl Overview and History
 

Language Overview: Perl is a stable, cross platform programming language.

Target Platforms: Perl is most suitable for creating websites targeting any browser (any web server with the Perl module installed).

History: PERL is an acronym for Practical Extraction and Report Language. It is used for mission critical projects in the public and private sectors. Perl is Open Source software, licensed under its Artistic License, or the GNU General Public License (GPL). Perl was created in 1987 by Larry Wall.

Posted By Mike Prestwood, Post #101723, KB Topic: Beginners Corner
-Collapse +Expand 10. Perl Report Tools Overview
 

Because website development targets a client browser (a document interfaced GUI), a common solution is to simply output an HTML formatted page with black text and a white background (not much control but it does work for some situations).

Posted By Mike Prestwood, Post #101656, KB Topic: Beginners Corner
-Collapse +Expand 11. Perl String Concatenation (.)
 

Perl String Concatenation

Perl uses a period (.) known as a dot to concatenate strings.

$fname = "Mike";
$lname = "Prestwood";

$fullname = $fname . $lname . "
";

print "My name is " . "Mike.
";
Posted By Mike Prestwood, Post #101618, KB Topic: Beginners Corner
-Collapse +Expand 12. Perl Unary Operators
 

An operation with only one operand (a single input). The following are the Perl unary operators: !, -, ~, +,�\, &, and *.

  • ! performs logical negation which is "not"
  • - performs arithmetic negation if the operand is numeric.
  • ~ performs bitwise negation, that is 1's complement.
  • + has no semantic effect whatsoever, even on strings.
  • \ creates a reference to whatsoever follows.
  • & Address of operator.
  • * Dereference address operator.
Posted By Mike Prestwood, Post #101560, KB Topic: Beginners Corner
-Collapse +Expand 13. Perl Variables ($x = 0;)
 

Perl is a loosely typed language with only three types of variables: scalars, arrays, and hashes. Use $ for a scalar variable, @ for an array, or % for a hash (an associative array). The scalar variable type is used for anytype of simple data such as strings, integers, and numbers. In Perl, you identify and use a variable with a $ even within strings

#!/usr/local/bin/perl -w
 
print("Content-type: text/html\n\n");

$fullname = 'Mike Prestwood';
$Age = 38;
$Weight = 162.4;
 

print "Your name is $fullname.
";
print "You are $Age and weigh $Weight.
";
Posted By Mike Prestwood, Post #101617, KB Topic: Beginners Corner
-Collapse +Expand 14. PHP Case Sensitivity (Yes and No)
 

PHP is case sensitive with variable names but not with commands. Although commands are case incenstive, I prefer to use all lowercase because it's easy to type and that's what I see most PHP coders doing and I see it on PHP.Net.

All of the following are equivalent:

echo "hello<br>";
ECHO "hello<br>";
Echo "hello<br>";
eCHo "hello<br>";

...but variables are case sensitive:

$fullname = "Mike Prestwood"; //These are two...
$FullName = "Wes Peterson";   //separate varialbes.
Posted By Mike Prestwood, Post #101540, KB Topic: Beginners Corner



Topic: Language Reference

-Collapse +Expand 15. Perl Associative Array (% Array Preface)
 

General Info: Associative Array

A set of unique keys linked to a set of values. Each unique key is associated with a value. Think of it as a two column table. MyArray['CA'] = 'California' MyArray['AR'] = 'Arizona'

Languages Focus: Associative Array

Associative arrays are also known as a dictionary or a hash table in other languages.

Perl Associative Array

my %weekdays = (
'Sun' => 'Sunday',
'Mon' => 'Monday',
'Tue' => 'Tuesday',
'Wed' => 'Wednesday',
'Thu' => 'Thursday',
'Fri' => 'Friday',
'Sat' => 'Saturday',
);
my $day_of_the_week = $weekdays{'Mon'};
Posted By Mike Prestwood, Post #101519, KB Topic: Language Reference
-Collapse +Expand 16. Perl Overloading
 

Perl

  • Operator - Yes
  • Method -
Posted By Mike Prestwood, Post #101465, KB Topic: Language Reference



Topic: Perl

-Collapse +Expand 17. Perl Assignment (=)
 

Perl assignment operators:

= Assignment $x = 8;
+= Addition $x += 8;
-= Substraction $x -= 8;
*= Muliplication $x *= 8;
/= Division $x /= 8;
%= Modulus $x %= 8;
**= Exponent $x **= 8;

$FullName = "Randy Spitz";
$Age = 38;
Posted By Mike Prestwood, Post #101882, KB Topic: Perl
-Collapse +Expand 18. Perl Comments (#)
 

Commenting Code
Perl uses # for single line comments and Perl does NOT have a multiple line comment.

Compiler Directives (A special comment.)

Perl also uses compiler directives embedded in comments with #! as in:

#!/usr/local/bin/perl -w
#This is a comment in Perl.
Posted By Mike Prestwood, Post #101620, KB Topic: Perl
-Collapse +Expand 19. Perl Constants (use constant)
 

In PHP, you declare constants using the define keyword:

define("CONST_NAME", "Value");

Constants in PHP are case sensitive. A common standard in PHP is to use all-uppercase letters, with underscores to separate words within the name.

 

use constant FULL_NAME => 'Mike Prestwood';
use constant AGE => 38;
  
print "Your name is " . FULL_NAME . ".<br>";
print "You are " . AGE . ".<br>";
Posted By Mike Prestwood, Post #102184, KB Topic: Perl
-Collapse +Expand 20. Perl Custom Routines (sub)
 

Perl uses subs and parameters are referenced in a special array. All arguments passed to a subroutine are stored in a special @_ array. To retrieve the arguments, you have to look inside the array and extract them.

sub sayHello {
 my ($pName) = $_[0];
 print("Hello $pName!");
}
sub add {
 my ($p1) = $_[0];
 my ($p2) = $_[1];
 return $p1 + $p2;
}
Posted By Mike Prestwood, Post #101659, KB Topic: Perl
-Collapse +Expand 21. Perl Development Tools
 

Many developers just use a text editor but you have to be careful when developing on Windows and deploying to Unix/Linix. Some Windows text editors including Notepad, and Microsoft Expression Web save text files in UTF-8 which is not compatible with Unix/Linux.

There are many Perl editors available including ActivePerl Pro Studio, and the free Perl Express. I usually use Perl Express.

Quick Start: Install Perl to IIS or Apache, install Perl Express then configure to use Perl, then install MySQL. For IIS 7, you will likely have to configure Hangler Mappings and add %s %s.

Posted By Mike Prestwood, Post #101636, KB Topic: Perl

Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech