Likes: 0
Results 1 to 1 of 1
Thread: >>Perl Basics 1<<
-
23-03-13, 03:16 PM #1
>>Perl Basics 1<<
Register to remove this adPerl 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:Perl Properties
Pathologically Eclectic Rubbish Lister
Practical Extraction and Report Language
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:
Output:# 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"; Do Some Math
(~) 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...
Code:
Output:# 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"; Run a System Command
(~) 51% perl math.pl2 + 2 =4log(1e23)= 52.95945713886312 * sin(3.1414)= 0.000385307177203065
Code:
Output:# file: system.pl system "ls"; Return the Time of Day
(~/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~
Code:
Output:# file: time.pl $time = localtime; print "The time is now $time\n";
(~) 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:The next time you open the file, Emacs will enter Perl mode.
# -*- mode: perl -*-
Running the Script
Option 1Run the perl program from the command line, giving it the name of the script file to run.Option 2Put the magic comment #!/usr/bin/perl at the top of the script.
(~) 50% perl time.pl The time is now Thu Sep 16 18:09:28 1999Make the script executable with chmod +x time.pl:#!/usr/bin/perl# file: time.pl$time = localtime;print "The time is now $time\n"; Run the script as if it were a command:
(~) 51% chmod +x time.plCommon Errors
(~) 52% time.pl The time is now Thu Sep 16 18:12:13 1999
Every script goes through a few iterations before you get it right. Here are some common errors:
Syntax Errors
Code:
Output:#!/usr/bin/perl # file: time.pl time = localtime; print "The time is now $time\n"; Runtime Errors
(~) 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.
Code:
Output:#!/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"; Forgetting to Make the Script Executable
(~) 54% math.plIllegal division by zero at math.pl line 6.
Getting the Path to Perl Wrong on the #! line
(~) 55% test.pltest.pl: Permission denied.
Code:
#!/usr/local/bin/pearl # file: time.pl $time = localtime; print "The time is now $time\n"; Useful Perl Command-Line Options
(~) 55% time.pltime.pl: Command not found.
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!
› See More: >>Perl Basics 1<<Last edited by G4M3R; 23-03-13 at 06:36 PM.
Related Threads - Scroll Down after related threads if you are only interested to view replies for above post/thread
Visitors found this page by searching for:
Nobody landed on this page from a search engine, yet!
SEO Blog