PDA

View Full Version : >>Perl Basics 1<<



G4M3R
23-03-13, 02:16 PM
Perl is a Programming Language


Written by Larry Wall in late 80's to process mail on Unix systems and since extended by a huge cast of characters. The name is said to stand for:

Pathologically Eclectic Rubbish Lister

Practical Extraction and Report Language

Perl Properties


Interpreted Language

"Object-Oriented"

Cross-platform

Forgiving

Great for text

Extensible, rich set of libraries

Popular for web pages

Extremely popular for bioinformatics

Some Simple Scripts

Here are some simple scripts to illustrate the "look" of a Perl program.
Print a Message to the Terminal

Code:


# file: message.pl print "When that Aprill with his shoures soote\n"; print "The droghte of March ath perced to the roote,\n"; print "And bathed every veyne in swich licour\n"; print "Of which vertu engendered is the flour...\n";


Output:

(~) 50% perl message.plWhen that Aprill with his shoures sooteThe droghte of March ath perced to the roote,And bathed every veyne in swich licourOf which vertu engendered is the flour...
Do Some Math

Code:


# file: math.pl print "2 + 2 =", 2+2, "\n"; print "log(1e23)= ", log(1e23), "\n"; print "2 * sin(3.1414)= ", 2 * sin(3.1414), "\n";


Output:

(~) 51% perl math.pl2 + 2 =4log(1e23)= 52.95945713886312 * sin(3.1414)= 0.000385307177203065
Run a System Command

Code:


# file: system.pl system "ls";


Output:

(~/docs/grad_course/perl) 52% perl system.plindex.html math.pl~ problem_set.html~ what_is_perl.htmlindex.html~ message.pl simple.html what_is_perl.html~math.pl problem_set.html simple.html~
Return the Time of Day

Code:


# file: time.pl $time = localtime; print "The time is now $time\n";


Output:

(~) 53% perl time.plThe time is now Thu Sep 16 17:30:02 1999Mechanics of Writing Perl Scripts

Some hints to help you get going.
Creating the Script

A Perl script is just a text file. Use any text (programmer's) editor.
By convention, Perl script files end with the extension .pl.
The Emacs text editor has a Perl mode that will auto-format your Perl scripts and highlight keywords. Perl mode will be activated automatically if you end the script name with .pl. Otherwise, you can force Emacs to enter Perl mode by placing this line somewhere near the top of the file:

# -*- mode: perl -*-
The next time you open the file, Emacs will enter Perl mode.
Running the Script

Option 1Run the perl program from the command line, giving it the name of the script file to run.

(~) 50% perl time.pl The time is now Thu Sep 16 18:09:28 1999
Option 2Put the magic comment #!/usr/bin/perl at the top of the script.

#!/usr/bin/perl# file: time.pl$time = localtime;print "The time is now $time\n";


Make the script executable with chmod +x time.pl:

(~) 51% chmod +x time.pl
Run the script as if it were a command:

(~) 52% time.pl The time is now Thu Sep 16 18:12:13 1999
Common Errors

Every script goes through a few iterations before you get it right. Here are some common errors:
Syntax Errors

Code:


#!/usr/bin/perl # file: time.pl time = localtime; print "The time is now $time\n";


Output:

(~) 53% time.plCan't modify time in scalar assignment at time.pl line 3, near "localtime;"Execution of time.pl aborted due to compilation errors.
Runtime Errors

Code:


#!/usr/bin/perl # file: math.pl $six_of_one = 6; $half_dozen = 12/2; $result = $six_of_one/($half_dozen - $six_of_one); print "The result is $result\n";


Output:

(~) 54% math.plIllegal division by zero at math.pl line 6.
Forgetting to Make the Script Executable



(~) 55% test.pltest.pl: Permission denied.
Getting the Path to Perl Wrong on the #! line

Code:


#!/usr/local/bin/pearl # file: time.pl $time = localtime; print "The time is now $time\n";




(~) 55% time.pltime.pl: Command not found.
Useful Perl Command-Line Options

You can call Perl with a few command-line options to help catch errors:
-cPerform a syntax check, but don't run.-wTurn on verbose warnings.-dTurn on the Perl debugger.Usually you will invoke these from the command-line, as in perl -cw time.pl (syntax check time.pl with verbose warnings). You can also put them in the top line: #!/usr/bin/perl -w.


CHECK OUT Perl Basics 2! (http://mmofuse.net/forums/f63/perl-basics-2-a-19914/)