DOC PREVIEW
FSU COP 4342 - Perl

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PerlIntroductionScalarsLists and arraysControl structuresI/OAssociative arrays/hashesRegular expressionsSubroutines and objectsDealing with filesDirectory and file manipulationUnix Tools: Perl 1Perl historyPERL stands for “Practical Extraction and Report Language”(although there is the alternative “Pathologically Eclectic RubbishLister”.)It was created by Larry Wall and became known in the 1990s.It was available both from ucbvax and via Usenet.Perl is released under the Artistic License and under the GNUGeneral Public and License.Unix Tools: Perl 1Perl’s Artistic License6. The scripts and library files supplied as input to or produced as outputfrom the programs of this Package do not automatically fall under thecopyright of this Package, but belong to whomever generated them, and maybe sold commercially, and may be aggregated with this Package. If suchscripts or library files are aggregated with this Package via the so-called“undump” or “unexec” methods of producing a binary executable image,then distribution of such an image shall neither be construed as a distributionof this Package nor shall it fall under the restrictions of Paragraphs 3 and 4,provided that you do not represent such an executable image as a StandardVersion of this Package.7. C subroutines (or comparably compiled subroutines in other languages)supplied by you and linked into this Package in order to emulate subroutinesand variables of the language defined by this Package shall not be consideredpart of this Package, but are the equivalent of input as in Paragraph 6,provided these subroutines do not change the language in any way thatwould cause it to fail the regression tests for the language.Unix Tools: Perl 1Advantages of PerlPerl 5 is a pleasant language to program in.It fills a niche between shell scripts and conventional languages.It is very appropriate for system administration scripts.It is very useful for text processing.It is a high level language with nice support for objects. A Perlprogram often will take far less space than the equivalent C orC++ program.Unix Tools: Perl 1Perl is InterpretedPerl is first “compiled” into bytecodes; those bytecodes are theninterpreted. Ruby, Python, and Java all have modes that arealong these lines, although of course there other options whichmake other tradeoffs.This is faster than shell interpretation, particularly when you getinto some sort of loop. It is still slower than standardcompilation.On machines that I have tested over the years, example timesinclude: an empty loop in bash for 1 million iterations takes 34seconds; 1 million iterations of an empty loop in Perl takes0.47-0.59 seconds; 1 million iterations of empty loop in C run in0.001 to 0.003 seconds.Unix Tools: Perl 1A Perl Program#!/usr/bin/perl -w# 2008 09 25 - rdluse strict;print ‘‘Hello, World!\n’’;exit 0;The first line indicates that we are to actually execute “/usr/bin/perl”.(The “-w” indicates “please whine”.) The second line is a comment.The third line makes it mandatory to declare variables. (Notice thatstatements are terminated with semicolons.) The 4th line does ourHello World, and 5th line terminates the program.Unix Tools: Perl 1Basic conceptsThere is no explicit “main”, but you can have subroutines.Features are taken from a large variety of languages, butespecially shells and C.It is very easy to write short programs that pack a lot of punch.Unix Tools: Perl 1Similarities to CMany operatorsMany control structuresSupports formatted i/oCan access command line argumentsSupports access to i/o streams, including stdin, stdout, and stderr.Unix Tools: Perl 1Similarities to shell programmingComment syntax of #$variablesInterpolation of variables inside of quoting.Support command line arguments.Implicit conversion between strings and numbers.Support for regular expressions.Some control structures.Many specific operators similar to shell commands and Unixcommand syntax.Unix Tools: Perl 1ScalarsScalars represent a single value:my $var1 = “some string”;my $var2 = 23;Scalars are strings, integers, or floating point numbers.There are also “magic” scalars which appear in Perl code. The mostcommon one is $_, which means the “default” variable, such as whenyou just do a print with no argument, or are looping over thecontents of a list. The “current” item would be referred to by $_.Unix Tools: Perl 1NumbersBoth integers and floating point numbers are actually stored as doubleprecision values —unless you invoke the “use integer” pragma:#!/usr/bin/perl -w# Script19.pl# 2006-09-18 - rdl. Illustrate use of "use integer"use strict;use integer;my $w = 100;my $x = 3;print "w / x = " . $w/$x . "\n";[langley@sophie 2006-Fall]$ ./Script19.plw / x = 33Unix Tools: Perl 1Floating point literalsFloating point literals are similar to those of C.All three of these literals represent the same value:12345.6789123456789e-4123.456789E2Unix Tools: Perl 1Integer decimal literalsSimilar to C:0 -99 1001Can use underscore as visual separator:2_333_444_555_666Unix Tools: Perl 1Other integeral literalsHexadecimal:0xff12 0x991bOctal:0125 07611Binary:0b101011Unix Tools: Perl 1C-like operatorsOperator Meaning= Assignment+ -*/ % Arithmetic& | << >> Bitwise operators> < >= <= Relationals returning “boolean” value&& || ! Logicals return “boolean” value+= -=*= Binary assignment++ -- Increment/Decrement? : Ternary, Scalar binary operator that takes on rhs valueAlso, see man perlopUnix Tools: Perl 1Operators not similar to C operatorsOperator Meaning**Exponetiation<=> Numeric comparisonx String repetition. String concatenationeq ne lt gt ge le String relationscmp String comparison=> Like comma but forces first left wordto be a stringAgain, see man perlopUnix Tools: Perl 1StringsStrings are a base type in Perl.Strings can be either quoted to allow interpolation (bothmetacharacters and variables), or quoted so as not to be. Doublequotes will allow this, single quotes prevent interpolation.Unix Tools: Perl 1Single quoted strings using ’Single quoted strings are not subject to most interpolation.However, there are two to be aware of: (1) Use \’ to indicate a literalsingle quote inside of a single quoted string that was defined with ’.(You can avoid this by using the q// syntax.) (2) Use \\ to insert abackslash; other \SOMECHAR are not interpolated inside of singlequoted strings.Unix Tools: Perl 1Double quoted stringsYou can specify special characters in double quoted


View Full Document

FSU COP 4342 - Perl

Download Perl
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Perl and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Perl 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?