CT320

CT320: Network and System Administration

Fall 2015

Perl

See this page as a slide show

PERL PROGRAMMING

Colorado State University

Computer Science Department

CT 320: Network and System Administration

Original slides from Dr. James Walden at Northern Kentucky University.

Perl Language

Source: http://en.wikipedia.org/wiki/Perl

Perl History

Source: http://en.wikipedia.org/wiki/Perl

Perl Usage (continued)

Perl Usage (continued)

Source: http://en.wikipedia.org/wiki/Common_Gateway_Interface

Perl Documentation

Basic Perl

#! /usr/bin/perl -w
print "Hello, world!\n"

Perl Scalars

$alpha = 123456
$beta = 3.14159
$gamma = String
$alpha = 123456;
$beta = 3.14159;
$gamma = "String";
print '$alpha = ' . $alpha . "\n";
print '$beta = ' . "$beta\n";
print "\$gamma = $gamma\n";

Perl Math

1 + 2 = 3
3 - 4 = -1
5 * 6 = 30
7 / 8 = 0.875
3 ** 4 = 81
5 % 2 = 1
#! /usr/bin/perl -w
$alpha = 1 + 2;
print "1 + 2 = $alpha\n";
$alpha = 3 - 4;
print "3 - 4 = $alpha\n";
$alpha = 5 * 6;
print "5 * 6 = $alpha\n";
$alpha = 7 / 8;
print "7 / 8 = $alpha\n";
$alpha = 3 ** 4;
print "3 ** 4 = $alpha\n";
$alpha = 5 % 2;
print "5 % 2 = $alpha\n";

Perl Strings

alpha = Foo
beta = Bar
gamma = FooBar
delta = 3
epsilon = BarBarBar
#! /usr/bin/perl -w
$alpha = "Foo";
$beta = 'Bar';
$gamma= $alpha . $beta;
$delta = 3;
$epsilon = $beta x $delta;
print "alpha = $alpha\n";
print "beta = $beta\n";
print "gamma = $gamma\n";
print "delta = $delta\n";
print "epsilon = $epsilon\n";

Declarations

    #! /usr/bin/perl -w
    use strict;
    my $alpha = 42;
    print "alpha is $a1pha";

Global symbol "$a1pha" requires explicit package name at ./typo line 4.
Execution of ./typo aborted due to compilation errors.

Sigils

$alpha = 5;             # alpha is a scalar
@beta = (11,22,33);     # beta is an array
$alpha = $beta[1];      # beta[1] is a scalar, however

It’s the type of the value, not the type of the variable.

Perl Arrays

# Array Definition
@stuff = ("apples", "pears", "eels");
@numbers = (123, 456, 789, 987, 654, 321);
# Array access
print '$stuff[2] = ', $stuff[2], "\n";
print '$numbers[4] = ', "$numbers[4]\n";
$stuff[2] = eels
$numbers[4] = 654

Perl Arrays (continued)

stuff = apples pears eels eggs lard butter milk
grub = milk
stuff = apples pears eels eggs lard butter
# Adding and removing elements
push(@stuff, "eggs", "lard");
@morestuff = ("butter", "milk");
push(@stuff, @morestuff);
print "stuff = @stuff\n";
$grub = pop(@stuff);
print "grub = $grub\n";
print "stuff = @stuff\n";

Perl Arrays (continued)

# Length versus contents
$x = @stuff;
print '$x = ' . "$x\n";
$x = "@stuff";
print '$x = ' . "$x\n";
$x = 6
$x = apples pears eels eggs lard butter

File I/O

    #! /usr/bin/perl -w
    # File Input
    open(INFO, "whatever");
    @lines = <INFO>;
    close(INFO);
    print @lines;

File I/O

    # File modes
    open(INFO, "datafile");   # Open for input
    open(INFO, ">datafile");  # Open for output
    open(INFO, ">>datafile"); # Open for append
    open(INFO, "<datafile");  # Open for input

File Output

    open(FILE, ">temp.txt");
    print FILE "This line goes to the file.\n";
    close(FILE);

Perl Loops

do while 0
do while 1
do while 2
do while 3
while 2
while 1
while 0
for 4
for 5
for 6
#! /usr/bin/perl -w
$i = 0;
do {
    print "do while $i\n";
} while $i++ < 3;

$j = 3;
while ($j-- > 0) {
    print "while $j\n";
}

for $var (4..6) {
    print "for $k\n";
}

# or a C-style for-loop

Perl Functions

# Subroutine
sub total {
    $sum = 0;
    foreach $item (@_) {
        $sum += $item;
    }
    return $sum;
}
# Main program
@data1 = (1,2,3,4);
$answer = total(@data1);
print "Sum of @data1 is $answer\n";
Sum of 1 2 3 4 is 10

System Calls

Mon Dec 23 09:25:05 MST 2024
one
two
greybull
$a = `date`;        # Note backquotes
print $a;           # Includes a newline

@info = `head -4 numbers`;
#print @info;
print $info[0];     # first line
print $info[1];     # second line

$status = system("cat /etc/hostname");

Admin Example

Nameservers from /etc/resolv.conf
192.168.0.1
205.171.3.25
$path = "/etc/resolv.conf";
open (CONFIG, $path);
print "Nameservers from $path:\n";
while (<CONFIG>) {
    chomp;
    @values = split(' ', $_);
    print "$values[1]\n"
        if lc($values[0]) eq "nameserver";
}

Perl Packages

Source: http://www.cpan.org/modules/

Perl Features

Source: http://linuxconfig.org/perl-programming-tutorial/

Modified: 2015-10-15T13:09

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building