Colorado State University
Computer Science Department
CT 320: Network and System Administration
Original slides from Dr. James Walden at Northern Kentucky University.
Source: http://en.wikipedia.org/wiki/Perl
Source: http://en.wikipedia.org/wiki/Perl
Source: http://en.wikipedia.org/wiki/Common_Gateway_Interface
#! /usr/bin/perl -w print "Hello, world!\n"
$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";
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";
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";
#! /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.
$alpha = 5; # alpha is a scalar @beta = (11,22,33); # beta is an array $alpha = $beta[1]; # beta[1] is a scalar, however
$
indicates a scalar
@ indicates an array
%
indicates a hash, or associative array
It’s the type of the value, not the type of the variable.
# 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
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";
# Length versus contents $x = @stuff; print '$x = ' . "$x\n"; $x = "@stuff"; print '$x = ' . "$x\n";
$x = 6 $x = apples pears eels eggs lard butter
#! /usr/bin/perl -w # File Input open(INFO, "whatever"); @lines = <INFO>; close(INFO); print @lines;
# 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
open(FILE, ">temp.txt"); print FILE "This line goes to the file.\n"; close(FILE);
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
# 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
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");
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"; }
Source: http://www.cpan.org/modules/
Modified: 2015-10-15T13:09 User: Guest Check: HTML CSSEdit History Source |
Apply to CSU |
Contact CSU |
Disclaimer |
Equal Opportunity Colorado State University, Fort Collins, CO 80523 USA © 2015 Colorado State University |