0% found this document useful (0 votes)
25 views56 pages

Lecture Notes 15

Uploaded by

Sayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views56 pages

Lecture Notes 15

Uploaded by

Sayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

C/C++ Programming in a UNIX Environment

CS 3377

Bhanu Kapoor, PhD


Department of Computer Science
University of Texas, Dallas, TX
[email protected]

Lecture Notes 15

1
Reading for Week 13-14
 Chapter 11 of A Practical Guide to
Linux® Commands, Editors, and Shell
Programming, Third Edition. Mark G.
Sobell.
◼ Chapter 11: The Perl Scripting Language

2
PERL Random Numbers
 Between 0 and 1
#!/bin/perl
$random_num = rand();
print $random_num "\n";

 Between 0 and 100


#!/bin/perl
$max = 100
$random_num = int(rand($max));
print $random_num "\n";

3
PERL random numbers
 Between any two numbers
#!/bin/perl
$min = 50
$max = 100
$random_num = int(rand($max -$min)) + $min;
print $random_num "\n";

4
PERL User Inputs and Chomp
 The chomp() function will remove (usually)
any newline character from the end of a
string.
#!/bin/perl
while (my $text = <STDIN>)
{ chomp($text);
print "You entered '$text'\n";
last if ($text eq '');
}
If you chomp an array, it will remove a newline
from the end of every element in the array.

5
Opening and Writing to a File
 open(my $fh, '>>', 'report.txt');
 Opens the file report.txt in the working
directory in append mode.
 $fh is the file handle for this file
 print $fh “Hello, world\n";

6
Variable Types

 Scalar
◼ $num = 14;
◼ $fullname = “John H. Smith”;
◼ Variable Names are Case Sensitive
◼ Underlines Allowed: $Program_Version =
1.0;
 Usage of scalars
◼ print ("pi is equal to: $pi\n");
◼ print "pi is still equal to: ", $pi, "\n";
◼ $c = $a + $b;
Variable Types
 A scalar variable can be "used" before it
is first assigned a value
◼ Result depends on context
◼ Either a blank string ("") or a zero (0)
◼ It can be source of subtle bugs in your code
◼ For example, if a variable name is mispelled
then what value should it get?
◼ Good idea to use –w opition

8
Variable Types

 List (one-dimensional array)


◼ @numbers = (16, 32, 48, 64);
◼ @names = (“Alice”, “Alex”, “Albert”);
◼ Index starts at 0
◼ Single elements are scalar: $names[0] =
“Alex”;
◼ Slices are ranges of elements
◼ @boys = @names[1..2];
◼ How big is my list?
◼ print “Number of people: “, @people, “ \n”;
Variable Types

 Hash (associative array)


◼ %var = { “name” => “paul”, “age” => 33 };
◼ Single elements are scalar
◼ print $var{“name”}; $var{age}++;
◼ How many elements are in my hash?
◼ @allkeys = keys(%var);
◼ $num = @allkeys;
Operators

 Arithmetic
◼ Basic operators: + - * / %
◼ $total = $subtotal * (1 + $tax / 100.0);
◼ Exponentiation: **
◼ $cube = $value ** 3;
◼ $cuberoot = $value ** (1.0/3);
◼ Bit-level Operations
◼ left-shift: << $val = $bits << 1;
◼ right-shift: >> $val = $bits >> 8;
Operators

 Assignments
◼ Includes: = += -= *= /= **= <<=
>>=
◼ $value *= 5;
◼ $longword <<= 16;
◼ Increment: ++
◼ $counter++ ++$counter
◼ Decrement: --
◼ $num_tries-- --$num_tries
Operators

 Boolean (bitwise operations)


◼ and or : & |
◼ Exclusive-or: ^
◼ Bitwise Negation: ~
◼ $picture = $backgnd & ~$mask | $image;

 Boolean Assignment
◼ &= |= ^=
◼ $picture &= $mask;

 Logical (Expressions)
◼ && And operator
◼ || Or operator
◼ ! Not operator
Operators

 expr1 && expr2


◼ expr1 is evaluated.
◼ expr2 is only evaluated if expr1 was true.

 expr1 || expr2
