PPT

greenbeansneedlesSoftware and s/w Development

Dec 13, 2013 (3 years and 6 months ago)

61 views

CSE 399
-
004

Unix/Linux Skills


Spring 2006

Unix/Linux Skills


Marc Corliss

Administrative



Homework 4


Will be finished graded by tomorrow (sorry for the delay)


Homework 5


Due today


Homework 6


Passed out


Final exam


Passed out on Wednesday

Unix/Linux Skills


Marc Corliss

Plan


Looked at:


Shell


Emacs


Scripting


Today, look at:


Another type of scripting: Perl

Unix/Linux Skills


Marc Corliss

Limitations of Shell Scripting


Shell scripting is nice but has its limitations


Must use shell commands to do everything


Have no numerical data types or other complex data types


No procedures


Not entirely true, there are very limited procedures


Sometimes, need a “conventional” programming
language

Unix/Linux Skills


Marc Corliss

Perl


Can write a script in perl (script.pl)


Perl has most (but not all) advanced programming language
features


Running the script is similar to running a shell script


Either make the file executable and do:

prompt$ ./script.pl


Or use the perl command (usually in /usr/bin):

prompt$ perl script.pl


Disclaimers:


Perl is kind of a messy, kitchen sink language


Python (2nd half of semester) much more elegant language


Furthermore, only going to cover Perl in one day

Unix/Linux Skills


Marc Corliss

Helloworld in Perl


As in shell scripts, need a reference to perl command


Format: “#!<path to perl>”


Without this line, can’t run using: prompt$ ./script.sh


Comments are started with “#”


Print command takes a string and prints it to the screen


Exit command exits with the return value 0





#!/usr/bin/perl

# Line below prints “Hello, World!”

print “Hello, world!
\
n”;

exit 0;

Unix/Linux Skills


Marc Corliss

Scalar Variables


Scalar variables can be used for both strings and
numbers


Unlike many other languages, these are interchangeable
(untyped)


The following is legal: $x = 5; $x = “foo”;


To assign or to use a variable, format is “$<variable
name>”


Notice that this is somewhat different from bash

Unix/Linux Skills


Marc Corliss

Arithmetic Operators


Addition: $x = $y + 2;


Subtraction: $x = 3
-

2;


Multiplication: $x = 5 * $y;


Division: $x = 5 / $y;


Note: if $y is zero, then this will result in an error


Power: $x = 9 ** 2;


$x is set to 9 squared (81)


Modulus: $x = $y % 2;


$x is set to the remainder after dividing $y by 2


Note: if you pass a string to an arithmetic operator then
treated as 0

Unix/Linux Skills


Marc Corliss

String Operators


Concatenation: $x = $y . “foo”;


If $y = “goo”, then $x will equal “goofoo”


Repetition: $x = $y x 3;


If $y = “goo”, then $x will equal to “googoogoo”


Note: if you pass a numeric variable to a string operator
then it is cast to a string (2 becomes “2”)

Unix/Linux Skills


Marc Corliss

Shortcuts


$x++; is equivalent to $x = $x + 1;


Similarly, for ‘
-



++$x is also equivalent to $x = $x + 1;


But be careful, $x++ does not behave identically to ++$x


$x++ increments after its value is used, ++$x increments before value is
used


If $x = 5, then $n = $x++ sets $n to 5


If $x = 5, then $n = ++$x sets $n to 6


$x
-
= 5; is equivalent to $x = $x
-

5;


Similarly, for ‘
-
’, ‘*’, ‘/’, ‘**’, ‘%’, ‘.’, and ‘x’

Unix/Linux Skills


Marc Corliss

Arrays of Scalars


Use ‘@’ to reference an array

@array = (“foo”, “goo”, “zoo”);

or

@x = (1, 2);


Use brackets to reference an element of an array

@array[1] = “boo”;

(@array is now equal to (“foo”, “boo”, “zoo”))


Note: first index is 0 (not 1)


Arrays can simultaneously contain both strings and
numbers

Unix/Linux Skills


Marc Corliss

Printing


Use print command to print to the screen


Can print scalars or arrays


Need to use “
\
n” to add newlines


By default print adds no newlines


Examples:


$x = 2; $y = “foo”; print “$x$y
\
n”;

Output: 2foo


@array = (“foo”, “goo”); print “@array
\
n”;

Output: foo goo


To print “$” or “@” can use backslash or single quotes

Unix/Linux Skills


Marc Corliss

If Statements


Format for if statements:

if (<test>) {


<some operations>

}

elsif (<test>) {


<some operations>

}



else {


<some operations>

}


Note: if <test> is 0 or “” then false, otherwise true

Unix/Linux Skills


Marc Corliss

Loops


While loops:

while (<test>) {


<some commands>

}


For loops:

for (<initialize variables>; <test>; <increment
variables>) {


<some commands>

}

Unix/Linux Skills


Marc Corliss

Files


Can open files in perl using open function:

open(FH, “foo.txt”);


Open sets a file handle (FH), which can be used to
access array:

@lines = <FH>;

or

for ($line=<FH>; $line; $line=<FH>) { <do something> }


By default file is opened for reading


To write or append can do:

open(FH, “>foo.txt”); # to write

open(FH, “>>foo.txt”); # to append


To read from standard input: open(FH, “
-
”);

Unix/Linux Skills


Marc Corliss

Regular Expressions


To find if a regular expression is contained in a string:

# returns 1 if $x contains philadelphia, 0 otherwise

$x ~= /philadelphia/;


To substitute using regular expressions:

# replaces all instances of philadelphia with
Philadelphia

$x ~= s/philadelphia/Philadelphia/g;


If you set variable “$_”, then don’t need “~=“:

# execute <some commands> if $_ contains
philadelphia

if (/philadelphia/) { <some comands> }



WARNING: “$_” is an implicit variable


It is set automatically by perl, must be careful when using these variables


For more information on implicit variables see the references on the last
slide

Unix/Linux Skills


Marc Corliss

Subroutines


Can define subroutines (procedures) in perl:

Format:

sub foo {


$global_var = 5;

}


To call a subroutine:

foo();

Unix/Linux Skills


Marc Corliss

Arguments and Return Values


Arguments are passed through “@_”


To access each argument individually use shift command:

sub foo {


$var = shift(@_);


if ($var == 5) { 1; } else { 0; }

}


To call with arguments:

foo(5);


Return value is result of last statement executed


The subroutine above returns 1 or 0 (depending on $var)

Unix/Linux Skills


Marc Corliss

Global vs. Local Variables


By default variables are global within perl


In the following code, $x is set to 2 globally

sub foo { $x = 2; }


Can also make local variables


In the following code, other subroutines copy of $x is
unchanged

sub foo { local($x); $x = 2; }

Unix/Linux Skills


Marc Corliss

More Information


We have only covered the basics of Perl


To find more information the following 2 sites are good:


http://www.perl.com


http://www.comp.leeds.ac.uk/Perl/start.html