Did You Know? man is the Linux’s manual pager. Each page argument given to man is normally the name of a program, utility or function. The manual page associated with each of these arguments is then found and displayed. A section, if provided, will direct man to look only in that section of the manual. The default action is to search in all of the available sections, following a pre-defined order and to show only the first page found, even if page exists in several sections.
This Perl programming tutorial is a great scripting guide to help you fully understand Perl script. Find Perl tutorials and programming examples to master your knowledge of Perl Scripting.
1. Using The Perl interpreter
1.1. Find Perl Interpreter
which perl
1.2. Implicit Execution
NOTE:Every script starts with shebang:"#!" which is not read as a comment. First line is also a place where you put your interpreter which in this case is perl.
# declare perl scalar do but not define value $perl_scalar; #we can use conditional operator '?:' to test perl defined funtion $variable = defined($perl_scalar) ? "Variable \$perl_scalar is Defined!" : "Variable \$perl_scalar is NOT Defined!"; print $variable."\n"; # declare perl scalar with value $perl_scalar="perl"; $variable = defined($perl_scalar) ? "Variable \$perl_scalar is Defined!" : "Variable \$perl_scalar is NOT Defined!"; print $variable."\n";
4.3. Scalar variable
#!/usr/bin/perl #Scalars hold just single data type: string, number or perl reference #Scalars definition in Perl $scalar_number = -5; $scalar_string1 = "In PERL Scalars are always referenced with \x24 in front of each variable name. "; $scalar_string2 = "5 items"; #Undescore can be use for big numbers $scalar_milion = 1_000_000; #Print scalar values print $scalar_number."\n"; print $scalar_string1."\n"; print $scalar_string2."\n"; print $scalar_milion."\n"; #perl scalar addition print $scalar_number + $scalar_milion."\n";
#PRINT ARRAY print @perl_array1; print @perl_array2; print @perl_array3; print $perl_array1[3]; print @perl_array4; # What index has a last element of an array print "Last element of perl_array4 has index: " . $#perl_array4 ."\n";
4.5.2. Push and Pop Arrays
#!/usr/bin/perl
# CREATE AN ARRAY @perl_array = (1 .. 3);
# PUSH NEW ELEMENT TO THE AND OF AN ARRAY push(@perl_array, "\n");
# PRINT ARRAY print @perl_array;
# POP LAST ELEMENT FROM AN ARRAY $perl_scalar = pop(@perl_array); print @perl_array;
#CREATE AN ARRAY @perl_array = (1, 2, 3, 4); # CHECK IF THE ARRAY ELEMENT EXISTS if (exists($perl_array[2])) { delete $perl_array[2]; } else { print "Array element is mising!\n" } print @perl_array, "\n";
4.6. Hash
4.6.1. Create Hash
#!/usr/bin/perl
# CREATE HASH %perl_hash = ( browser => iceweasel, # you can also use comma instead of arrow operator os , linux, ); # PRINT HASH ELEMENT print "$perl_hash{'browser'}\n";
4.6.2. Add Element to a Hash
#!/usr/bin/perl
# CREATE HASH %perl_hash = ( browser => iceweasel, # you can also use comma instead of arrow operator os , linux, ); # PRINT HASH ELEMENT print "$perl_hash{'browser'}\n";
# ADD ELEMENTS TO A HASH %perl_hash = (%perl_hash, programming, perl);
# PRINT ALL ELEMENTS print join(" ", %perl_hash). "\n";
while (($hash_key, $hash_value) = each %perl_hash3 ){ print "$hash_key: $hash_value\n"; }
5. Perl Regular Expressions
5.1. Regular Expressions and Special Characters
\D
Matches non-digit character
\d
Matches digit character
\E
End case modification
\e
escape
\f
Form feed
\L
Matches lowercase until \E found
\l
Next character lower case
\n
New line
\r
Return
\S
Match a non-white space character
\s
Match a white space character
\t
Match tab
\U
Match upper case until \E found
\u
Next character uppercase
\W
Match non-word
\w
Match word
\Q
Quote pattern metacharacter until \E found
5.2. Match Characters
#!/usr/bin/perl
foreach(@ARGV) { # Regex Match lower case and upper case character "p" ( ignores alphabetic case ) if (m/p/i) {$p1++;} # Regex Match lower case character "p" only if (m/p/) {$p2++;} # Regex Match two characters "ex" and ignore alphabetic case if (m/ex/i) {$ex++;} } print "p1=$p1\np2=$p2\nex=$ex\n";
5.3. Substitution
Sample File: perl_regex.txt
# Perl Regular Expressions # # Character Substitute #
#!/usr/bin/perl
open (FILEHANDLE, $ARGV[0]) || die "Problems opening file";
@file=;
foreach(@file) { # Substitute "#" with "$" and work globally for each instance found # NOTE: all metcharacters needs to bu escaped with "\" like in # this case "$" is escaped "\$" to be read literally # Meta characters are: \ | { [ ( ) ^ $ * + ? . s/\#/\$/g; # Substitute upper case "E" with lower case "e" s/E/e/; # Substitute first match of " " with "_" s/\s/\_/; # Substitute first match of " " with "\" # Note: Is your choice which substitute form you use s/// or s||| s|\s|\\|; print; }
5.3.1. Substitution with evaluation
In case a string is to be substituted with the output of a function call - rather than static text we can use the evaluation modifier (/e) which evaluates the right hand side as code, rather than a string.
#!/usr/bin/perl
my $text_eval = my $text_noeval = "Here is some texxxt.\n"; $text_noeval =~ s/(xx+)/'(x^'.length($1).')'/; print "Text without evaluation:".$text_noeval."\n"; $text_eval =~ s/(xx+)/'(x^'.length($1).')'/e; print "Text with evaluation:".$text_eval."\n";
linuxconfig.org:~$ ./subst_eval.pl Text without evaluation:Here is some te'(x^'.length(xxx).')'t.
Text with evaluation:Here is some te(x^3)t.
5.4. Translation
#!/usr/bin/perl
$string="uSe REgular Expression claSSes TO tRanslatE FroM upPEr case tO lOwER caSe chArActErs"; # Use perl to convert string characters from upper case to lower case $string =~ tr/A-Z/a-z/; print "$string\n"; # Use perl to convert string characters from lower case to upper case $string =~ tr/a-z/A-Z/; print "$string\n";
5.5. Classes
A regular expression surrounded in square brackets is called a character class which matches any single character described by the regular expression.
#!/usr/bin/perl foreach(@ARGV) { # Substitute all characters "except ^" upper case characters and character "e" with "#" s/[^A-Ze]/\#/g; print; } print "\n";
5.6. Quantifiers
#!/usr/bin/perl
@array1 = @ARGV; @array2 = @ARGV;
print "\@array1 = "; foreach(@array1) { # Substitute at least 3 "s" characters s/s{3,}/SS/g; print; } print "\n\@array2 = "; foreach(@array2) { # Substitute one or more "s" characters s/s+/S/g; print; } print "\n";
5.7. Assertion
#!/usr/bin/perl
foreach(@ARGV) { # Substitute character "a" and the end of the string with "$" s/a$/\$/g; # Substitute character "a" and the beginning of the string with "^" s/^a/\^/g; print; } print "\n";
5.8. Multiple Match
#!/usr/bin/perl # /g match globally $text="We want to improve your Perl Regular Expressions skills."; print "Number of Substitutions made: " . ($text =~ s/e/E/); print "\n$text\n";
$text="We want to improve your Perl Regular Expressions skills."; print "Number of Substitutions made: " . ($text =~ s/e/E/g); print "\n$text\n";
5.9. Regular Expression Extention
(?=)
Matches If would match next
(?!)
Matches If would NOT match next
(?<=)
Matches If would match just before
(?)
Matches If would NOT match just before
(?#)
= Comment
#!/usr/bin/perl
$_="We want you to improve your Perl Regular Expressions skills."; # Replace space with "#" if it is followed by "to" s/\s(?=to)/#/g; print "$_\n"; # Replace space with "_" if it is NOT followed by "s" s/\s(?!s)/_/g; print "$_\n";
5.10. Grouping
#!/usr/bin/perl
$a=$ARGV[0];
if ($a =~ /(.*)@(.*)\.(.*)/) { print "$1\n$2\n$3\n"; }
6. Perl Subroutines
6.1. Create Simple Perl Subroutine
#!/usr/bin/perl # Lets create subroutine which we can use to check for presence of number in the string. # Name of the subroutine is numbers_in_string sub numbers_in_string { if ($mystring =~ /[0-9]/) { print "Supplied string contains numbers!\n"; } else { print "Supplied string does NOT contain numbers!\n"; } } # declare global scope variable which means that this variable can be accessed from anywhere in this code. $mystring="number one"; # perl subroutine calls numbers_in_string; $mystring="number 1"; # perl subroutine calls numbers_in_string; numbers_in_string($mystring);
6.2. Pass and Return Values
#!/usr/bin/perl
sub perl_addition { # all variable passed to the perl subroutines are stored in special @_ variable ($number1, $number2) = @_; # return the result return $number1 + $number2 ; } # print result by calling perl_addition() subroutine print "Number1 + Number2 = " . perl_addition(4, 2) . "\n";
7. Perl operators
7.1. Precedence of Perl operators
Operators
Associativity
Parentheses and List operators
left
->
left
++ --
n/a
**
Right
! ~ \ unary+ unary-
left
=~ !~
left
* / % x
left
+ - .
left
<< >>
left
Named unary operators and file test operators
n/a
< > <+ >+ lt gt le ge
n/a
== != <=> eq ne cmp
n/a
&
left
| ^
left
&&
left
||
left
.. ...
n/a
?:
right
=+= -+ *=
right
, =>
left
Rightward List operators
n/a
not
right
and
left
or xor
left
#!/usr/bin/perl
print 1 + 2 * 3 + 4 . "\n"; print ((1 + 2) * (3 + 4)); print "\n"; # Print as a function or operator print ((5 + 5 ) * 5); print "\n"; print (5 + 5 ) * 5; print "\n"; # Use unary + operator to tell perl # that we are not making print() function call # but rather using parentheses as a precedence print +(5 + 5 ) * 5; print "\n";
# getting user input $user_input = ; # also possible to use just (<>) print $user_input; # clear user input and remove new line character chomp($user_input); print $user_input ." ";
[[Image:perl_user_input.gif]]
9.1. Reading Command Line Arguments
#!/usr/bin/perl # reading command line arguments with perl # @ARGV is Perl build-in array which # contains all arguments passed during command line execution print join(" ", @ARGV);
# read all files passed by command line as a arguments. while (<>) { print; }
10.2. Open File for Read and Write
#!/usr/bin/perl
# Create filehandle for write called WFILEHANDLE for file perl.txt # if the file does not exists it will be created. open (WFILEHANDLE, ">perl.txt") or die ("Cannot open perl.txt .\n");
# Insert data to perl.txt print WFILEHANDLE "Perl Programming Tutorial";
#Close filehandle. close (WFILEHANDLE);
# Create filehandle for read called RFILEHANDLE for file perl.txt open (RFILEHANDLE, "
# read file and print to while () { print; } print "\n";
10.3. Determine Number of Lines in a File
#!/usr/bin/perl
open(FILEHANDLE, $ARGV[0]) or die ("Could not open a given file");
@lines=; print "Number of Lines in the file: " . scalar(@lines) . "\n"; # Perl can Print particular line from the file print "Line number 23: ". $lines[22] . "/n";
10.4. Determine Number of Characters in a File
#!/usr/bin/perl
open(FILEHANDLE, $ARGV[0]) or die ("Could not open a given file"); my $input =0; while (defined($char = getc FILEHANDLE)) { $input++; } print "Number of characters: " . $input . "\n"; close FILEHANDLE;
10.5. Seek position within a File
#!/usr/bin/perl
# seek is helpful perl function, especially with huge files, where sequential access # may be time consuming and may require lots of processing power. Seek provides quick random access. # 0 -set the new position in bytes to POSITION # 1 -set the current position plus POSITION # 2 -set the new position EOF plus POSITION (often negative) # use seek function to set position 20000 bytes
open(FILEHANDLE, $ARGV[0]) or die ("Could not open a given file !!"); seek FILEHANDLE, 20000,0; # use perl tell function to check file position. print tell FILEHANDLE; print " -> seek FILEHANDLE, 20000,0 \n"; # Add another 36 bytes seek FILEHANDLE, 36,1; print tell FILEHANDLE; print " -> seek FILEHANDLE, 36,1 \n"; # Return position to byte 10 seek FILEHANDLE, 10,0; print tell FILEHANDLE; print " -> seek FILEHANDLE, 10,0 \n"; # Set position to the end of the file (eof) seek FILEHANDLE, 0,2; print tell FILEHANDLE; print " -> seek FILEHANDLE, 0,2 \n";
12. Perl Octal, Hexadecimal, And Decimal Conversions
#!/usr/bin/perl
print "\n";
#perl bin to dec #PERL CONVERSION FROM BINARY TO DECIMAL $decimal_number = 0b10010110; print "Binary number 10010110 is " . $decimal_number . " in decimal.\n";
#perl dec to bin #PERL CONVERSION FROM DECIMAL TO BINARY $decimal_number = 23451; $binary_number = unpack("B32", pack("N", $decimal_number)); print "Decimal number " . $decimal_number . " is " . $binary_number . " in binary.\n\n";
#perl oct to dec #PERL CONVERSION FROM OCTAL TO DECIMAL $octal_number = 224; $decimal_number = oct($octal_number); print "Octal number " . $octal_number . " is " . $decimal_number . " in decimal.\n"; #perl dec to oct #PERL CONVERSION FROM DECIMAL TO OCTAL $decimal_number = 8; $octal_number = sprintf("%o",$decimal_number); print "Decimal number " . $decimal_number . " is " . $octal_number . " in octal.\n\n";
#perl hex to dec #PERL CONVERSION FROM HEXADECIMAL TO DECIMAL $hexadecimal_number = "F1"; $decimal_number = hex($hexadecimal_number); print "Hexadecimal number " . $hexadecimal_number . " is " . $decimal_number . " in decimal.\n";
#perl dec to hex #PERL CONVERSION FROM DECIMAL TO HEXADECIMAL $decimal_number= 333; $hexadecimal_number = sprintf("%x", $decimal_number); print "Decimal number " . $decimal_number . " is " . $hexadecimal_number . " in hexadecimal.\n\n";
13. Create Perl Package
Here is an example of simple perl package: perl_package.pm
# DECLARE PERL PACKAGE package perl_package;
BEGIN { # INITIALIZATION CODE }
# DEFINE PERL PACKAGE sub package_subroutine { print "Hello from Perl Package.\n"; } # TO INDICATE THAT PACKAGE LOADS OK return 1;
END { # CLEAN UP CODE }
With the following script we can call package subroutine "package_subroutine": test_package.pl
if ($perl_mysql_connect) { print "Perl have created connection to MySQL database!\n" } else { print "Perl could not create connection to MySQL database!\n" }
# connect to perl to postgresql database my $perl_postgresql = DBI->connect("DBI:Pg:dbname=$postgresql_database;host=$postgresql_host", "$postgresql_user", "$postgresql_password");
if($perl_postgresql) { print "Perl established connection to PostgreSQL database\n"; }
15. Object Oriented Perl
There are many materials focussed on object oriented Perl from the introductory PerlBoot to the more comprehensive PerlToot. In this section are topics or viewpoints which couldn't be found elsewhere.
15.1. Template of get/set methods
Below is an example of a script using an object with two pieces of data with a method to set/get each one: varName and varAge.
linuxconfig:~/learn_perl/oo$ $ cat test.pl #!/usr/bin/perl use strict; use warnings; use Person;
my $p = Person->new();
$p->varName('Anna'); $p->varAge(30);
print $p->varName." is ".$p->varAge." years old.\n";
Running this function behaves as expected:
linuxconfig:~/learn_perl/oo$ ./test.pl Anna is 30 years old.
The obvious implementation of this object would be as follows:
my $self = {}; bless($self, $class); return $self; }
sub varName { my ($self, $name) = @_; if (defined($name)) { $self->{NAME} = $name; } return $self->{NAME}; };
sub varAge { my ($self, $age) = @_; if (defined($age)) { $self->{AGE} = $age; } return $self->{AGE}; };
1;
The main thing to observe in the code above is that the varName and varAge are identical in functionality. In the example below, we implement both of these methods using a single function template.
my $self = {}; bless($self, $class); return $self; }
my @vars = qw(Name Age); foreach my $var (@vars) { no strict 'refs'; # permit the symbolic references to varName, varAge *{"var".$var} = sub { my ($self, $stuff) = @_; if (defined($stuff)) { $self->{uc($var)} = $stuff; # change Name to NAME } return $self->{uc($var)}; }; }