◼ expr1 is evaluated.
◼ expr2 is only evaluated if expr1 was false.

 Examples
◼ open (…) || die “couldn’t open file”;
◼ $debug && print “user’s name is $name\n”;
Operators

◼ Modulo: %
◼ $a = 123 % 10; ($a is 3)
◼ Multiplier: x
◼ print “ride on the ”, “choo-”x2, “train”;
(prints “ride on the choo-choo-train”)
◼ $stars = “*” x 80;
◼ Assignment: %= x=
Operators

◼ String Concatenation: . .=
◼ $name = “Uncle” . “ “. “Sam”;
◼ $cost = 34.99;
◼ $price = “Hope Diamond, now only \$”;
◼ $price .= “$cost”;
Conditionals

numeric string
◼ Equal: == eq
◼ Less/Greater Than: < > lt gt
◼ Less/Greater or equal: <= >= le ge
◼ Zero and empty-string means False else True
◼ Comparison: <=> cmp
◼ Results in a value of -1, 0, or 1
◼ Logical Not: !
◼ if (! $done) {
print “keep going”;
}
Conditionals
#!/bin/perl –w
$a = "123";
$b = "1234";
$c = "124";

if ($b > $c) {


print "$b > $c\n";
} else {
print "$b <= $c\n";
}

if ($b gt $c) {
print "$b gt $c\n";
} else {
print "$b le $c\n";
}
18
Control Structures

 “if” statement - first style


◼ if ($porridge_temp < 40) {
print “too hot.\n”;
}
elsif ($porridge_temp > 150) {
print “too cold.\n”;
}
else {
print “just right\n”;
}
Control Structures

 “if” statement - second style


◼ statement if condition;
◼ print “\$index is $index” if $DEBUG;
◼ Single statements only
◼ Simple expressions only
 “unless” is a reverse “if”
◼ statement unless condition;
◼ print “millenium is here!” unless $year < 2000;
Control Structures

 “for” loop - first style


◼ for (initial; condition; increment) { code }
◼ for ($i=0; $i<10; $i++) {
print “hello\n”;
}
 “for” loop - second style
◼ for [variable] (range) { code }
◼ for $name (@employees) {
print “$name is an employee.\n”;
}
Control Structures

 “for” loop with default loop variable


◼ for (@employees) {
print “$_ is an employee\n”;
print; # this prints “$_”
}

 foreach and for are the same.


Control Structures

 “while” loop
◼ while (condition) { code }
◼ $cars = 7;
while ($cars > 0) {
print “cars left: ”, $cars--, “\n”;
}
◼ while ($game_not_over) {…}
Control Structures

 “until” loop is opposite of “while”


◼ until (condition) { code }
◼ $cars = 7;
until ($cars <= 0) {
print “cars left: ”, $cars--, “\n”;
}
◼ while ($game_not_over) {…}
Control Structures

 Bottom-check Loops
◼ do { code } while (condition);
◼ do { code } until (condition);
◼ $value = 0;
do {
print “Enter Value: ”;
$value = <STDIN>;
} until ($value > 0);
No Switch Statement

 No Switch (Case) statement in PERL.


 Use if/else combinations instead
◼ if (cond1) { … }
elsif (cond2) { … }
elsif…
else…
 This is optimized at compile time
27
Subroutines
 User-defined functions
 Called subroutines in PERL
 “Functions” generally mean built-in
functions
sub myfunc{
print “Hello, World!\n”;
}

myfunc( );
The Basics
sub myfunc{
print “Hey, I’m in a function!\n”;
}

myfunc( );

 Because function is declared, ( ) is optional


(you can just say myfunc; )
 You can declare without defining:
◼ sub myfunc;
◼ You should define it eventually….
 Official name of subroutine is &myfunc
◼ ampersand not normally necessary to call it
Parameters
 Arguments/Inputs to subroutines
 Subroutine can be called with any
number of parameters.
 Get passed in via local @_ variable.
