Sie sind auf Seite 1von 6

perl

questions
use strict, use warnings?
what is shift || 5?
what is postfix notation?
join function?
file test operators? -f, -d, -s

object creation - constructor with de-reference operator?
---

the purpose of my in my $n (my is a local variable , in block scope)

' this is $n' - single quotes doesn't interpolate. no need to escape double quote. same
as q{this is $n}, to interpolate use double quotes or qq{} or qq| this is $n| , any
delimiter
. (dot) is the concatinating operator

$ scalar variable - 1 dimentional
@ array / list variable - 2 dimentional (subscript array with $ sign e.g $array[0]

if numer starts with 0, its a octal number

"", 0, "0" and undef are considered false in if condition
false expression evaluates to '' not 0
true expression evaluates to 1
eq, gt, lt, ne -- for comparing strings

use constant{
TRUE =>1,
PI=> 3.1432
}

perl has elsif (not elseif or else if)
perl has and (alongwith && high precedence)
perl has or (alongwith || high precedence)
perl also has unless (opposite of if, i.e. execute block if condition is false)
perl also has until (opposite of while, i.e. stop the loop when the condition becomes
true)
perl has next (like the 'continue' we have Java)
perl has last (like the 'break' we have Java)
foreach example (different from Java)
foreach my $s (@list){
message($s);
}
also can use join
print (join (':', @list))

. is a concat operator

no index out of bound exception in Perl for array (TEST)

70 special variables
$_ default input
$. line number
$! - system error
$@ eval() error ?
$$ process id
$0 program name i.e. perl script name
$1, $2, $3 etc. pattern results?
@_ list of arguments to subroutine (method)
@ARGV - command line (like String args[])
%ENV env variables
<*> directory list
__FILE__ Constants, file name
__LINE__ line number of script
__PACKAGE__ sub routine name
__END__ end of script

regex
pattern match opertaor =~
/pattern/ or m|pattern| (\ for escape)
subsititute
s/pattern/replacement/ - first match
s/pattern/replacement/g - global

$time = "01:32:55";
$time =~/(..):(..):(..)/;
$hour =$1; # first parenthesis
$min = $2; # and so on

^ beginning
. any character
$ last character
{3} - number of characters to match
* - 0 or more (greedy)
.+ greedy
.+? non greedy

Most people new to regular expressions will attempt to use <.+>.
They will be surprised when they test it on a string like This is a <EM>first</EM>
test.
You might expect the regex to match <EM> and when continuing after that match,
</EM>.

But it does not. The regex will match <EM>first</EM>. Obviously not what we
wanted.
The reason is that the plus is greedy.
That is, the plus causes the regex engine to repeat the preceding token as often as
possible.
Only if that causes the entire regex to fail, will the regex engine backtrack.
That is, it will go back to the plus, make it give up the last iteration, and proceed
with the remainder of the regex

?* - non greedy
+ - one or more
? - 0 or once
[] for class/type of characters
e.g.[0-9] - digits
[141] - match 1 or 4 or 1
\d digits, \w
\D non digit
[[:digit:]] - posix class
[[:punct:]] - punctuations
--
use subs qw( subname1 subname2) - list of words , declare so that subs can be
called without quotes and defined after usage

\ - is the reference operator (pointer) e.g. $ref = \$var
$$ref , value pointed by this pointer
for $arr = [1,2,3], arr is a reference var so $arr will print the address (because of
square brackets instead of parenthesis "()");
@$arr will de-reference
$arr->[2] - de-reference member of the array
for $hashref = {name=>'Ron',inst=>'guitar'}, hashref is a reference var so $hashref
will print the address (because of curly brackets instead of parenthesis "()");
%$hashref will de-reference
$hashref->{inst} - de-reference hash member
--
open (filehandle, mode,filename)
<filehandle> - to read a line
filehandle - to write to file

--
object oriented - objects are held in reference variables
$fh = IO::File->new('workingfile.txt','r') -- constructor with de-reference operator?
$fh->getline or $fh->read($buff,$buffsize)
$fh->print($line) or $fh->print($buff)
--
chop $string - removes last character at end of string
chomp $string - removes new line character at end of string
uc $string - uppercase
lc $string - lowercase
substr($str,startindex,endindex);
index($str,'s');
rindex($str,'s'); rear index
int($n) - integer part
hex($n) - converts hex to dec
oct($n) - converts oct to dec
rand() - random 0-0.9999
rand(100) - 0 -99.99999
srand(seed) - will give same number all the time

push method array to add at the end of array e.g. push(@arr,"item");
pop method to get and remove from end of array e.g. pop(@arr)
shift remove from top of array (0th element)
unshift to undo
reverse @list
sort @list
$t=time - millisecs
localtime($t) - formatted
gmtime($t)
--
switch is called given
case is called when - can use regex, undef and default

--
'use' statement to import
module should always return 1, with just 1;
package for modules
our instead of my for global var
sun new {
my $class = shift;
my $self ={};
bless ($self,$class);
return $self;
} is the constructor

---
Live lessons Notes

use line #!/usr/bin/perl as first line (shabang line) to run without perl command
to get exit code of previous program: echo $?
to exit use: die "msg";
to see all symbols use: od -c filename.txt
to get expanded error message: use diagnostics;
for exponential operator use '** 2' i.e. raise to the power of 2
braces mandatory in if/else blocks and the if condition
else if is: elsif
unless is opposite of if
single quotes dont interpolate except \\ and \'
OCTAL numbers start with 0
Research printf
push :add to end of array
pop :remove from end of array
shift: remove from front of array
unshift: add to front of array
@array[-1] is the last element
$count = @array; # returns number of elements in @array
$#array ; index of last element
. (dot) is concat operator
last : to break loop
sort @array
reverese @array # opposite of sort
reverse %hash # changes keys to values and vice versa
Research postfix notation
x operator for string repeatition
$0 - name of the program
-f file test operator to find if its a regular file
@ARGV list of arguments to the script - can be omitted for shift statements
@_ list of arguments to the subroutine - can be omitted for shift statements
$_ default variable, i.e if no variable is used in assignment e.g while ($_ = <>) can
change to while (<>) without mentioning it explicitly
chomp - remove last new line character
=~ : binding operator
text =~ /pattern/ is same as imaginary function regmatch(pattern,text)
text =~ s/pattern/replacement/ is same as imaginary function
regsubstitute(pattern,replacement,text). Think of s as substitute
for .e.g. text =~ s/dog/text/ # to replace 'dog' with 'cat' in 'text'
\ to escape characters in pattern

[aeiou] - any one character inside []
[^aeiou] - any one character except the ones in inside []

? zero or one
* zero or more
+ one or more
. any 1 char

Das könnte Ihnen auch gefallen