programming:perl
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
programming:perl [2023/11/01 07:31] – removed - external edit (Unknown date) 127.0.0.1 | programming:perl [2023/11/01 07:31] (current) – ↷ Page moved from camunda:programming:perl to programming:perl skipidar | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | |||
+ | ===== Perl ===== | ||
+ | |||
+ | === IDE === | ||
+ | | Notepad++ | NppExec can execute perl in Editor < | ||
+ | | EPIC is a Perl Plugin for Eclipse | [[http:// | ||
+ | |||
+ | |||
+ | ===Howtos=== | ||
+ | |||
+ | | 1. What does the header in perl files mean?| [[http:// | ||
+ | | 2. A syntax summary | ||
+ | | 3. A huge tutorial collection here | [[http:// | ||
+ | | 4. Howto compare values in perl | [[http:// | ||
+ | | 5. References in Perl | [[http:// | ||
+ | | 6. All Perl Operators | [[http:// | ||
+ | | 7. Objects in Perl | [[http:// | ||
+ | | 8. Packages and Modules in Perl | [[http:// | ||
+ | | 9. External Modules | [[http:// | ||
+ | |10. Passing Arguments to subs | {{http:// | ||
+ | |11. Perl regular expressions| {{http:// | ||
+ | |||
+ | === Documentation === | ||
+ | | All Perl Functions | [[http:// | ||
+ | |||
+ | |||
+ | === Install Modules === | ||
+ | The modules are located on [[http:// | ||
+ | |||
+ | The modules can be installed per //ppm// command. | ||
+ | <sxh perl> | ||
+ | ppm install File-Slurp | ||
+ | ppm install Mojolicious | ||
+ | ppm install DBD-mysql | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===Stolperfallen=== | ||
+ | <sxh perl> | ||
+ | #ACHTUNG: Notations | ||
+ | # my - variable definition | ||
+ | # % - hash | ||
+ | # @ - array | ||
+ | # $ - scalar, hash value | ||
+ | # () - create functions, array, hashes | ||
+ | # {} - access hashes | ||
+ | # [] - access arrays | ||
+ | # => - create hashes | ||
+ | # ->{} - dereference hash-reference and access value in hash | ||
+ | # ->[] - dereference array-reference and access value in array | ||
+ | # -> | ||
+ | # $$ref - OR dereference object-reference and execute its method $$ref:: | ||
+ | |||
+ | |||
+ | #ACHTUNG: comparison | ||
+ | # == - for numeric values AND String which start with a number | ||
+ | # eg - for Strings | ||
+ | |||
+ | |||
+ | # ACHTUNG - available magic vars | ||
+ | # @_ - an array with sub-arguments, | ||
+ | # $_[] - accesses a particular argument, like $_[0], $_[1] ... | ||
+ | |||
+ | # ACHTUNG - to access a concrete hashvalue use $$. To access the whole hashblock (e.g. in foreach) use %. | ||
+ | %hash = (' | ||
+ | $hash{sam}=" | ||
+ | |||
+ | #ACHTUNG do declare a subroutine you don't need parenthesis like myfunc() | ||
+ | sub myfunc{ | ||
+ | ... | ||
+ | } | ||
+ | |||
+ | #ACHTUNG: use $ or -> to dereference a Hash in order to ACCESS single hashvalues. | ||
+ | print " | ||
+ | print " | ||
+ | |||
+ | |||
+ | #ACHTUNG when accessing a Variable inside of a Namespace - prepend the $ to whole expression, not to the variable name | ||
+ | print $Foo::var; | ||
+ | |||
+ | #ACHTUNG - calling object' | ||
+ | MyObject:: | ||
+ | MyObject-> | ||
+ | |||
+ | </ | ||
+ | ==== Syntax ==== | ||
+ | Here the perl Syntax is listed as a Demo script. | ||
+ | |||
+ | |||
+ | === Introduction === | ||
+ | <sxh perl> | ||
+ | # | ||
+ | #HTML | ||
+ | |||
+ | #comment: perl need a semicolon at the end of the line. Dont forget it! | ||
+ | |||
+ | #ACHTUNG: Notations | ||
+ | # my - variable definition | ||
+ | # % - hash | ||
+ | # @ - array | ||
+ | # $ - scalar, hash value | ||
+ | # & - sub (function) | ||
+ | # () - create functions, array, hashes | ||
+ | # {} - access hashes | ||
+ | # [] - access arrays | ||
+ | # => - create hashes | ||
+ | # ->{} - dereference hash-reference and access value in hash | ||
+ | # ->[] - dereference array-reference and access value in array | ||
+ | # -> | ||
+ | # $$ref - | ||
+ | |||
+ | |||
+ | #ACHTUNG: comparison | ||
+ | # == - for numeric values AND String which start with a number | ||
+ | # eg - for Strings | ||
+ | |||
+ | |||
+ | |||
+ | # reference types are get by " | ||
+ | # If $slr_ref contains... then ref($slr_ref) returns... | ||
+ | # a scalar value undef | ||
+ | # a reference to a scalar " | ||
+ | # a reference to an array " | ||
+ | # a reference to a hash " | ||
+ | # a reference to a subroutine " | ||
+ | # a reference to a filehandle " | ||
+ | # a reference to a typeglob " | ||
+ | # a reference to a precompiled pattern " | ||
+ | # a reference to another reference " | ||
+ | |||
+ | |||
+ | # ACHTUNG - available magic vars | ||
+ | # @_ - an array with sub-arguments, | ||
+ | # $_[] - accesses a particular argument, like $_[0], $_[1] ... | ||
+ | |||
+ | |||
+ | # ACHTUNG - anonymous data stuctures | ||
+ | # {' | ||
+ | # [' | ||
+ | |||
+ | # NOTATIONS | ||
+ | # saves the first argument to $firstargs and others to #saves the first argument to $firstargs and others to @restArgs | ||
+ | # ($firstarg, @restArgs) = @_; | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | print " | ||
+ | |||
+ | #the newline wont be understood | ||
+ | $mystring = 'Hello String'; | ||
+ | print " $mystring "; | ||
+ | |||
+ | #multiline string | ||
+ | $multilined_string = << | ||
+ | This is my multilined string | ||
+ | note that I am terminating it with the word " | ||
+ | EOF | ||
+ | print $multilined_string; | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | # perl converts strings to numbers by taking the prefix | ||
+ | $n = '3 apples'; | ||
+ | $m = '2 oranges'; | ||
+ | print $n + $m; | ||
+ | |||
+ | |||
+ | #false collection | ||
+ | $false = 0; # the number zero | ||
+ | $false = 0.0; # the number zero as a float | ||
+ | $false = 0b0; # the number zero in binary | ||
+ | $false = 0x0; # the number zero in hexadecimal | ||
+ | $false = ' | ||
+ | $false = ""; | ||
+ | $false = undef; # the return value from undef | ||
+ | $false = 2-3+1; | ||
+ | |||
+ | |||
+ | #datatypes | ||
+ | $scalar = " | ||
+ | $scalar = ' | ||
+ | $scalar = ' | ||
+ | @array = (' | ||
+ | @array = qw(Billy Joe Jim-Bob); #qw inserts quotes and commas | ||
+ | %hash = (' | ||
+ | %hash = (joe => ' | ||
+ | $hash{joe}=" | ||
+ | $hash{sam}=" | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | #Filehandle is written in capital case | ||
+ | open(MYFILE,"> | ||
+ | die(" | ||
+ | close(MYFILE); | ||
+ | |||
+ | |||
+ | #boolean structure | ||
+ | $real_result = 0 ? 0 : 1; | ||
+ | print $real_result; | ||
+ | |||
+ | |||
+ | #subroutine define and call | ||
+ | sub mysubroutine | ||
+ | { | ||
+ | print " | ||
+ | print "Not a very interesting routine\n"; | ||
+ | print "This does the same thing every time\n"; | ||
+ | } | ||
+ | & | ||
+ | |||
+ | |||
+ | #subroutine with arguments | ||
+ | sub subWithArgs | ||
+ | { | ||
+ | # (my %$arg0) = @_; | ||
+ | my $aaRef = shift; | ||
+ | # %mhash = {' | ||
+ | # @mlist = (' | ||
+ | # print keys %mhash | ||
+ | |||
+ | |||
+ | foreach (keys %{$aaRef}){ | ||
+ | print $_ .' '; | ||
+ | } | ||
+ | } | ||
+ | %mhash = {' | ||
+ | subWithArgs(\%mhash); | ||
+ | |||
+ | |||
+ | %mhash = (one => ' | ||
+ | foreach $hashkey (sort keys %mhash){ | ||
+ | print $hashkey; | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | #typoglob are pointers | ||
+ | $firstval = " | ||
+ | *pointertofirst = *firstval; | ||
+ | print $pointertofirst; | ||
+ | |||
+ | |||
+ | #control structures | ||
+ | # initialize a hash structure (name, age) | ||
+ | #VORSICHT: runde Klammern! | ||
+ | @myarray = (" | ||
+ | foreach $arrayitem (@myarray){ | ||
+ | print " | ||
+ | } | ||
+ | #printing an array | ||
+ | print @myarray; # prints: onetwothree | ||
+ | print " | ||
+ | print " | ||
+ | |||
+ | %persons = (John => 25, Anne =>32, Paul =>22, Smith => 29); | ||
+ | foreach $name (sort keys %persons) { | ||
+ | print "$name is $persons{$name} years old.\n"; | ||
+ | }continue{ | ||
+ | print " | ||
+ | } | ||
+ | |||
+ | for($i=0; $i<5; $i++){ | ||
+ | print $i; | ||
+ | last; # this is break. This ends the loop. | ||
+ | next; # this will jump to the next iteration, its like continue statement | ||
+ | redo; # this will repeat the loop | ||
+ | } | ||
+ | |||
+ | $alter=18; | ||
+ | unless($alter> | ||
+ | print " | ||
+ | } | ||
+ | |||
+ | |||
+ | # implicit loopings | ||
+ | @myarray = (1, | ||
+ | @result = grep{$_> | ||
+ | print join(" ", | ||
+ | |||
+ | |||
+ | |||
+ | #map is like walk | ||
+ | @myarray = (1, | ||
+ | @selected = map $_** 2, @myarray; | ||
+ | print " | ||
+ | |||
+ | my @myarray = (1, | ||
+ | @selected = map {$_-7; $_**3; | ||
+ | print " | ||
+ | |||
+ | |||
+ | #subroutine alias function | ||
+ | # @_ is the array with the parameters. | ||
+ | # parameters are passed by reference | ||
+ | @arg = (' | ||
+ | myfunc(' | ||
+ | sub myfunc{ | ||
+ | print " | ||
+ | print "All arguments: @_ \n"; | ||
+ | print "first argument is $_[0] \n"; | ||
+ | print " | ||
+ | print " | ||
+ | print " | ||
+ | |||
+ | # | ||
+ | #5; | ||
+ | # may explicitely return | ||
+ | return 5; | ||
+ | } | ||
+ | |||
+ | print countParams(1, | ||
+ | sub countParams{ | ||
+ | scalar @_; #counts the array length | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | #pass by reference | ||
+ | $var = 1; | ||
+ | |||
+ | print "Var is $var \n"; #prints 1 | ||
+ | settotwo($var); | ||
+ | print "Var is $var \n"; #prints 2 | ||
+ | |||
+ | sub settotwo{ | ||
+ | $_[0]=2; #parameters are passed by reference. change the parameter. | ||
+ | } | ||
+ | |||
+ | |||
+ | # to pass parameters by value - save the parameter to temporary | ||
+ | $var = 1; | ||
+ | |||
+ | print "Var is $var \n"; | ||
+ | setpars($var); | ||
+ | print "Var is $var \n"; | ||
+ | sub setpars{ | ||
+ | ($first, $second, $third)=@_; #now you can use the parameters in the list | ||
+ | $first=1; | ||
+ | $second=2; | ||
+ | $third=3; | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | #REFERENCES | ||
+ | # references are set by \ | ||
+ | # dereferenced by $$ or by -> | ||
+ | $str = " | ||
+ | $ref = \$str; | ||
+ | print " | ||
+ | |||
+ | #reference types | ||
+ | $ref = \$ref; | ||
+ | print ref $ref . " | ||
+ | if((ref $ref) eq " | ||
+ | if((ref $ref) eq " | ||
+ | if((ref $ref) eq " | ||
+ | |||
+ | @marr = (' | ||
+ | $marr_ref = \@marr; # | ||
+ | print $marr_ref-> | ||
+ | foreach $arritem (@$marr_ref){ # | ||
+ | print $arritem; | ||
+ | }continue{ | ||
+ | print " | ||
+ | } | ||
+ | |||
+ | %mhash = (one => ' | ||
+ | $mhash_ref = \%mhash; # | ||
+ | foreach $hashkey (sort keys %$mhash_ref){ | ||
+ | print " | ||
+ | print " | ||
+ | print $mhash_ref-> | ||
+ | }continue{ | ||
+ | print " | ||
+ | } | ||
+ | |||
+ | #anonymous hash-REFERENCE is created by using {} | ||
+ | my $hashref = {' | ||
+ | foreach ( keys %$hashref ){ | ||
+ | print $_ ; | ||
+ | print " : "; | ||
+ | print $hashref -> {$_}; | ||
+ | print " | ||
+ | } | ||
+ | |||
+ | #anonymous array-REFERENCE is created by using [] | ||
+ | my $arrayref = [' | ||
+ | foreach ( @$arrayref ){ | ||
+ | print $_ ; | ||
+ | print " | ||
+ | } | ||
+ | |||
+ | #replace single value by reference | ||
+ | $hashref-> | ||
+ | $arrayref-> | ||
+ | |||
+ | #replace the whole referenced structure | ||
+ | %$hashref = (' | ||
+ | @$arrayref = (' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | #NAMESPACES | ||
+ | #package < | ||
+ | # In the Namespace there can be Variables or subs | ||
+ | # Namespace is accessed by NAMESPACE:: | ||
+ | |||
+ | package Foo; | ||
+ | sub mySub{ | ||
+ | print "mySub says blang \n"; | ||
+ | } | ||
+ | $var = 47; | ||
+ | package main; | ||
+ | |||
+ | Foo:: | ||
+ | print $Foo:: | ||
+ | |||
+ | #or you can paste a function directly into the Namespace. | ||
+ | sub Foo:: | ||
+ | print "this is a new sub\n"; | ||
+ | } | ||
+ | Foo:: | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | #OBJECTS | ||
+ | # Objects are simulated by Namespaces | ||
+ | sub MyObject:: | ||
+ | print "\n List of passed to this method: \n"; | ||
+ | foreach $arg (@_){ | ||
+ | print "$arg has type ". ref(\$arg). " | ||
+ | } | ||
+ | } | ||
+ | #ACHTUNG - calling object' | ||
+ | MyObject:: | ||
+ | MyObject-> | ||
+ | |||
+ | my $mPoint1 = Point-> | ||
+ | my $mPoint2 = Point-> | ||
+ | print $mPoint1-> | ||
+ | |||
+ | sub Point::new { | ||
+ | my ($class, $x, $y) = @_; # class (here Point) is implicetly passed to the | ||
+ | # Bless defines a structure, which will perform as an object instance | ||
+ | # Here the Object will have the type Point, which will be an array | ||
+ | # Implicit return of a REFERENCE to the new object | ||
+ | bless [$x, $y], $class; | ||
+ | } | ||
+ | |||
+ | sub Point:: | ||
+ | my ($self, $from) = @_; # self is the reference to the instance of the current class, which is passed as 1 argument, when using -> to get arguments. | ||
+ | print ref($from); | ||
+ | my ($dx, $dy) = ($$self[0] - $$from[0], $$self[1] - $$from[1]); | ||
+ | sqrt($dx * $dx + $dy * $dy); | ||
+ | } | ||
+ | |||
+ | # | ||
+ | sub Point:: | ||
+ | # shift pops the first argument from the @_ array | ||
+ | # the first argument - is the imlicit reference to the this object-instance | ||
+ | my ($self) = shift; | ||
+ | print " | ||
+ | print " | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | # CREATING AN OBJECT WITH GETTER AND SETTER | ||
+ | # create a new Object using " | ||
+ | # constructor | ||
+ | sub ObjectStory:: | ||
+ | my($class, $title, $story, $answer) = @_; | ||
+ | # make the | ||
+ | %hash = (title=> | ||
+ | bless \%hash, $class; #ACHTUNG: bless can only take references to objects, to use them as " | ||
+ | } | ||
+ | |||
+ | # getter and setter all together | ||
+ | sub ObjectStory:: | ||
+ | my ($self, $title) = @_; | ||
+ | if(defined $title){ | ||
+ | $self-> | ||
+ | } | ||
+ | return $self-> | ||
+ | } | ||
+ | |||
+ | # ObjectStory usage. | ||
+ | my $myObject = ObjectStory-> | ||
+ | print "\n Der ObjectStory Titel ist: ". $myObject-> | ||
+ | $myObject-> | ||
+ | print "\n Der neue ObjectStory Titel ist: ". $myObject-> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | #BLOCKS | ||
+ | #Following blocks will be executed on different Stages of the script | ||
+ | # print " | ||
+ | # die " | ||
+ | # die " | ||
+ | # END { print "1st END: done running \n"; | ||
+ | # CHECK { print "1st CHECK: done compiling \n"; | ||
+ | # INIT { print "1st INIT: started running \n"; | ||
+ | # END { print "2nd END: done running \n"; | ||
+ | # BEGIN { print "1st BEGIN: still compiling \n"; | ||
+ | # INIT { print "2nd INIT: started running \n"; | ||
+ | # BEGIN { print "2nd BEGIN: still compiling \n"; | ||
+ | # CHECK { print "2nd CHECK: done compiling \n"; | ||
+ | # END { print "3rd END: done running \n"; | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | #OWN Module | ||
+ | use CGI; | ||
+ | use Cwd ' | ||
+ | use File:: | ||
+ | use File:: | ||
+ | |||
+ | #block is executed on start. This block will make the script search for required modules in the same folder. User require inside of the block to import pm files. | ||
+ | BEGIN{ | ||
+ | # editing the @INC Variable to put the current script' | ||
+ | my $scriptdir = dirname(rel2abs($0)); | ||
+ | push @INC," | ||
+ | |||
+ | # use ZvgEntry; # wont work because evaluated at compiletime | ||
+ | require MyModule; # require is evaluated at runtime. Here the the name of the *.pl file should be. | ||
+ | } | ||
+ | |||
+ | my $myModule = new MyModule(); | ||
+ | $myModule-> | ||
+ | $myModule-> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | #REGEXP | ||
+ | my $string = " | ||
+ | | ||
+ | #operator " | ||
+ | # =~ m/regex/ | ||
+ | if ($string =~ m/test/) { | ||
+ | print " | ||
+ | }else{ | ||
+ | print "no match!!! \n"; | ||
+ | } | ||
+ | |||
+ | |||
+ | #Error handling | ||
+ | |||
+ | #printing Errors is done by redirecting the output to STDERR file | ||
+ | print STDERR "My Error"; | ||
+ | |||
+ | #you can let perl do somehting on error | ||
+ | print " | ||
+ | |||
+ | #or you can do multiple things | ||
+ | print " | ||
+ | print STDERR "Error on printing xoxo"; | ||
+ | print " | ||
+ | }; | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Starting Blocks ==== | ||
+ | There are several Blocks in Perl, where some code can be inserted, which will influence the running sequence. | ||
+ | <sxh perl> | ||
+ | % cat sto-INIT-eg | ||
+ | # | ||
+ | print " | ||
+ | die " | ||
+ | die " | ||
+ | END { print "1st END: done running \n"; | ||
+ | CHECK { print "1st CHECK: done compiling \n"; | ||
+ | INIT { print "1st INIT: started running \n"; | ||
+ | END { print "2nd END: done running \n"; | ||
+ | BEGIN { print "1st BEGIN: still compiling \n"; | ||
+ | INIT { print "2nd INIT: started running \n"; | ||
+ | BEGIN { print "2nd BEGIN: still compiling \n"; | ||
+ | CHECK { print "2nd CHECK: done compiling \n"; | ||
+ | END { print "3rd END: done running \n"; | ||
+ | </ | ||
+ | |||
+ | Will be executed as: | ||
+ | <sxh perl> | ||
+ | 1st BEGIN: still compiling | ||
+ | 2nd BEGIN: still compiling | ||
+ | 2nd CHECK: done compiling | ||
+ | 1st CHECK: done compiling | ||
+ | 1st INIT: started running | ||
+ | 2nd INIT: started running | ||
+ | PRINT: main running | ||
+ | DIE: main dying | ||
+ | 3rd END: done running | ||
+ | 2nd END: done running | ||
+ | 1st END: done running | ||
+ | </ | ||
+ | |||
+ | ==== Creating Modules ==== | ||
+ | The Modules are created in ***.pm** files by using the **package** command. | ||
+ | |||
+ | To import a module you can use the following commands | ||
+ | <sxh perl> | ||
+ | use ModuleName; # evaluated at compiletime. Expecially you can't add new path sources previously to a " | ||
+ | require ModuleName; # evaluated at runtime. | ||
+ | </ | ||
+ | |||
+ | To import own module from the same directory as the current script - add teh current dir to the var @INC, inside of the **BEGIN** block. \\ | ||
+ | ACHTUNG: Only use **require** to import Modules from newly added directory, because only require is evaluated at runtime. | ||
+ | |||
+ | <sxh perl> | ||
+ | use Cwd ' | ||
+ | use File:: | ||
+ | |||
+ | BEGIN{ | ||
+ | # editing the @INC Variable to put the current script' | ||
+ | my $fullscriptpath = abs_path($0); | ||
+ | my($filename, | ||
+ | |||
+ | push @INC," | ||
+ | |||
+ | # use ModuleFromScriptDir; | ||
+ | require ModuleFromScriptDir; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Error handling ==== | ||
+ | Use function **eval** to catch **die** signals. | ||
+ | |||
+ | <sxh perl> | ||
+ | eval { | ||
+ | #code you want to catch errors from | ||
+ | die($!); | ||
+ | }; | ||
+ | #errors are in @_ | ||
+ | if ($@) { | ||
+ | print " | ||
+ | } | ||
+ | |||
+ | print "code after die will be executed"; | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Parse HTML ==== | ||
+ | [[http:// | ||
+ | [[http:// | ||
+ | |||
+ | <sxh perl> | ||
+ | |||
+ | $dom = Mojo:: | ||
+ | |||
+ | #id | ||
+ | print ($dom-> | ||
+ | |||
+ | # get all text | ||
+ | print ($dom-> | ||
+ | |||
+ | # get text at the first occurence of <p> | ||
+ | print ($dom-> | ||
+ | |||
+ | #find all tags | ||
+ | my @trobjects = $dom-> | ||
+ | for my $trobject (@trobjects){ | ||
+ | print ($trobject-> | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Regular Expressions ==== | ||
+ | The regular Expression is explained the best by example. | ||
+ | ==Basics== | ||
+ | Regeular Expressions in Perl are written like that: | ||
+ | <sxh perl>" | ||
+ | |||
+ | < | ||
+ | // - marks the beginning and the End of the regexp | ||
+ | () - marks the part, which should be returned, if the exression is configured to return the match and not only to tell true/false when it matchen/not matches. | ||
+ | (?: content_here) - is called "not matching parenthesis" | ||
+ | =~ is used with a string. Without further parameters the expression returns true, when the given string matches. | ||
+ | !~ inverse of above | ||
+ | [^o] - match everything except of " | ||
+ | </ | ||
+ | |||
+ | ==Making the expression return something == | ||
+ | The expression can be configured to return something by using the /g **modifier** which are written **AFTER** the expression. | ||
+ | |||
+ | |/g|<sxh perl>my @matches = ( $str =~ / | ||
+ | |||
+ | ==Modifiers== | ||
+ | **Modifier** are written **AFTER** the expression. | ||
+ | < | ||
+ | i Makes the match case insensitive | ||
+ | m Specifies that if the string has newline or carriage | ||
+ | return characters, the ^ and $ operators will now | ||
+ | match against a newline boundary, instead of a | ||
+ | string boundary | ||
+ | o Evaluates the expression only once | ||
+ | s Allows use of . to match a newline character | ||
+ | x Allows you to use white space in the expression for clarity | ||
+ | g Globally finds all matches | ||
+ | cg Allows the search to continue even after a global match fails | ||
+ | </ | ||
+ | |||
+ | ==Operators== | ||
+ | **Operators** are written **BEFORE** the expression. More about it [[http:// | ||
+ | <sxh perl> | ||
+ | #Substitute Regular Expression - s/// | ||
+ | "my dog" =~ s/dog/cat/; # my cat | ||
+ | |||
+ | #Match Regular Expression - m// | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | ==Character classes== | ||
+ | < | ||
+ | \r \n \s ... | ||
+ | </ | ||
+ | |||
+ | What means what is listed here: http:// | ||