sub myfunc{
foreach $word (@_){
print “$word ”;
}
$foobar = 82
myfunc “hello”, “world”, $foobar;
 prints “hello world 82”
Passing current parameters
 Call a function with the current value of
@_ as the parameter list by using &.
 &myfunc;
◼ myfunc’s @_ is alias to current @_
 same as saying myfunc(@_);
Array Parameters
 If arrays or hashes passed into a
subroutine, they get ‘squashed’ into one
flat array: @_
@a = (1, 2, 3); @b=(8, 9, 10);
myfunc (@a, @b);
 inside myfunc, @_ = (1, 2, 3, 8, 9, 10);
 Maybe this is what you want.
◼ if not, need to use references…
References in Parameters

 To pass arrays (or hashes), and not squash


them:
sub myfunc{
($ref1, $ref2) = @_;
@x = @$ref1; @y = @$ref2;

}
@a = (1, 2, 3); @b = (8, 9, 10);
myfunc (\@a, \@b);
Return Values
 In Perl, subroutines return last expression
evaluated.
sub count {
$sum = $_[0] + $_[1];
}
$total = count(4, 5);
#$total = 9
 Standard practice is to use return keyword
sub myfunc{

return $retval;
}
Return Issues
 Can return values in list or scalar context.
sub to_upper{
@params = @_;
foreach (@params) {tr/a-z/A-Z/;}
return @params;
}
@uppers = to_upper ($word1, $word2);
$upper = to_upper($word1, $word2);
#$upper gets size of @params
Anonymous Functions
 Declare a function without giving it a name.
 call it by storing it’s return value in
definition
◼ $subref = sub { print “Hello\n”; };

 To call, de-reference the return value:


◼ &$subref;

 Works with parameters


◼ &$subref($param1, $param2);
Scoping
 Perl has two ways of creating local
variables
◼ local and my

 my creates a new variable lexically


scoped to inner most block
◼ block may be subroutine, loop, or bare { }
sub fctn{
my $x = shift(@_);

}
print $x; #ERROR!!!
Lexical Variables
 Variables declared with my are called
“lexical variables” or “lexicals”
 They are not visible outside block and
mask globals with same name:

$foo = 10;
{
my $foo = 3;
print $foo; #prints 3
}
print $foo; #prints 10
Where’s the scope
 Subroutines declared within a lexical’s
scope have access to that lexical
 Like implementing static variables in Perl
{
my $num = 20;
sub add_to_num { $num++ }
sub print_num { print “num = num\n”; }
}
add_to_num;
print_num;
print $num; #ERROR!
local
 local does not create new variable
 assigns temporary value to an existing (global)
variable
 has dynamic scope, rather than lexical
 functions called from within scope of local
variable get the temporary value
sub fctn { print “a = $a, b = $b\n”; };
$a = 10; $b = 20;
{
local $a = 1;
my $b = 2;
fctn();
}
#prints a = 1, b = 20
What to know about scope
 my is statically (lexically) scoped
◼ look at code. whatever block encloses my is
the scope of the variable
 local is dynamically scoped
◼ scope is enclosing block, plus subroutines
called from within that block
 Almost always want my instead of local
◼ notable exception: cannot create lexical
variables such as $_. Only ‘normal’, alpha-
numeric variables
◼ for built-in variables, localize them.
Prototypes
 Perl’s way of letting you limit how you’ll
allow your subroutine to be called.
 when defining the function, give it the
‘type’ of variable you want it to take:
 sub f1 ($$) {…}
◼ f1 must take two scalars
 sub f2($@) {…}
◼ f2 takes a scalar, followed by a list
 sub f3(\@$) {…}
◼ f3 takes an actual array, followed by a
scalar
Prototype Conversions
 sub fctn($$) { … }
 fctn(@foo, $bar)
 Perl converts @foo to scalar (ie, takes
its size), and passes that into the
function
 sub fctn2(\@$) {…}
 fctn2(@foo, $bar)
 Perl automatically creates reference to
@foo to pass as first member of @_
Prototype generalities

if prototype char is: Perl expects:


\$ actual scalar variable
\@ actual array variable
\% actual hash variable
$ scalar
@ array – ‘eats’ rest of params and
force list context
% hash – ‘eats’ rest of params and
forces hash context
* file handle
& subroutine (name or definition)
Getting around parameters
 If you want to ignore parameters, call
subroutine with & character in front
◼ sub myfunc (\$\$){ … }
◼ myfunc (@array); #ERROR!
◼ &myfunc (@array); #No error here
Simple Patterns
 Place the regex between a pair of
forward slashes ( / / ).
try:
#!/bin/perl –w

while (<STDIN>) {
if (/abc/) {
print “>> found ‘abc’ in $_\n”;
}
}

 If you type anything containing ‘abc’


the print statement is returned.
Binding Operator

 Previous example matched against $_


 Want to match against a scalar variable?
 Binding Operator “=~” matches pattern
on right against string on left.
 Usually add the m operator – clarity of
code.
 $string =~ m/pattern/
Simple Patterns
 Also access files and pattern match
using I/O.
try:
#!/bin/perl –w

open IN, “<genomes_desc.txt”;


while ($line = <IN>) {
if ($line=~m/elegans/) { #true if finds ‘elegans’
print $line;
}
}
Flexible matching

 Within regex there are many


characters with special meanings –
metacharacters
 star (*) matches any number of
instances
/ab*c/ => ‘a’ followed by zero or more ‘b’ followed by ‘c’

 plus (+) matches at least one instance


/ab+c/ => ‘a’ followed by 1 or more ‘b’ followed by ‘c’

 question mark (?) matches zero or one


instance
/ab?c/ => ‘a’ followed by 0 or 1 ‘b’ followed by ‘c’
Flexible Matching
 Match a character a specific number or
range of instances
{x} will match x number of instances.
/ab{3}c/ => abbbc

{x,y} will match between x and y instances.


/a{2,4}bc/ => aabc or aaabc or aaaabc

{x,} will match x+ instances.


/abc{3,}/ => abccc or abccccccc or
abcccccccc
Flexible Matching
 dot (.) is a wildcard character – matches
any character except new line (\n)
/a.c/ => ‘a’ followed by any character followed by ‘c’

 Combine metacharacters
/a.{4}c/ => ‘a’ followed 4 instances of any
character followed by ‘c’
so will match addddc , afgthc , ab569c,etc.

To use a * , + , ? or . in the pattern when not a metacharacter,


need to 'escape' them with a backslash.
/C\. elegans/ => C. elegans only
Grouping Patterns
 So far using metacharacters with one
character.
 Can group patterns – place within
parenthesis “()”.
 Powerful when coupled with quantifiers.
 /MLSTSTG+/ => MLSTSTGGGGGGGGG…
 /MLS(TSTG)+/ =>
MLSTSTGTSTGTSTG…TSTG
 /ML(ST){2}G/ => MLSTSTG
Alternative Matching
 Two ways which depend on nature of
pattern
 1) Use a verticle bar ‘|’ matches if
