More About Operators


Hello readers, hope you are having fun with perl. In the last post we learnt about the operators used in perl with Numbers and Strings. In this post we will see few more usage of operators..

The Auto Increment and Decrement:

Like most other Programming language, Perl also gives leverage of using Pre and Post Increment/Decrement of numbers.

Pre Increment: The value is incremented first, and then the expression is evaluated.
Pre Decrement: The value is decrement first, and then the expression is evaluated. 
Post Increment : The expression is evaluated first, then the value is incremented.
Post Decremented : The expression is evaluated first, then the value is decremented.

Example:

#!/usr/bin/perl
#

use strict;
use warnings;

# Pre Increment block

my $var1 = 5;
print ++$var1 + 10;
print "\n";

# Pre Decrement block

my $var2 = 5;
print --$var2 + 10;
print "\n";

# Post Increment block;

my $var3 = 5;
print $var3++ + 10;
print "\nValue of \$var3 : ",$var3;
print "\n";

# Post Decrement block;

my $var4 = 5;
print $var4-- + 10;
print "\nValue of \$var4 : ",$var4;
print "\n";


[gray@ckserver Perl Programming]$ perl operators.pl
16
14
15
Value of $var3 : 6
15
Value of $var4 : 4
[gray@ckserver Perl Programming]$


Concatenation Operators:
'.' with strings, helps in concatenation, i.e joining strings

#!/usr/bin/perl
#

use strict;

my $g = "Yo Yo ";
my $h = "Honey Singh!";
print $g.$h."\n";

[gray@ckserver Perl Programming]$ perl operators_1.pl
Yo Yo Honey Singh!
[gray@ckserver Perl Programming]$



Note For Offline helps, at the shell type:

[gray@ckserver Perl Programming]$ perldoc
Usage: perldoc [-h] [-V] [-r] [-i] [-v] [-t] [-u] [-m] [-n nroffer_program] [-l] [-T] [-d output_filename] [-o output_format] [-M FormatterModuleNameToUse] [-w formatter_option:option_value] [-L translation_code] [-F] [-X] PageName|ModuleName|ProgramName
       perldoc -f PerlFunc
       perldoc -q FAQKeywords

The -h option prints more help.  Also try "perldoc perldoc" to get
acquainted with the system.                        [Perldoc v3.14_04]
[gray@ckserver Perl Programming]$
[gray@ckserver Perl Programming]$ perldoc perldoc

Thats all for this post, have fun..

Feel Free To Leave A Comment
If Our Article has Helped You, Support Us By Making A Small Contribution, Thank You!

0 comments: