Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 7, 2016
0 parents commit 6506f7b
Show file tree
Hide file tree
Showing 29 changed files with 1,300 additions and 0 deletions.
68 changes: 68 additions & 0 deletions 1590594541-1/Chapter03/charcount
@@ -0,0 +1,68 @@
#!/usr/bin/perl
#
# prints the line number and the number of consonants, vowels and special
# characters found on each line of the filename given as an argument.

use strict;
use FileHandle;

my $file = $ARGV[0] || '';
my $verbose = $ARGV[1] || 0;

unless (-f $file) {
die("Usage: $0 filename [-v]");
}
my $FH = FileHandle->new("< $file") or die("unable to open file($file): $!");

my $i_cnt = 0;
my $i_cons = 0;
my $i_vows = 0;
my $i_spec = 0;

# parse the file
while (<$FH>) {
my $line = $_;
$i_cons += my $cons = &parse($line, '[bcdfghjklmnpqrstvwxyz]');
$i_vows += my $vows = &parse($line, '[aeiou]');
$i_spec += my $spec = &parse($line, '[^a-zA-Z0-9\s]');
$DB::trace = 0;
print sprintf('%6d', $i_cnt).':'.
' hard'.('.'x(8-length($cons))).$cons.
' soft'.('.'x(8-length($vows))).$vows.
' spec'.('.'x(8-length($spec))).$spec;
print $verbose ? " $line\n" : "\n";
$i_cnt++;
}

print ' total:'.
' hard'.(' 'x(8-length($i_cons))).$i_cons.
' soft'.(' 'x(8-length($i_vows))).$i_vows.
' spec'.(' 'x(8-length($i_spec))).$i_spec.
"\n";

sub parse {
my $str = shift;
my $reg = shift;
my $cnt = my @cnt = ($str =~ /($reg)/g);
return $cnt;
}

exit 0;

__END__
=head1 NAME
charcount - display lineinfo on input files
=head1 SYNOPSIS
charcount inputfilename
=head1 DESCRIPTION
Prints the line number, followed by the number of consonents, vowels and
special characters found on each line of the filename given as an argument.
=cut
128 changes: 128 additions & 0 deletions 1590594541-1/Chapter03/walrus
@@ -0,0 +1,128 @@
The Walrus and The Carpenter by Lewis Carroll.

The sun was shining on the sea,
Shining with all his might:
He did his very best to make
The billows smooth and bright
And this was odd, because it was
The middle of the night.

The moon was shining sulkily,
Because she thought the sun
Had got no business to be there
After the day was done
"It's very rude of him," she said,
"To come and spoil the fun!"

The sea was wet as wet could be,
The sands were dry as dry.
You could not see a cloud, because
No cloud was in the sky:
No birds were flying overhead
There were no birds to fly.

The Walrus and the Carpenter
Were walking close at hand;
They wept like anything to see
Such quantities of sand:
"If this were only cleared away,"
They said, "it would be grand!"

"If seven maids with seven mops
Swept it for half a year.
Do you suppose," the Walrus said,
"That they could get it clear?"
"I doubt it," said the Carpenter,
And shed a bitter tear.

"O Oysters, come and walk with us!"
The Walrus did beseech.
"A pleasant walk, a pleasant talk,
Along the briny beach:
We cannot do with more than four,
To give a hand to each."

The eldest Oyster looked at him,
But never a word he said:
The eldest Oyster winked his eye,
And shook his heavy head
Meaning to say he did not choose
To leave the oyster-bed.

But four young Oysters hurried up,
All eager for the treat:
Their coats were brushed, their faces washed,
Their shoes were clean and neat
And this was odd, because, you know,
They hadn't any feet.

Four other Oysters followed them,
And yet another four;
And thick and fast they came at last,
And more, and more, and more
All hopping through the frothy waves,
And scrambling to the shore.

The Walrus and the Carpenter
Walked on a mile or so,
And then they rested on a rock
Conveniently low:
And all the little Oysters stood
And waited in a row.

"The time has come," the Walrus said,
"To talk of many things:
Of shoes and ships and sealing-wax
Of cabbages and kings
And why the sea is boiling hot
And whether pigs have wings."

"But wait a bit," the Oysters cried,
"Before we have our chat;
For some of us are out of breath,
And all of us are fat!"
"No hurry!" said the Carpenter.
They thanked him much for that.

"A loaf of bread," the Walrus said,
"Is what we chiefly need:
Pepper and vinegar besides
Are very good indeed
Now if you're ready, Oysters dear,
We can begin to feed."

"But not on us!" the Oysters cried,
Turning a little blue.
"After such kindness, that would be
A dismal thing to do!"
"The night is fine," the Walrus said.
"Do you admire the view?

"It was so kind of you to come!
And you are very nice!"
The Carpenter said nothing but
"Cut us another slice:
I wish you were not quite so deaf
I've had to ask you twice!"

"It seems a shame," the Walrus said,
"To play them such a trick,
After we've brought them out so far,
And made them trot so quick!"
The Carpenter said nothing but
"The butter's spread too thick!"

"I weep for you," the Walrus said:
"I deeply sympathize."
With sobs and tears he sorted out
Those of the largest size,
Holding his pocket-handkerchief
Before his streaming eyes.

"O Oysters," said the Carpenter,
"You've had a pleasant run!
Shall we be trotting home again?'
But answer came there none
And this was scarcely odd, because
They'd eaten every one.

70 changes: 70 additions & 0 deletions 1590594541-1/Chapter04/findvars
@@ -0,0 +1,70 @@
#!/usr/bin/perl

%INC = ();

use FileHandle;

map { eval "require $_;" } @ARGV;

my $regex = $ENV{REGEX} || '[\$\@\%\*]&+';

map { $files{$_} = FileHandle->new("< $INC{$_}") } keys %INC;

&getdata(%files);
&report(%data);

exit 0;

sub wanted {
return ($_[0] =~ /$regex/ && $_[0] !~ /^\s*\#/ ? 1 : 0);
}

sub getdata {
my %files = @_;
foreach my $f (sort keys %files) {
$cnt = 0;
my $fh = $files{$f};
while (<$fh>) {
++$cnt;
$data{$f}{$cnt} = $_ if &wanted;
}
}
}

sub report {
my %data = @_;
foreach my $k (sort keys %data) {
print "\n$k vars:\n";
foreach my $r (sort { $a <=> $b } keys %{$data{$k}}) {
print "\t[$r]\t$data{$k}{$r}";
}
}
}

__END__
=head1 NAME
findvars - display variables in loaded packages
=head1 SYNOPSIS
findvars [modulenames]+
=head1 DESCRIPTION
Given a list of module names, find and display all variables found.
=head1 REGEX
The regex used to find variables is fairly the clunky: C<[$@%*]> by default.
If you wish to define another one, set the C<REGEX="xxx"> enviroment variable,
where C<xxx> is the regular expression to use.
=head1 NOTES
Note that although we ignore directly commented out lines, we slurp up C<pod>
indiscriminately.
=cut
25 changes: 25 additions & 0 deletions 1590594541-1/Chapter04/findvars.diff
@@ -0,0 +1,25 @@
--- findvars.original 2004-11-07 10:56:17.000000000 +0100
+++ findvars.fixed 2004-11-07 10:35:06.000000000 +0100
@@ -4,9 +4,11 @@

use FileHandle;

-map { eval "require $_;" } @ARGV;
+map { eval "require $_;"
+ or print STDERR "Warning: unable to require $_: $@"
+} @ARGV;

-my $regex = $ENV{REGEX} || '[\$\@\%\*]&+';
+my $regex = $ENV{REGEX} || '[\$\@\%\*\&]+';

map { $files{$_} = FileHandle->new("< $INC{$_}") } keys %INC;

@@ -26,7 +28,7 @@
my $fh = $files{$f};
while (<$fh>) {
++$cnt;
- $data{$f}{$cnt} = $_ if &wanted;
+ $data{$f}{$cnt} = $_ if &wanted($_);
}
}
}
72 changes: 72 additions & 0 deletions 1590594541-1/Chapter04/findvars.fixed
@@ -0,0 +1,72 @@
#!/usr/bin/perl

%INC = ();

use FileHandle;

map { eval "require $_;"
or print STDERR "Warning: unable to require $_: $@"
} @ARGV;

my $regex = $ENV{REGEX} || '[\$\@\%\*\&]+';

map { $files{$_} = FileHandle->new("< $INC{$_}") } keys %INC;

&getdata(%files);
&report(%data);

exit 0;

sub wanted {
return ($_[0] =~ /$regex/ && $_[0] !~ /^\s*\#/ ? 1 : 0);
}

sub getdata {
my %files = @_;
foreach my $f (sort keys %files) {
$cnt = 0;
my $fh = $files{$f};
while (<$fh>) {
++$cnt;
$data{$f}{$cnt} = $_ if &wanted($_);
}
}
}

sub report {
my %data = @_;
foreach my $k (sort keys %data) {
print "\n$k vars:\n";
foreach my $r (sort { $a <=> $b } keys %{$data{$k}}) {
print "\t[$r]\t$data{$k}{$r}";
}
}
}

__END__
=head1 NAME
findvars - display variables in loaded packages
=head1 SYNOPSIS
findvars [modulenames]+
=head1 DESCRIPTION
Given a list of module names, find and display all variables found.
=head1 REGEX
The regex used to find variables is fairly the clunky: C<[$@%*]> by default.
If you wish to define another one, set the C<REGEX="xxx"> enviroment variable,
where C<xxx> is the regular expression to use.
=head1 NOTES
Note that although we ignore directly commented out lines, we slurp up C<pod>
indiscriminately.
=cut

0 comments on commit 6506f7b

Please sign in to comment.