Perl Grouping Statements, Especially When Statements Are Not Expressions
Transcript of Perl Grouping Statements, Especially When Statements Are Not Expressions
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
1/23
* either the entry exist in the language, and please tell.* either the entry doesn't exist in the language, and please tell so. The entry will be marked as such
and won't appear as missing anymore.
* Category: Object Oriented, Dynamically typed, Has anonymous functions
* Various
{ ... }(1) block (grouping statements, especially when statements are not expressions)nothing needed breaking lines (useful when end-of-line and/or indentation has a special meaning)# commenting (until end of line)< > = comparisonlt gt le ge comparisonmin minstr / max maxstr(2) comparison (min / max (binary or more))a b comparison (returns 3 values (i.e. inferior, equal or superior))cmp comparison (returns 3 values (i.e. inferior, equal or superior))
=pod...=cut
(3) documentation comment== != equality / inequality (shallow)incremental garbage collection => not needed force garbage collection( ... ) grouping expressions__LINE__ __FILE__ information about the current line and fileeval runtime evaluationcase-sensitive tokens (case-sensitivity (keywords, variable identifiers...))[_a-zA-Z0-9]+ tokens (variable identifier regexp)CamelCase for modules, ALL_CAPS for constants, unclear for functions / variables tokens (what is
the standard way for scrunching together multiple words)do use a block as a return value (when statements are not expressions)= variable assignment or declaration (assignment)my / our / local / use vars variable assignment or declaration (declaration)
* Functions
sub { my ($a, $b) = @_; ... } anonymous function{ my ($a, $b) = @_; ... }(4) anonymous functionf(a,b,...) function call&$f(a,b,...) or $f->(a,b,...) function callf a, b, ... function callf function call (with no parameter)f() function call (with no parameter)&$f or $f->() function call (with no parameter)AUTOLOAD function called when a function is not defined (in dynamic languages)sub f { ... } function definitionsub f { ... @_ } function definition (variable number of arguments)
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
2/23
return(5) function return value (breaks the control flow)no syntax needed(6) function return value (function body is the result)caller runtime inspecting the caller information
* Control Flow
next / last breaking control flow (continue / break)goto breaking control flow (goto (unconditional jump))redo/ breaking control flow (redo / retry)return(5) breaking control flow (returning a value)eval {a}; if ($@) ... exception (catching)die exception (throwing)if (c) {...} if_then... if c if_thenc and ... if_thenif (c) {b1} elsif (c2) {b2} else {b3} if_then_elsec ? b1 : b2 if_then_elseunless ifnot_then (unless)do {...} until c loop (do something until condition)for loop (for "a la C" (while + initialisation))for (my $i = 10; $i >= 1; $i--) { ... } loop (for each value in a numeric range, 1 decrement)foreach my $i (1 .. 10) { ... } loop (for each value in a numeric range, 1 increment (see also the
entries about ranges))for (my $i = 1; $i sub { ... },v2 => sub { ... },
);if ($case{val}) { $case{val}->() } else { ... }
multiple selection (switch)
use Switch;switch ($val) {
case v1 { ... }case v2 { ... }else ...
})
(7) multiple selection (switch), sequence; sequence
* Object Oriented & Reflexivity
$o->SUPER::method(...) accessing parent methodfirst parameter(8) current instance
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
3/23
ref get the type/class corresponding to an object/instance/valuecanhas the method@ISA = qw(parent1 parent2) inheritanceDESTROY manually call an object's destructorobject->method(para) method invocationobject->method method invocation (with no parameter)
Storable::dclone object cloningnew class_name(...) object creationisa testing class membership
* Package, Module
package p;declare@ISA = qw(Exporter); @EXPORT = qw(name1 name2 ...); declare (selective export)use p(9) import (everything into current namespace)use p;(10) import (package (ie. load the package))require p import (package (ie. load the package))use p qw(name1 name2 ...) import (selectively):: package scope' package scope
* Strings
chr ascii to characterordcharacter to asciiDumper convert something to a string (see also string interpolation)"" . e convert something to a string (see also string interpolation)x duplicate n timessubstr extract a substringindex locate a substringrindex locate a substring (starting at the end)all strings allow multi-line strings multi-lineStorable::store serialize (marshalling)print simple print (on simple objects)printf simple print (printf-like)sprintf sprintf-likeStorable::retrieve sprintf-like. string concatenationeq ne string equality & inequalitylength string size"\n" strings (end-of-line (without writing the real CR or LF character))"... $v ..." strings (with interpolation of variables)
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
4/23
uc / lc uppercase / lowercase / capitalized string
* Booleans
undef false value0(11) false value
"" false value
"0" false value() false value! logical notnot(12) logical not|| / && logical or / and (short circuit)or / and logical or / and (short circuit)anything not false true value
* Bags and Lists
splice(@a, $i, 0, $e) adding an element at index (side-effect)
unshift adding an element at the beginning (list cons) (side-effect)push adding an element at the end (side-effect)reduce(13)f(... f(f(init, e1), e2) ..., en)first(2) find an elementfor for each element do somethingforeach for each element do somethingshift get the first element and remove it
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
5/23
pop get the last element and remove itjoin(s, l) join a list of strings in a string using a glue stringgrep keep elements (matching)a[-1] last element, list concatenation[ a, b, c ] list constructor( a, b, c ) list constructor
scalar @l list sizea[i]list/array indexingreverse reversemin minstr / max maxstr(2) smallest / biggest elementsort(14) sortmap transform a list (or bag) in another one
* Various Data Types
(a) computable tuple (these are a kind of immutable lists playing a special role in parameterpassing) (1-uple)
() computable tuple (these are a kind of immutable lists playing a special role in parameterpassing) (empty tuple)t computable tuple (these are a kind of immutable lists playing a special role in parameter
passing) (using a tuple for a function call)$h{k} dictionary (access: read/write)
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
6/23
{ a => b, c => d } dictionary (constructor){ a, b, c, d } dictionary (constructor)exists $h{k} dictionary (has the key ?)keys dictionary (list of keys)values dictionary (list of values)(%h1, %h2)(15) dictionary (merge)delete $h{k} dictionary (remove by key)
undef optional value (null value)v optional value (value)v optional value (value)a .. b range (inclusive .. inclusive)\ reference (pointer) (creation)$ @ % &(16) reference (pointer) (dereference)->[...] ->{...} ->(...)(17) reference (pointer) (dereference)( a, b, c ) tuple constructor
* Mathematics
+ / - / * / / addition / subtraction / multiplication / division& / | / bitwise operators (and / or / xor)> bitwise operators (left shift / right shift / unsigned right shift)~ bitwise operators (negation)** exponentiation (power)log10 logarithm (base 10)log logarithm (base e)% modulo (modulo of -3 / 2 is 1)- negation
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
7/23
1000, 1000.0, 1E3(18) numbers syntax (decimals)1_000, 10_00, 100_0 numbers syntax (integer thousand-seperator)
0b1, 07, 0xf numbers syntax (integers in base 2, octal and hexadecimal)1000 numbers syntax (integers)mathematical operator priorities and associativities (addition vs multiplication)mathematical operator priorities and associativities (exponentiation vs negation (is -3^2 equal
to 9 or -9))rand random (random number)
srand random (seed the pseudo random generator)sqrt / exp / abs square root / e-exponential / absolute valuesin / cos / tan trigonometry (basic)asin / acos / atan(19) trigonometry (inverse)int / / floor / ceil truncate / round / floor / ceil
Remarks
* (1) introduce scope* (2) in List::Util* (3) see also =head1, =head2, =over, etc
* (4) when callee has special "&" prototype* (5) in Lua, "return xxx" can only appear before a block end. in Matlab, only in inline('...')* (6) in Matlab, only for anonymous function* (7) Perl >= 5.8.0* (8) in Python, usually called self* (9) if names are exported using @EXPORT* (10) if names are not exported or are exported using @EXPORT_OK* (11) beware of 0.0 which is true in Pike!* (12) Smalltalk: postfix operator
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
8/23
* (13) in Perl, in List::Util* (14) in Scheme, not standard, but nearly standard* (15) right-bias* (16) prefix* (17) postfix* (18) integers are decimals* (19) Ruby >= 1.7
PixelThis document is licensed under GFDL (GNU Free Documentation License).Generated from syntax-across-languages.html.pl$Id: syntax-across-languages.html.pl 408 2008-08-29 08:32:23Z pixel $
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
9/23
robert
adhi
kusuma
putra
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
10/23
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
11/23
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
12/23
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
13/23
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
14/23
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
15/23
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
16/23
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
17/23
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
18/23
robert
adhi
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
19/23
kusuma
putra
robert
adhi
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
20/23
kusuma
putra
robert
adhi
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
21/23
kusuma
putra
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
22/23
-
8/9/2019 Perl Grouping Statements, Especially When Statements Are Not Expressions
23/23