#!/usr/bin/env perl# To ensure that the maximum number of compile-time checks and warnings are reported#use strict;#use warnings;# it's m.pl file $name="Jack"; # semilolumn need to beprint "print as in python, but without end of line! ${name}\n";print "names should be used in bash style ${name} \n";print 'Single quotes like in bash';print "Perl is case sensitive..."; $x=12.0; $y=0xFF;print "Powering like in a python ", $y ** 2.0;print "It is need not actually obvios howto run command line interpreter to interact\n but...you can try perl -d -e 1\n";print "WOW CONCATENAting STRINGS WITH "."POINT "."VERY NOT USUALLY\n"; @arr = (1,2,3,4,5);print "ARRAYS NAME SHOULD BEGIN WITH @\n";print "@arr\n";print "SUPPORT ROLLING INDICIES LIKE IN PYTHNON. Example of @arr[-1]\n"; $arr=@arr; # same as $arr=scalar(@arr). actually it is elements count in array. $... and @... is still defferent variablesprint "THERE ARE IN ", "@{arr}", " -- ${arr} elements\n";print (1..9,'a'..'z'); @stack=();push(@stack, 1); push(@stack, (2,3));print "\nlast element in @stack was " . pop(@stack) . "\n"; @words=qw(One Two Three Four);# slicing operationprint "@words[0,1..2]\n";# split and join operationprint join("\n", split(",", "1,2,3,!"));print "\n";# splice @vals=(0,1,2,3); @removed=splice(@vals, 1, 2); print "@vals";print "\n"; %map=(); %map=("key1" => 12,"key2" => 13,);print $map{"key11"};print keys(%map), "\n";print values(%map), "\n";print exists $map{"key11"};if (1==1) { print "Hello";} elsif (1==2) { ;} $a=12; $b=23;print "\n$a is less then $b" if $a<$b;print "\n$a is less then $b" unless $a>$b; $er=12;if (defined $er) {print "\nER IS DEFINED";}# REGEXP MATCHING
# FOR MORE INFO VISIT http://perldoc.perl.org/perlre.html#Modifiers
if ("I like coffee" =~ /I like (.)+/) {print "\nPASS REGEXP1";}if ("I like coffee" =~ /like (.)/) {print "\nPASS REGEXP2";}if ("like coffee" !~ /I like (.)/) {print "\nNOT PASS REGEXP3";}if ("my\@mail.ru" =~ /^\w+@\w+\.\w+$/) {print "\nPASS EMAIL REGEXP4";}# REGEXP REPLACING FIRST OCCUR $str="I like coffee coffee"; $str =~ s/coffee/Cola/;print "\n",$str;# REGEXP REPLACING GLOBAL $str="I like coffee coffee"; $str =~ s/coffee/Cola/g;print "\n",$str;
# REGEXP WITH NOT-DESCTRUCTIBLE MODE (http://perldoc.perl.org/perlrequick.html)
print $str =~ s/coffee/Cola/r;
# REGEXP REPLACING WITH MATCHING $str="like it I"; $str =~ s/(\w+) (\w+) (\w+)/$3 $1 $2/;print "\n",$str;#die "DIE WARNING";2warn "HELLO WARNING";print "aaaaaaaaaaaaaaaaa\n";for (my $i=0; $i<5; $i++) {print "$i ";}foreach (my $i=0; $i<5; $i++) {print "$i ";}print "\n";foreach my $i (1,2,4..6) {print "$i ";}print "\n";
for $i (@a) {print "$i", "\n";}
for $i (glob("*.*")) {print $i, "\n";}
for (1..5) {print "anonymous loop; ";} %guys=("bob" => 12, "jane" => 23);print "\nITERATE IN HASH/DICTIONARY\n";while (($name, $age) = each(%guys)) { print "$name "; }
while ( my ($k, $v) = each(%ENV)) {print "$k=>$v\n";}
print "\nITERATE IN HASH/DICTIONARY SECOND APPROACH\n";foreach my $name (sort(keys(%guys))) {print "$name ";}print "\n";foreach my $i (1,2,4..6) {print "$i "; if ($i>2){ last;}}# STDOUT IS INTERLEAVINGsystem("date");print "\nRETURN CODE IS $?\n";foreach $line (`ls -1`) {# print $line}# READ FROM FILEopen(USEDFILE, "<2.txt");print "PRINT CONTENT\n";print <USEDFILE>;print "\nPRINT ONE MORE TIME\n";print <USEDFILE>; open(USEDFILE, "<2.txt"); $first_line=<USEDFILE>; # fetch one linechomp $first_line; # remove line separatorprint $first_line . "\n";#FORCE CLOSE FILE, IF NOT TO DO IT FILE WILL BE CLOSED AFTER SCRIPT TERMINATIONclose USEDFILE;# WRITE TO FILEopen(WRITE, ">w.txt");print WRITE "HELLO!"; # NO COMMA! SPECIAL SYNTAX!close WRITE;rename("w.txt", "w_1.txt");unlink("w_1.txt"); @all_files=glob("*");print "@all_files";print "\n";split(";", "a;sd"); $a=1+1;print $_;print $$;sub f(){return "return value\n";}print f();print f;print &f;sub print_list(@){@args=@_; return "@args";}print print_list(" append", "as", "ss");#sub print_two_args($$){$p1=shift; $p2=shift; print "$p1=>" . "$p2";}sub print_two_args($$){$p1=@_[0]; $p2=@_[1]; print "$p1=>" . "$p2";}print "\n"; print_two_args("as", "for me"); @a=(1,2,3);sub print_type { $arg=ref shift;#%types=(1=>"SCALAR", 2=>"ARRAY", 3=>"HASH", 4=> "CODE", 5=>"REF", 6=>"GLOB", 7=>"LVALUE", 8=>"FORMAT", 9=>"IO",10=>"VSTRING", 11=>"RegExp");#print "\n", $types{$arg};print "\n", $arg;} $a=123; $b=ref \$a;print $b;#ref a;#print_type($a);sub f($$$$$$){return "return value\n";}