#!/usr/bin/perl
$stringvar = "Hello World";
$intnum = 3;
$realnum = -3.42e-5;
print $stringvar, ".\n";
print 'it\'s cool to be able to write apostrophe', "\n";
print 'even cooler is writing backslash : \\' . "\n";
print 'first line without endline.';
print ' new line, you see!' . "\n";
print "tab \t \t is also cool. \n";
print "look you can even type double quote: \" \n";
print "\Uthis is gonna change to upper case \E . end of upper case \n";
print '3 + 5 = ', 3 + 5, "\n";
$str2 = "!";
$str3 = $stringvar . $str2;
print $str3, "\n";
print $nosuchvardefined , "\n";
print "in previous line there was no such variabled defined, so it print nothing \n";
print "three copy of : ", $str3 x 3 , "\n";
print "perl takes care of conversion between number and string \n";
print "for example: ", $stringvar . $intnum, "works \n";
print "as well as: ", "5 x 3 = ", "5" * 3, "\n";
$intnum += 5;
print "new intnum is: ", $intnum, "\n";
print "Oh! I forgot about commenting in perl \n";
$what = "dish";
print "there are five $whates on the table \n";
print "there are five ${what}es on the table \n";
print " 2 ^ 10 = ", 2**10, "\n";
print " 2 ^ 0.7 = " , 2**0.7, "\n";
if ( 5 > 3.23e-1 ) {
print "this is example of if statement \n";
}
if ( 'bad' lt 'good' ) {
print "you can also compare strings using gt ne lt le ge \n";
}
$logicalvar = (2 != 2);
if ($logicalvar) { print "this is not gonna be printed \n"}
if (whatever) {print "any non empty string is considerd as true \n";}
if (3.42) {print "any nonzero number is considered as true \n";}
if (5) {
print "example of nested if \n";
if (0) {print "this is not gonna be printed";}
}
if ( !(2 > 3) ) {print " ! means not operator \n";}
print "should we keep going (y/n)? \n";
$swkg = <STDIN>;
print "your answer was: $swkg";
print "did you noticed the newline in swkg variable?\n";
print "OK lets get rid of that new line character other wise the following is not gonna work.\n";
chomp($swkg);
print "your answer without newline is: $swkg" . "chomp worked! \n";
if ( ($swkg eq 'y') || ($swkg eq 'n') )
{
if ( $swkg eq 'y')
{
print "Ok lets keep going!\n";
}
else
{
print "exiting...\n";
exit 0;
}
}
else
{
print "You OK man? \n";
print "BTW: it was a example of \"or\" operator \n";
}
$count = 1;
while ($count <= 10) {
print "count is now $count \n";
$count += 1;
}
print "perl handles undefined variables like this: ", $undefintvar * 5, "\n";
print "and like this: ", $undefintvar . "something", "\n";
$n=1;
$facn=1;
while ($n <= 9) {
$facn *= $n;
$n += 1;
}
print "9! = ", $facn, "\n";
print "give me sth: \n";
$inp = <STDIN>;
if ( defined($inp) ){
print "useful for end of file detection \n";
}
$nl = "\n";
$r[0] = "Hello";
$r[1] = 1.24e-87;
($fa, $fb) = ("Hello", 2.3e3);
print $fa, $fb, $nl;
($fa, $fb) = ($fb, $fa);
print "swapping two variables:", ($fa, $fb), $nl;
@lff = ("apple ", "orange ", "strawberry ");
print $lff[0], $lff[1] , $lff[2], $nl;
@lff2 = qw( apple orange strawberry );
print @lff2, $nl ;
@lff3 = (@lff, "melon");
print $lff3[3], $nl;
@lofn = 1..10;
print @lofn, $nl;
$x[0] = 0;
$x[1] = 1;
$x[20] = 20;
print @x, $nl;
$udfv = $x[8];
print $udfv, $nl;
print "length of x is = ", $#x, $nl;
@nr1 = 5..9;
$nrw = pop (@nr1 );
print $nrw, $nl;
print @nr1, $nl ;
push (@nr1, 0);
print @nr1, $nl;
push (@nr1, 100..114);
print @nr1, $nl;
print shift(@nr1), $nl;
print shift(@nr1), $nl;
unshift(@nr1, "NEITB ");
print $nr1[0], $nl;
print "@nr1", $nl;
foreach $i (1..10) {
print "i = $i", $nl;
}
foreach $j (@lff2) {
print $j, $nl;
}
$fcl=1;
foreach (1..10) {
$fcl *= $_;
}
print "10 ! = $fcl \n";
@lst = (1..10);
@lst2 = reverse(@lst);
print "@lst2 \n";
@lst3=(1.23,0.74,1.2,0.5,1.2e-3);
@lst3s = sort(@lst3);
print "@lst3s \n";
print 45 + @lst3s, $nl;
$df = something;
@ps = something ;
($ws, $yt) = something ;
print $ws, $yt, $nl;
@plsj = ();
print "lff has ", @lff, " fruits in it \n";
print "lff has ", scalar (@lff), " fruits in it \n";
print "Start writing, when finished type Ctrl + D \n";
@lines = <STDIN>;
print "*****This is what you typed:***************\n";
print @lines;
print "*****End*********\n";
chomp (@lines);
print "@lines \n";