DOC PREVIEW
FSU COP 4342 - Programming in Perl

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 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 11 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 11 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 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Programming in Perl●Introduction●Scalars●Lists and Arrays●Control Structures●I/O●Hashes●Regular Expressions●Dealing with Files●Subroutines●Directory and File ManipulationHistory●Perl stands for Practical Extraction and Report Language.●Created by Larry Wall in the mid 1980s.●Released to Usenet readers and became popular.●Perl is free for use and is distributed under the GNU public license.Advantages of Perl●Fills the gap between programming in a conventional compiled language and shell programming.●Is very high-level. A typical Perl program may take 30% to 70% as much code as a C program.●Good for accomplishing quick tasks and primarily for text manipulation.Perl Is Interpreted●Your Perl program is initially compiled into bytecodes when you invoke your program and these bytecodes are then interpreted.●Similar in some ways to Java.●Faster than shell interpretation.●Still much slower than conventionally compiled programs.Sample Perl Program ●First line indicates the name of the program that executes the file. Can execute this file like a shell script. The -w means to print warnings.●Second line is a comment.●Third line is a pragma to indicate that variables should be declared and strings should be quoted.●Fourth line prints the string.●Last line exits the program. #!/usr/bin/perl -w use strict; # This line will print a hello world message. print “Hello world!\n”; exit 0;Basic Concepts●No main function, but can have subroutines.●Many features taken from C and shell commands.●Easy to write a program with a few commands to perform simple tasks. Features Similar to C●Many operators.●Many control structures.●Supports formatted I/O.●Can access command line arguments.●Supports using standard input, output, and error.Features Similar to Shell Programming●Comments: # to the end of the line●$variables●Interpolation of variables in “strings”.●Support for command line arguments.●Implicit conversions between strings and numbers.●Support for regular expressions.●Some control structures.●Many specific operators similar to shell commands or Unix utilities.Scalar Data●Scalars represent a single value.●Scalar types:–Strings–Numbers●Strings and numbers, like in the shell, are used almost interchangeably in Perl.Numbers●Perl stores all numbers as double-precision values internally.●Numeric Literals–floating-point literals–integer literals●decimal integer literals●non-decimal integer literalsFloating-Point Literals●Floating-point literals (or constants) in Perl are similar to those in C.●All of the following represent the same value.149.567149567e-31.49567E20.0149567e4Decimal Integer Literals●Similar to C.0 -54 511●Can use underscores for large values.28396838762_839_683_876Nondecimal Integer Literals●Similar to C.0177 # literals beginning with zero# are octol constants0x7f # literals beginning with 0x # are hexadecimal constants●Not found in C.0b1111111 # literals beginning with 0b # are binaryOperators Similar to C●assignment: =●arithmetic: +, -, *, /, %●bitwise: &, |, ^, ~, <<, >>●relational: <, <=, ==, !=, >=, >●logical: &&, ||, !●binary asg: +=, -=, *=, ...●Increment: ++, --●Ternary: ?:Operators Different from C●** # exponentiation●<=> # numeric comparison●=~, !~ # match operators●x # string repetition●. # string concatenation●eq,ne,lt,gt,le,ge # string relational●cmp # string comparison●\,, => # listStrings●Unlike many conventional programming languages, string is a basic type in Perl.●String Literals–single-quoted strings–double-quoted stringsSingle-Quoted Strings●Use single-quoted strings when you do not want variables to be interpolated.●Can use the '\' character to indicate that a single quote is part of the string ('...\'...') or a backslash is part of the string ('...\\...').●The '\' followed by any other character is just a regular '\'.'Hello World!''This is just a \ character.''Whalley\'s Class''\'''The \\ is used to access directories in DOS.'Double-Quoted Strings●Double-quoted strings are similar to C in that you can use the backslash to specify a special character.“This line ends with a newline.\n”“These\twords\tare\tseparated\tby\ttabs.”“The \”title\” of a book should be quoted.”“The price is \$1,000.”●Double-quoted strings can also be used to interpolate variables, as in the Bourne shell.String Operators●'.' is used for string concatenation.“One string can be concatenated ” . “with another.”'The price is $1,000.' . “\n”●'x' is used for string repetition.“double” x 2 eq “doubledouble”“ ” x 10 # means 10 blanks in a row. Implicit Conversions between Strings and Numbers●Implicit conversions are performed depending on the operator that is used. The coercions are performed without any warnings.9 x “5” # “99999”“1” + “2” # 3“45” - 1 . 7 # “447”Scalar Variables●Scalar variable names are preceded by '$'. Unlike shell variables, a '$' is always used.●General form.$[A-Za-z_][A-Za-z_0-9]*●Scalars can hold both strings and numbers.Declaring Scalar Variables●If you use the following pragma:use strict; then all variables must be declared. You can do this with the my operator.●General form. Use the first form to declare one variable. Use the second form to declare multiple variables.my <variable_name>;my (<variable_name>, ..., <variable_name>);●Variable declarations can go anywhere, but are often placed at the top of the program.Example Scalar Variable Declarationsmy $sum; # used to hold a sum of valuesmy ($i, $j, $k); # counter variablesmy $line; # contains a line of textmy $n = 0; # variable with an initial valuemy $s = “”; # another variable with an initial # valuemy $a = $b; # variables can be initialized to # have a run-time valueInterpolation of Variables in Strings●Variables are interpolated inside double-quoted strings. Say the value of $n is 7. The string “The value of \$n is: $n.\n” would be interpolated to be:“The value of $n is: 7.\n”●One can use the form: ${name} when the variable is followed by a character in a string that could be part of an identifier. Say the value of $day is “Tues”. The string“Today is ${day}day.\n” would be interpolated to be:“Today is Tuesday.\n”Assigning Scalar Values●The assignment operator is '=', which is the same operator that is


View Full Document

FSU COP 4342 - Programming in Perl

Download Programming in 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 Programming in 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 Programming in 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?