#!/usr/local/bin/perl print 'Please enter your name : '; # prints the text $inputName = <STDIN>; # accept a line of input from STDIN print "Hello World and $inputName\n"; # print the text, data in inputName variable and a newline Output: Please enter your name : name Hello World and name |
From the point where a # is encountered, the rest of the line is treated as a comment.
If you observed, we used single quotes in one print statement and double quotes in the other.
Single quoted strings in Perl don't allow you to use backslashed characters (except for \' and \\) and don't allow the variable substitution done in the last print statement.
So, the statement - print 'the string is $abcd' - would print the exact line as it is - the string is $abcd.
If we had used double quotes - print "the string is $abcd" - this would print - the string is <value of the variable abcd>.
#!/usr/local/bin/perl $hostname = `hostname`; print << "END"; The hostname is $hostname\n END print << 'END'; The hostname is $hostname\n END Output: The hostname is <hostname> The hostname is $hostname\n |
You can use back ticks to execute a unix command and return it's output.
Here, we execute the command hostname and store it in $hostname.
If you use << with print, it will print the text following the statement until it encounters the text given in the statement.
In both the print statements here, END is the string used to denote upto where the print statement should process.
Since double quotes were used in the first print statement, the value of hostname is written followed by a new line.
In the second print statement, single quotes are used and so, $hostname\n is printed as it is.
#!/usr/local/bin/perl
$one = 1;
$two = 2;
$three = 3;
$four = 4;
$abcd = "abcd";
$pqrs = "pqrs";
$xyz = "xyz";
print $abcd . " another string\n"; # concantenate the two strings and print
print $abcd x $three . "\n"; # repeat the string abcd three times
print $one + $two . "\n"; # add the data in variables one and two. The numerical value is converted to string and concantenated with \n
# I won't bother you with -, * and /
print $three % $two . "\n"; # modulo - returns the remainder of three divided by two
print $three ** $two . "\n"; # exponentiation - three raised to power two
print $one++ . "\n"; # post increment
print ++$one . "\n"; # pre increment
print $one-- . "\n"; # post decrement
print --$one . "\n"; # pre decrement
$one += $two; # $one = $one + $two
$one -= $two; # $one = $one - $two
Output:
abcd another string
abcdabcdabcd
3
1
9
1
3
3
1
|
If you want an exhaustive list of perl operators, check out man perlop.
We've seen scalar variables so far.
Lets take a look at arrays (also called lists).
#!/usr/local/bin/perl
@text_list = ("abcd","pqrs","xyz");
print $text_list[0] . " " . $text_list[1] . " " . $text_list[2] . "\n";
$last_index_text_list = $#text_list;
print $last_index_text_list . "\n";
($a, $b, $c) = @text_list;
print $a . " " . $b . " " . $c . "\n";
($a, $b) = ($b, $a); # yes, they exchange their data
print $a . " " . $b . "\n";
$some_text = "a b c d";
@abcd_list = split (/ /,$some_text);
print "$abcd_list[0], $abcd_list[1], $abcd_list[2], $abcd_list[3]\n";
$abcd_with_hyphens = join ("-",@abcd_list);
print "$abcd_with_hyphens\n";
Output:
abcd pqrs xyz
2
abcd pqrs xyz
pqrs abcd
a, b, c, d
a-b-c-d
|
#!/usr/local/bin/perl open (HANDLE, $file); # Open for input $line = <HANDLE>; # read one line close (HANDLE); open(HANDLE, "<$file"); # Open for input $lines = <HANDLE>; # read all line close (HANDLE); open (HANDLE, ">$file"); # Open for output print HANDLE "something"; #output to file close (HANDLE); open (HANDLE, ">>$file"); # Open for appending close (HANDLE); |
#!/usr/local/bin/perl
@text_list = ("abcd","pqrs","xyz");
@num_list = (1, 2, 3);
foreach $item (@text_list) {
if ( ($item eq "abcd") || ($item ne "xyz") ) { # if item equals "abcd" or item not equal to "xyz"
print $item . "\n"; # prints "abcd" and "pqrs" followed by a new line each
} elsif ($item eq "pqrs") {
print $item . "\n"; # prints "pqrs" followed by a new line
}
}
for ($i = 0; $i <= 2; $i++) { # ==, != work for numerical data
print $text_list[$i] . "\n";
}
$a = "abcd";
while ( $a ne "a" ) {
chop ($a); # removes the last character of the string
} # do { ... } while (
|
You can think of associative arrays (also called hashes) as name value pairs or arrays whose indices need not be integers.
#!/usr/local/bin/perl
%food_chain = (
"cat" => "mouse",
"lion" => "deer"
);
%another_way = (
"cat" , "mouse",
"lion", "deer"
);
$yet_another_way{"cat"} = "mouse";
$yet_another_way{"lion"} = "deer";
foreach $index (keys(%food_chain)) {
print "$index eats $food_chain{$index}\n";
}
foreach $value (values(%food_chain)) {
print "$value gets eaten\n";
}
delete $food_chain{"lion"};
Output:
cat eats mouse
lion eats deer
mouse gets eaten
deer gets eaten
|
#!/usr/local/bin/perl
$sentence = "Any sentence will do";
if ( $sentence =~ /Any[ ]+/ ) {
print "\"Any\" followed by atleast one space found in \"$sentence\"";
}
if ( $sentence !~ /any[ ]+/ ) {
print "\"any\" followed by atleast one space not found in \"$sentence\"";
}
$sentence =~ s/[ ]+/ /g; # replaces all groups of spaces with a single space;
# The g at the end makes it global - replace all matches that are found, not just the first one
print "$sentence\n";
$sentence =~ tr/el/ab/; # change all occurances of e to a and all occurences of l to b
print "$sentence\n";
Output:
"Any" followed by atleast one space found in "Any
sentence will do"
"any" followed by atleast one space not found in "Any
sentence will do"
Any sentence will do
Any santanca wibb do
|
#!/usr/local/bin/perl
sub mysub {
local ($local_var);
local ($first, $second, $third) = @_; # parameters are stored in the list named "_"
# could have used $first = $_[0]; $second = $_[1]; ...
$$second = "modified";
$third = "modified";
$local_var = "just a local variable";
return $first;
}
# Main program
$value1 = "abcd";
$value2 = "pqrs";
$result = &mysub("first", \$value1, $value2); # pass a constant, pass the variable value1 by reference, pass the contents of value2
print "$result $value1 $value2 \n";
Output:
first modified pqrs
|