0

Beginner’s Introduction to Perl Practice

Reference for this practice: http://www.perl.com/pub/2000/10/begperl1.html

  • The n indicates the newline character. Perl does not skip a new line unless you include with n.
    #!/usr/bin/perl
    print "Hi there!n";
    

    Output:

    Hi there!
    

 

Functions & Statements

  • Almost all functions can be given a list of parameters, which are separated by commas.
  • Each statement ends with a semicolon.
  • Statements don’t need to be on separate lines.
  • There may be multiple statements on one line or a single statement can be split across multiple lines.
    #!/usr/bin/perl
    print "This is a single statement.";
    print "Look, ", "a ", "list!";
    print "n";
    # multiple statements on one line or
    #a single statement can be split across multiple line.
    print "This is "; print "two statements.n"; print "But this ", "is only one statement.n";
    

    Output:

    This is a single statement.Look, a list!
    This is two statements.
    But this is only one statement.
    

 

Numbers, Strings & Quotes

  • Never insert commas or spaces into numbers in Perl.
  • Single quote – the content is taken in a literal way.
  • Double quotes – the content should be interpreted.
    #!/usr/bin/perl
    
    # testing with n
    print "This stringnshows up on two lines.";
    print "n";
    print 'This string n shows up on only one.';
    print "n";
    
    # testing with t
    print "This stringtshows in tab mode.";
    print "n";
    print 'This string t has no tab mode.';
    print "n";
    

    Output:

     This string
     shows up on two lines.
     This string n shows up on only one.
     This string shows in tab mode.
     This string t has no tab mode.
     

 

Variables

  • Three types of variables: scalars, arrays and hashes.
  • All variable names are a punctuation character, a letter or underscore, and one or more alphanumeric characters or underscores.
  • Scalars are single things whcih can be a number or a string.
  • The name of a scalar begins with a dollar sign, such as $i or $item.
  • Does not need to specify whether a scalar is a number or a string because Perl converts it automatically.
  • Perl will convert the scalar variables that exist in double-quoted string to a value.
#!/usr/bin/perl

$apple_count=5;
$count_report = "There are $apple_count apples.";
print "The report is: $count_reportn";

Output:

The report is: There are 5 apples.

 

Numbers

  • Numbers in Perl can be manipulated with mathematical operations: addition, multiplication, division and subtraction.
  • Special operators can be used to manipulate a scalar’s value without needing two elements in an equation.
#!/usr/bin/perl

# operations
$a = 5;
print "a = $an";
$b = $a + 10;
print "b = $bn";
$c = $b * 10;
print "c = $cn";
$a = $a - 1;
print "a = $an";

Output:

a = 5
b = 15
c = 150
a = 4
#!/usr/bin/perl
$a = 5;
print "a : $an";
$a++;
print "after a++ : $an";
$a +=10;
print "after a+=10 : $an";
$a /=2;
print "after a /=2 : $an";

Output:

a : 5
after a++ : 6
after a+=10 : 16
after a /=2 : 8

 

Strings

  • Period (.) puts strings together.
  • Plus sign (+) adds numbers.
#!/usr/bin/perl

$a = "8";
print "a = $an";
$b = $a + "1";
print "b = $bn";
$c = $a . "1";
print "c = $cn";

Output:

a = 8
b = 9
c = 81

 

Arrays

  • Array names begin with @.
  • Define arrays by listing their contents in parentheses, separated by commas.
  • To retrieve the elements of an array, you replace the @ sign with a $ sign, and follow that with the index position of the element you want.
  • Find the length of an array with $#array_name.
  • If the array doesn’t exist or is empty, $#array_name is -1.
  • Resize an array by changing the value of $#array_name.
#!/usr/bin/perl

@months = ("January","February","March");
@fruits= ("orange","grapes","apples");

# print the contents of months
print "Months: @monthsn";

# print the first element in months array
print "First element in months array: $months[0]n";

# Rename March to Mjune
$months[2]="Mjune";
print "Months after rename: @monthsn";

# Create a new array and assign a value
$vege[0]="carrot";
print "Vege: @vegen";

# Print the length of months
print "Length of months: $#monthsn";

Output:

Months: January February March
First element in months array: January
Months after rename: January February Mjune
Vege: carrot
Length of months: 2
After resize the array months length is: 0
The current content in array months is: January

 

Hashes

  • A key and a value.
  • Each key in a hash has one and only one corresponding value.
  • The name of a hash begins with a percentage sign, like %parents.
  • Define hashes by comma-separated pairs of key and value.
#!/usr/bin/perl

%days_in_summer = ("July" => 31, "August" => 31, "September" => 30);

# Print value
print $days_in_summer{"September"};
print "n";

# Modify
$days_in_summer{"February"} = 29;

@month_list = keys %days_in_summer;
print "@month_listn";

Output:

30
February September July August

 

Loops

#!/usr/bin/perl
for $i(1,2,3,4,5) {
print "$in";
}

Output:

1
2
3
4
5
  • You can specify a range of numbers as (1..5) for (1,2,3,4,5).
#!/usr/bin/perl
@one_to_ten=(1..10);
$top_limit = 25;
for $i(@one_to_ten, 15,20..$top_limit)
{
print "$in";
}

Output:

1
2
3
4
5
6
7
8
9
10
15
20
21
22
23
24
25
#!/usr/bin/perl
%month_has = ("February" => 29, "March" => 31, "April" => 30);
for $i(keys %month_has)
{
print "$i has $month_has{$i} days.n";
}

for $marx ('Groucho', 'Harpo', 'Zeppo', 'Karl') {
print "$marx is my favourite Marx brother.n";
}

Output:

February has 29 days.
March has 31 days.
April has 30 days.
Groucho is my favourite Marx brother.
Harpo is my favourite Marx brother.
Zeppo is my favourite Marx brother.
Karl is my favourite Marx brother.</pre>