either left side or right side matches,
/(human|mouse|rat)/ => any string with
human or mouse or rat.
 2) Character class is a list of characters
within '[]'. It will match any single
character within the class.
/[wxyz1234\t]/ => any of the nine.
Shortcuts
 \d => any digit [0-9]
 \w => any “word” character [A-Za-z0-
9_]
 \s => any white space [\t\n\r\f ]
 \D => any character except a digit [^\d]
 \W => any character except a “word”
character [^\w]
 \S => any character except a white space
[^\s]
 /\s*/ => any amount of white space
Anchoring a Pattern
 /pattern/ will match anywhere in the
string
 Anchors hold the pattern to a point in the
string.
 caret “^” (shift 6) marks the beginning of
string while dollar “$” marks end of a
string.
/^elegans/ => elegans only at start of string.
/Canis$/ => Canis only at end of string.
/^\s*$/ => a blank line.
‘$’ ignores the new line character ‘\n’
Substitutions
 Match a pattern within in a string and
replace with another string.
 Uses the ‘s’ operator
s/abc/xyz/ => find abc and replace with xyz

 Only finds first instance of match.


Using ‘g’ modifer will find and replace
all.
$line = ‘abcaabbcabca’;
$line =~ s/abc/xyz/g;
print $line; xyzaabbcxyza

You might also like