for loops
OOP
Files
List Array
subroutines
Hashes
Strings
Regular Expressions
Programs
Command Line Perl
p029.pl p028.pl p027.pl p026.pl p025.pl p024.pl p023.pl p022.pl p021.pl p020.pl p019.pl p018.pl p017.pl p016.pl p015.pl p014.pl p013.pl p012.pl p011.pl p010.pl p009.pl p008.pl p007.pl p006.pl p005.pl p004.pl p003.pl p002.pl p001.pl
CI implementation
In
~/base/p1/misc/proj/script/
update_report_html.pl
test01.pl
test02.pl
test03.pl
ProjReport.pm
ReportHtml.pm
For good code
use warnings;
further
use strict;
use diagnostics;
command line
foreach $arg (0 .. $#ARGV)
{ printf "*%s*\n",$ARGV[$arg]; }
$self->{'processed'}=(); - new list in class variable
my $current = shift(@{$self->{'processqueue'}}); - shift deletes
from the start
pop deletes from end
unshift @names, "Frank" - adds
Frank to the start
push @names, "Chelton"
adds to end
$#list size of list-1
scalar @arraysize of list
if (defined(@{$self->{'errorunknownfiles'}})) - can not ask
the size of an array if no elements in it!
my @p3=(@p1,@p2) joining lists.
my @p4=@p3 assignment
@{p3}[2] access
splice @names,1,3; cuts from pos 1, 3 elements.
my $s1="a:b:c"; on space
my @a2 = split(/:/,$s1);
my @a3 = split /\s+/,$s1
Array of arrays
my @A =
Using references
(
["0","1","2","3"],
["4","5","6","7"]
);
print $A[1][3];
my @v2 = (1,3,5,7,'eleven');
Array of references
my $rv2 = \@v2;
$rv2->[4]=11;
print "@v2";
my @v3 = (2,4,6,8,10);
my $rv3 = \@v3;
my @m1 = ($rv2,$rv3);
For OOP
$self->{'data'}=();
build by pushing array
push(@{$self->{'data'}},\@arr);
Now iterating over it
for(my $i=0; $i <= $#{$self->{'data'}}; $i++)
foreach(@{$self->{'data'}[$i]})
if ( ! defined($x1) ) ... testing for undefined variables.
rename($old,$new);
open for writing
p011.pl
exercise: open a file for writing and print with right
justification, implements trim function
p012.pl
Reading and testing
p007.pl
< for read, > for write.
open FILE, ">filename.txt" or die $!;
my @lines = <FILE>;
close(FILE);
foreach(@lines) { ... }
if (-e $filename) { ... } file exists
symlink
p008.pl
$ perldoc strict
$ perldoc -f substr
My convention for arguments and return value
# 1: first arg
See
p013.pl
# 2: second arg
# ...
# Return a ...
sub somefunc
{
my $arg1_=shift; ...
Implemented basic OOP.
Inheritance and virtual functions
p016/main.pl
p016/Fruit.pm
p016/Apple.pm
This required each class to have its own file, which means each
program now needs its own directory as programs generally have
several classes.
i.e. <class name>.pm
p017.pl - looks as separating access from modifying variables, perhaps more trouble than its worth.
Composition
p014/main.pl
p014/GeomPoint.pm
p014/GeomCircle.pm
Accessing list from hash inside the class
foreach (@{$self->{'report'}})
{ printf "*%s",$_; };
use lib "../p1/comsci/pl" adds to
@INC, then use a package there
use MyTimestamp;
require "path/file.pm"; does a specific file
Define and access
my %wk =
(
"Jan", "01",
"Feb", "02",
...
);
my $mon2 = %wkday->{"Mar"};
exists, delete
p020.pl
hash from list
my $h1=( "Fred", 84893, "Pete", 9630 );
\%h1 pass to subroutine
$h1{"Hawaii"}=32854; add
for loop in sub
my $h2=shift;
See p018.pl
my $key;
foreach $key (sort %$h2)
{ printf "%s : %d\n",$key,$h2{$key}; }
my $s1="hat"; a string
my $len=length($s1) string length
if ($s1 eq "hat") { ... } string comparison
ne string not equal
my $s2 = $s1 . "abc"; add strings
{eq,ne,gt,lt,ge,le}
my $s2=substr($s1,1,-1); delete first and last characters
substr($s1, index($s1,'float25'),7) = 'float50';
replace a string inside $s1
$_ is the global variable for the pattern.
$_="Some test string";
if ($s1=~/pattern/)... $s1 is target string
$` start
$& what matched
$' end
.* matches everything
/pat1.*pat2/s include new lines
/^\s*$/ blank line
\d+ at least one digit
-? optional -
/true/i case insensitive
\b beginning or end match of word
/\bcat\b/ matches word cat only
/^The/ start matched
/end.$/ end of string
Match lines, see
p026.pl
my @out = grep { /\Q$line\E/} @lines;
Negative of a search with ! operator
my @a3 = grep {!/proj\/html/} @files;
if (! /proj\/html/) ...
printf "hat";
$ perl -e "{ printf \"hat\"; }
$ perl -e "{ printf \"\\\"hat\"; } for "hat
$ perl -e "{ if ( -e \"tmp12.txt\" ) { printf \"hat\"; } }"
More difficult to write but works.
To use perl to return error
codes to the os.
my $file="tmp12.txt";
if ( -e $file )
{ unlink $file; exit(0); }
exit(1);