Perl Scripting
Perl=Practical Extraction and Report Language
not shell programming
use version 5.6
Simple Perl script test.pl
#!/usr/local/bin/perl
print “This is a test \n”
Option 1:
>chmod +x test.pl
>test.pl
Option 2:
>perl test.pl
make sure /usr/local/bin/perl is in your path
Perl Variables
Simple variables in Perl can have two types of values: integers and strings
There are also object variables (maybe see this later)
Integers: 1, 2, -10
Strings: sequences of characters, quoted either as ' ' or “ .... “
a string in between ' ' has value exactly the sequence of characters in between quotes
➢“ “ some substitutions occurs
$i=10;
$s1=' winter for the last $i months ';
$s2=” winter for the last $i months “;
print $i;
print $s1;
print $s2;
Result:
10
winter for the last $i months
winter for the last 10 months
$s3=”winter for the last \n $i months”
winter for the last \n stands for “new line”
10 months
Important to notice:
Unlike shell scripting, you use $var on the left side of an assignment $i=10
Like in shell scripting, you do not need to make explicit the type of the variable $i=10 # understood as an integer; $s=”10” # treated as a string
Everything in a Perl script is a statement, and statements must end in semicolon $i=10;
$s1=' winter for the last $i months ';
$s2=” winter for the last $i months “;
print $i;
print $s1;
print $s2;
To echo values on the terminal display, use a print statement: print expr, ...., expr;
print 'winter ', “ for the last $i months, \n”, “unfortunately”
winter for the last 10 months, unfortunately Perl automatically converts a string to an integer or the other way around, depending on the context:
$a=”10”
print “ a is $a \n”
$a1=$a + 20 + only makes sense as an integer operand
print “a1 is $a1 \n”
$a2=$a.” months” . (concatenation) only makes sense for strings
print “a2 is $a2 \n”
$a3=$a.$a1
print “ a3 is $a3 \n”
$a4=$a3-1
print “ a4 is $a4”
a is 10 integer
a1 is 30 integer
a2 is 10 months string
a3 is 1030 string
a4 is 1029 integer
Services: - Perl Scripting Homework | Perl Scripting Homework Help | Perl Scripting Homework Help Services | Live Perl Scripting Homework Help | Perl Scripting Homework Tutors | Online Perl Scripting Homework Help | Perl Scripting Tutors | Online Perl Scripting Tutors | Perl Scripting Homework Services | Perl Scripting