mirror of
https://github.com/Hizenberg469/Makefile-tutorial.git
synced 2026-04-19 13:52:24 +03:00
perl is finished
This commit is contained in:
168
perl/wiki
168
perl/wiki
@@ -806,4 +806,170 @@
|
||||
Value of 1 2 3 is : 123
|
||||
Type of reference is : HASH
|
||||
Value of %var is : key220key110
|
||||
Value of %var is : key110
|
||||
Value of %var is : key110
|
||||
|
||||
|
||||
-> Passing Hashes to subroutine and function pointer:
|
||||
(Reference to function)
|
||||
|
||||
For ex:
|
||||
|
||||
@cardG1CPUarch = ('csc01', 'mips', 'xcc360g', 'ppc', 'cef4c', 'ppc');
|
||||
|
||||
print "==== Passing hashes to subroutines ======\n";
|
||||
#Function definition
|
||||
sub PrintHash{
|
||||
my (%hash) = @_;
|
||||
|
||||
foreach my $key ( keys %hash ){
|
||||
my $value = $hash{$key};
|
||||
print "$key : $value\n";
|
||||
}
|
||||
}
|
||||
|
||||
#Function definition
|
||||
sub PrintRefHash{
|
||||
my ($hashref) = @_;
|
||||
|
||||
foreach my $key (sort keys %$hashref){
|
||||
my $value = $hashref->{$key};
|
||||
print "$key : $value\n";
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
print "\n==========Calling using Reference of Subroutines===\n\n";
|
||||
$pHashRef = \&PrintHash;
|
||||
&$pHashRef(%cardG1CPUarch);
|
||||
print "\n=========Calling using Reference of Hash========\n\n";
|
||||
PrintRefHash(\%cardG1CPUarch);
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
======== Passing hashes to subroutines ==========
|
||||
|
||||
=====Calling using Reference of Subroutines ========
|
||||
|
||||
cef4c : ppc
|
||||
xcc360g : ppc
|
||||
csc01 : mips
|
||||
|
||||
====Calling using Reference of Hash======
|
||||
|
||||
cef4c : ppc
|
||||
xcc360g : ppc
|
||||
csc01 : mips
|
||||
|
||||
|
||||
-> File Handling in Perl:
|
||||
|
||||
For ex:
|
||||
|
||||
print "\n\n======File Handling in Perl======\n\n";
|
||||
#<FILEHANDLE> operator. In a scalar context, it returns a single
|
||||
#line from the filehandle.
|
||||
|
||||
#Open file to read
|
||||
open(DATA1, "<file1.txt") or die "Can't open data";
|
||||
#Open new file to write
|
||||
open(DATA2, ">file2.txt");
|
||||
# Copy data from one file to another.
|
||||
while(<DATA1>){
|
||||
print DATA2 $_;
|
||||
}
|
||||
|
||||
close(DATA1);
|
||||
close(DATA2);
|
||||
|
||||
#Renaming files
|
||||
rename ("/usr/test/file1.txt", "/usr/test/file1_renamed.txt");
|
||||
#Deleting existing file
|
||||
unlink("/usr/test/file2.txt");
|
||||
|
||||
-> REGEX:
|
||||
|
||||
print "\n\n=====REGEX======\n\n";
|
||||
#\(^[a-zA-Z0-9]+\)\. \([a-z]+\)
|
||||
#Ex: SomeDocument1.docx
|
||||
|
||||
#$bar = "This is foo and again foo";
|
||||
|
||||
$bar = "This is foo and again";
|
||||
if ($bar =~ /foo/){
|
||||
print "First time is matching\n";
|
||||
}
|
||||
else{
|
||||
print "First time is not matching\n";
|
||||
}
|
||||
|
||||
$bar = "foo";
|
||||
if ( $bar =~ /foo/ ){
|
||||
print "Second time is matching\n";
|
||||
}
|
||||
else{
|
||||
print "Second time is not matching\n";
|
||||
}
|
||||
|
||||
$date = '03/26/2018';
|
||||
$date =~ s#(\d+)/(\d+)/(\d+)#$3/$1/$2#;
|
||||
print "$date\n";
|
||||
|
||||
$string = "The time is: 11:01:22 on 9/12/01";
|
||||
$string =~/:\s+/g;
|
||||
($time) = ($string =~ /\G(\d+:\d+:\d+)/);
|
||||
$string =~ /.+\s+/g;
|
||||
($date) = ($string =~ m{\G(\d+/\d+/\d+)});
|
||||
print "Time: $time, Date: $date\n";
|
||||
|
||||
#implicit case of $_
|
||||
foreach ('csc01', 'cef5', 'xcc369g'){
|
||||
#print $_;
|
||||
print;
|
||||
print "\n";
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
====== REGEX ======
|
||||
|
||||
First time is matching
|
||||
Second time is matching
|
||||
2018/03/26
|
||||
Time: 11:01:22, Date: 9/12/01
|
||||
csc01
|
||||
cef5
|
||||
xcc360g
|
||||
|
||||
|
||||
-> Operators in Perl:
|
||||
|
||||
• Bit wise operators: &, | , ^ , <<, >> and ~ (one’s complement),
|
||||
• Logical operators : &&, || , ‘and’, ‘or’ (difference && || have higher precedence)
|
||||
and ‘not’ .
|
||||
• Quoting operators : q{}(single quote Ex: q{abc} = ‘abc’). qq{}(double quotes Ex
|
||||
qq{abc} = “abc”). qx{} (invert quotes, Ex: qx{abc} = `abc` ).
|
||||
• Other Operators :
|
||||
‘.’(dot) : concatenation operator, Ex: $a . $b
|
||||
‘..’(range) : range operator , Ex: (1..10)
|
||||
++/-- : similar to other programming language.
|
||||
-> : dereferencing operator
|
||||
‘x’ : repetition operator , Ex ( ‘-’ x 4 ) = ----
|
||||
|
||||
• Arithmetic Operators : Similar to other programming languages. Ex : +,-,/,*,%,
|
||||
and ** (exponent operator)
|
||||
• Numeric Equality Operators : ==, !=, >,<, <=, >= and $a <> $b will return -1,0
|
||||
or 1 depending upon left is lesser, equal to or greater than compared to right
|
||||
side value.
|
||||
• String Equality Operators : eq, ne , gt, lt, le, ge, cmp (returns -1,0,1) operators
|
||||
checks string wise .
|
||||
• Short hand operators : Similar to other programming languages. Derived from
|
||||
arithmetic operators.
|
||||
|
||||
• @ARGV : The array containing the command-line arguments intended for the
|
||||
script.
|
||||
• @INC : The array containing the list of places to look for Perl scripts to be
|
||||
evaluated by the do, require, or use constructs.
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user