S.No | Perl | C |
---|---|---|
1 | There are several development tools in Perl as compare to C | Development tools are less and are not very advanced |
2 | It executes more slowly than C in a few situations | C has speed almost equal to that of Perl |
3 | Code can be hidden in Perl | The same is not possible in the case of C |
4 | The executable can be created without depending on the additional tools | Additional tools are the prime requirement |
$ symbol
. Variable can be a number or a string.@ symbol
prefix. Arrays are indexed by numbers.@add, $add
. The scalar variables are in one table of names or namespace and it can hold single specific information at a time and array variables are in another table of names or namespace. Scalar variables can be either a number or a string$pvt = Calculation(5,5);
print("Result = $pvt\n");
sub Calculation{
my ($fstVar, $secndVar) = @_;
my $square = sub{
return($_[0] ** 2);
};
return(&$square($fstVar) + &$square($secndVar));
};
$result = $firststring . ” “.$secondstring;
#!/usr/bin/perl
$firststring = "abcd";
$secondstring = "efgh";
$combine = "$firststring $secondstring";
print "$Combine\n";
abcd efgh
@ sign
. Example of array :my @arrayvar = ("string a", "string b "string c");
pl –wd filename.pl
$
). A scalar is either a number, a string, or a reference. A reference is actually an address of a variable, which we will see in the upcoming chapters. 0
. They are preceded by an "at
" sign (@
). %
). $x = 10;
$base_str = ‘Hello’;
$x=10;
$mystr=”abc”;
Equality | Operators |
---|---|
Equal | eq |
Not Equal | ne |
Comparison | cmp |
Less than | lt |
Greater than | gt |
Less than or equal | le |
Greater than or equal | ge |
say()
function is not supported by older Perl versions. It is like Perl print()
function with only one difference that it automatically adds a new line at the end. Perl size = $#arrayName +
$#arrayName
is the maximum index of the array.perl -w scriptName.pl
#/usr/local/bin/perl -w
se strict
" command in Perl calls strict pragma. This pragma helps to catch some bugs or errors in our script and stops the program execution. $ sign
. Strings can be placed inside single or double quote.$test = 2.3456;
{
my $test = 3;
print "In block, $test = $test ";
print "In block, $:: test = $:: test ";
}
print "Outside the block, $test = $test ";
print "Outside the block, $:: test = $::test ";
Output :
In block, $test = 3
In block, $::test = 2.3456
Outside the block, $test = 2.3456
Outside the block, $::test = 2.3456
The scope of “my” variable visibility is in the block only but if we declare one variable local then we can access that from the outside of the block also. ‘my’ creates a new variable, ‘local’ temporarily amends the value of a variable.
sub uniqueentr
{
return keys %{{ map { $_ => 1 } @_ }};
}
@array = ("perl","php","perl","asp");
print join(" ", @array), "\n";
print join(" ", uniqueentr(@array)), "\n";
@myarray = (1,2,3,4,5);
@subarray = @myarray [0,1];
Print (“@subarray\n”);
@daysOfMonth = (1..31); #daysOfMonth will contain 1 to 31 elements.
@fortnight = @daysOfMonth[1..15];
open(myfile,filename) ||DIE(“File cannot be opened\n”);
@_
’. In other words, array @_
is used as an alias for subroutine arguments.print &sum(1..4),”\n”;
sub sum{
my $sum = 0;
for my $i(@_){
$sum += $i;
}
return $sum;
}
@_
that contains parameters is iterated to find the sum and then the sum is returned.my $datestring = localtime( time );
($sec,$min,$hour,$mday,$mon, $year,$wday,$yday,$isdst) = localtime(time);
#!/usr/bin/perl
my $foo = 100;
$foo = \$foo;
print "Value of foo is : ", $$foo, "\n";
Value of foo is : REF(0x9aae38)
grep BlOCK LIST
grep ( EXPR, LIST )
$!
is a built-in error reporting variable in Perl. It tells us the reason for the error and prints it. It prints what the operating system tells it.
$dbh->commit or die $dbh->errstr;
$dbh->rollback or die $dbh->errstr;