Commit f848d7f3 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

9781565921498

0 → 120000
+1 −0
Changes for 9781565921498: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
9781565921498
 No newline at end of file

README.ERRATA.html

0 → 100755
+11 −0
Changes for README.ERRATA.html: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
<HTML>
<BODY>


The code in these example archives may not match the examples in the latest print copy of the book. Please check 
<A HREF="http://www.oreilly.com/catalog/pperl2/errata/"><B>the posted errata</B></A> for discrepancies between the book and online errata.  We 
apologize for the inconvenience.

</BODY>
</HTML>

README.md

0 → 100644
+15 −0
Changes for README.md: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Example files for the title:  
	  
# Programming Perl 2nd Edition, by Tom Christiansen
	  
[![Programming Perl 2nd Edition, by Tom Christiansen](http://akamaicovers.oreilly.com/images/9781565921498/cat.gif)](https://www.safaribooksonline.com/)
	  
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
	  
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
	  
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
	  
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
	  
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.

examples/ch1/MISC

0 → 100644
+234 −0
Changes for examples/ch1/MISC: 234 added lines, 0 removed lines.
Original line number Diff line number Diff line
print "Howdy, world!\n";
*****
$phrase = "Howdy, world!\n";          # Set a variable.
print $phrase;                        # Print the variable.
*****
$answer = 42;               # an integer
$pi = 3.14159265;           # a "real" number
$avocados = 6.02e23;        # scientific notation
$pet = "Camel";             # string
$sign = "I love my $pet";   # string with interpolation
$cost = 'It costs $100';    # string without interpolation
$thence = $whence;          # another variable
$x = $moles * $avocados;    # an expression
$cwd = `pwd`;               # string output from a command
$exit = system("vi $x");    # numeric status of a command
$fido = new Camel "Fido";   # an object
*****
$camels = '123';
print $camels + 1, "\n";
*****
@home = ("couch", "chair", "table", "stove");
*****
($potato, $lift, $tennis, $pipe) = @home;
*****
($alpha,$omega) = ($omega,$alpha);
*****
$home[0] = "couch";
$home[1] = "chair";
$home[2] = "table";
$home[3] = "stove";
*****
%longday = ("Sun", "Sunday", "Mon", "Monday", "Tue", "Tuesday",
            "Wed", "Wednesday", "Thu", "Thursday", "Fri",
            "Friday", "Sat", "Saturday");
*****
%longday = (
    "Sun" => "Sunday",
    "Mon" => "Monday",
    "Tue" => "Tuesday",
    "Wed" => "Wednesday",
    "Thu" => "Thursday",
    "Fri" => "Friday",
    "Sat" => "Saturday",
);
*****
$wife{"Adam"} = "Eve";
*****
print "Adam's wife is ", $wife{'Adam'}, ".\n";
*****
$e = exp(1);   # 2.718281828459, or thereabouts
*****
Noel 25
Ben 76
Clementine 49
Norm 66
Chris 92
Doug 42
Carol 25
Ben 12
Clementine 0
Norm 66
*****
% perl -e 'print "Hello, world!\n";'
*****
% perl gradation
*****
#!/usr/bin/perl
*****
% gradation
*****
% ../bin/gradation
*****
#!/bin/sh -- # perl, to stop looping
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if 0;
*****
#!/usr/bin/perl -w
*****
open(SESAME, "filename");               # read from existing file
open(SESAME, "<filename");              #   (same thing, explicitly)
open(SESAME, ">filename");              # create file and write to it
open(SESAME, ">>filename");             # append to existing file
open(SESAME, "| output-pipe-command");  # set up an output filter
open(SESAME, "input-pipe-command |");   # set up an input filter
*****
print STDOUT "Enter a number: ";          # ask for a number
$number = <STDIN>;                        # input the number
print STDOUT "The number is $number\n";   # print the number
*****
chop($number = <STDIN>);    # input number and remove newline
*****
$number = <STDIN>;          # input number
chop($number);              # remove newline
*****
$a = 123;
$b = 456;
print $a + $b;     # prints 579
print $a . $b;     # prints 123456
*****
$a = 123;
$b = 3;
print $a * $b;     # prints 369
print $a x $b;     # prints 123123123
*****
print $a . ' is equal to ' . $b . "\n";    # dot operator
print $a, ' is equal to ', $b, "\n";       # list
print "$a is equal to $b\n";               # interpolation
*****
print "-" x $scrwid, "\n";
*****
$a = $b;
$a = $b + 5;
$a = $a * 3;
*****
$a *= 3;
*****
$line .= "\n";  # Append newline to $line.
$fill x= 80;    # Make string $fill into 80 repeats of itself.
$val ||= "2";   # Set $val to 2 if it isn't already set.
*****
chop($number = <STDIN>);
*****
$a = 5;        # $a is assigned 5
$b = ++$a;     # $b is assigned the incremented value of $a, 6
$c = $a--;     # $c is assigned 6, then $a is decremented to 5
*****
open(GRADES, "grades") or die "Can't open file grades: $!\n";
*****
-e "/usr/bin/perl" or warn "Perl is improperly installed\n";
-f "/vmunix" and print "Congrats, we seem to be running BSD Unix\n";
*****
if ($debug_level > 0) {
    # Something has gone wrong.  Tell the user.
    print "Debug: Danger, Will Robinson, danger!\n";
    print "Debug: Answer was '54', expected '42'.\n";
}
*****
if ($city eq "New York") {
    print "New York is northeast of Washington, D.C.\n";
}
elsif ($city eq "Chicago") {
    print "Chicago is northwest of Washington, D.C.\n";
}
elsif ($city eq "Miami") {
    print "Miami is south of Washington, D.C.  And much warmer!\n";
}
else {
    print "I don't know where $city is, sorry.\n";
}
*****
unless ($destination eq $home) {
    print "I'm not going home.\n";
}
*****
while ($tickets_sold < 10000) {
    $available = 10000 - $tickets_sold;
    print "$available tickets are available.  How many would you like: ";
    $purchase = <STDIN>;
    chomp($purchase);
    $tickets_sold += $purchase;
}
*****
print "This show is sold out, please come back later.\n";
*****
 while ($line = <GRADES>) {
*****
while (@ARGV) {
    process(shift @ARGV);
}
*****
for ($sold = 0; $sold < 10000; $sold += $purchase) {
    $available = 10000 - $sold;
    print "$available tickets are available.  How many would you like: ";
    $purchase = <STDIN>;
    chomp($purchase);
}
*****
foreach $user (@users) {
    if (-f "$home{$user}/.nexrc") {
        print "$user is cool... they use a perl-aware vi!\n";
    }
}
*****
foreach $key (sort keys %hash) {
*****
foreach $user (@users) {
    if ($user eq "root" or $user eq "lp") {
        next;
    }
    if ($user eq "special") {
        print "Found the special account.\n";
        # do some processing
        last;
    }
}
*****
LINE: while ($line = <ARTICLE>) {
    last LINE if $line eq "\n"; # stop on first blank line
    next LINE if /^#/;          # skip comment lines
    # your ad here
}
*****
while ($line = <FILE>) {
    if ($line =~ /http:/) {
        print $line;
    }
}
*****
while (<FILE>) {
    print if /http:/;
}
*****
while (<FILE>) {
    print if /http:/;
    print if /ftp:/;
    print if /mailto:/;
    # What next?
}
*****
$_ = "fred xxxxxxx barney";
s/x*//;
*****
/\bFred\b/
*****
next LINE if /^#/;
*****
s/(\S+)\s+(\S+)/$2 $1/
*****
@array = (1 + 2, 3 - 4, 5 * 6, 7 / 8);
*****
sort @guys, @gals, other();
*****
print reverse sort map {lc} keys %hash;
*****

examples/ch1/gradation

0 → 100644
+19 −0
Changes for examples/ch1/gradation: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
#!/usr/bin/perl

open(GRADES, "grades") or die "Can't open grades: $!\n";
while ($line = <GRADES>) {
    ($student, $grade) = split(/ /, $line);
    $grades{$student} .= $grade . " ";
}

foreach $student (sort keys %grades) {
    $scores = 0;
    $total = 0;    
    @grades = split(/ /, $grades{$student});
    foreach $grade (@grades) {
        $total += $grade;
        $scores++;
    }
    $average = $total / $scores;
    print "$student: $grades{$student}\tAverage: $average\n";
}
Loading