Perl Notes from Udemy
- Perl 模块安装
- Section 1: Basic Perl: Getting Started
-
- T2. Hello World
- T3. Downloading Text and Images With Perl
- T4. Arrays and Checking Whether Files Exist
- T5. Reading Files and Beginning Regular Expressions
- T6. Writing Files and Replacing Text
- T7. Wildcards in Regular Expressions
- T8. Groups: Finding Out What You Actually Matched
- T9. Quantifiers: Greedy vs. Non-Greedy
- T10. Escape Sequences
- T11. Numeric Quantifiers
- Section 2: More on Reading Files Line By Line: Tips, Tricks and Vital Knowledge
-
- T13. Split and reading csv files
- T14. Join and Viewing Data Using Data::Dumper
- T15. Chomp and Removing Spaces in Splits
- T16. "Pushing" Onto Arrays
- T17. Array of arrays
- T18. Hashes: Lookup Tables in Perl
- T19. Iterating over Hashes
- T20. Array of Hashes
- T21. Storing CSV Data in a Data Structure
- T22. Validating CSV Data
- T23. Cleaning CSV Data
- Section 3: Web Scraping and More Regular Expressions
- Section 4: Building a Complete Progam: Command Line Options
- Section 5: Parsing XML and Complex Data Structures
-
- T35. Finding All Files in a Directory and Filtering Arrays
- T36. Processing Files One By One
- T37. Parsing XML with Regular Expressions
- T38. Using XML::Simple, and Extracting Data from Complex Structures
- T39. Extracting Data from Complex Structures: A Complete Example
- T40. Building Complex Data Structures
- Section 6: Working with Databases
- Section 8: Modules and OO Perl
- Section 9: Web Application Basics
- Section 10: Basic Sysadmin Tasks
Perl 模块安装
参考:
各个平台下 Perl 模块安装总结
官网介绍
Error: Can’t create ‘/Library/Perl/5.18/App’:改用sudo
Section 1: Basic Perl: Getting Started
T2. Hello World
use strict;
use warnings;
sub main {
print "Hello World!\n";
}
main();
T3. Downloading Text and Images With Perl
use strict;
use warnings;
use LWP::Simple;
sub main {
print "Downloading ...\n";
# Download the specified HTML page and print it to the console.
# print get("http://www.caveofprogramming.com/");
# Download the Cave of Programming home page HTML and save it to "home.html"
# getstore("http://www.caveofprogramming.com/", "home.html");
# Download logo.png from the Internet and store it in a file named "logo.png"
my $code = getstore('http://www.caveofprogramming.com/wp-content/themes/squiffy/images/logo.png', "logo.png");
# Did we get code 200 (successful request) ?
if($code == 200) {
print "Success\n";
}
else {
print "Failed\n";
}
print "Finished\n";
}
main();
T4. Arrays and Checking Whether Files Exist
use strict;
use warnings;
$|=1;
sub main {
# An array of file names. Note the "@"" for array
my @files = (
'/Users//Desktop/Perl/Tutorial2.pl',
'/Users//Desktop/Perl/Tutorial3.pl',
'/Users//Desktop/Perl/Tutorial4.pl',
);
# foreach loops through the array, setting the loop
# variable ($file in this case) to each value in turn.
foreach my $file (@files) {
# -f tests if a file exists.
if ( -f $file ) {
print "Found file: $file\n";
}
else {
print "File not found: $file\n";
}
}
}
main();
T5. Reading Files and Beginning Regular Expressions
use strict;
use warnings;
# turn off output buffering
$|=1;
sub main {
my $file = '/Users//Desktop/Perl/egg.txt';
# Either successfully open the file or else die (stop the program)
open(INPUT, $file) or die("Input file $file not found.\n");
# Read each line from the file, one line at a time.
while(my $line = <INPUT>) {
if($line=~/egg/) {
print "$line\n";
}
}
close(INPUT);
}
main();
T6. Writing Files and Replacing Text
use strict;
use warnings;
$|=1;
sub main {
# open $input for reading
my $input = '/Users//Desktop/Perl/egg.txt';
open(INPUT, $input) or die("Input file $input not found.\n");
# open $output for writing
my $output = 'output.txt';
open(OUTPUT, '>'.$output) or die "Can't create $output.\n";
# Read the input file one line at a time.
while(my $line = <INPUT>) {
# If this line has the word "egg" in it, write it
# to the output file, otherwise ignore it.
# \b matches the edges (boundaries) of words.
if($line =~ /\begg\b/) {
$line =~ s/\begg\b/dinosaur/ig; # i: case insensitive, g: global
print OUTPUT $line;
}
}
close(INPUT);
close(OUTPUT);
}
main();
T7. Wildcards in Regular Expressions
use strict;
use warnings;
$|=1;
sub main {
my $file = 'C:\tutorial\perl\mymanjeeves.txt';
open(INPUT, $file) or die("Input file $file not found.\n");
while(my $line = <INPUT>) {
# The dot matches any character, even space or punctuation
# e.g. the example below matches
# I was
# I said
# If an
# I take
# etc.
# (only five characters including the space are actually matched by the epxression)
if($line =~ /I..a./) {
print $line;
}
}
close(INPUT);
}
main();
T8. Groups: Finding Out What You Actually Matched
use strict;
use warnings;
$|=1;
sub main {
my $file = 'C:\tutorial\perl\mymanjeeves.txt';
open(INPUT, $file) or die("Input file $file not found.\n");
while(my $line = <INPUT>) {
# Surround the bits you want to "capture" with round brackets
if($line =~ /(I..a.)(...)/) {
# The stuff matched by the first set of round brackets is now in $1
# The stuff matched by the second set is in $2
print "First match: '$1'; second match:'$2'\n";
}
}
close(INPUT);
}
main();
T9. Quantifiers: Greedy vs. Non-Greedy
use strict;
use warnings;
$|=1;
sub main {
my $file = 'C:\tutorial\perl\mymanjeeves.txt';
open(INPUT, $file) or die("Input file $file not found.\n");
while(my $line = <INPUT>) {
# * matches zero or more of the preceding character; e.g.
# d* matches zero or more d's 7* zero or more 7's, etc.
# .* matches zero or more of any character, as many as possible
# .*? matches zero or more of any character, as few as possible
if($line =~ /(s.*?n)/) {
print "$1\n";
}
}
close(INPUT);
}
main();
If you want to match a .
, remember to use \.
.
T10. Escape Sequences
use strict;
use warnings;
$|=1;
sub main {
# \d digit
# \s space
# \S non-space
# \w alphanumeric
# Some examples; in the following examples,
# each example shows the text, the regular expression
# and the output, in that order.
# Digits:
# 'I am 117 years old tomorrow.'
# (\d+)
# Matched: '117'
# Space (will also match a tab)
# I am 117 years old tomorrow.
# (am\s*\d+)
# Matched: 'am 117'
# \S (non space -- note, capital 'S')
# 'I am 117 years old tomorrow.'
# (y\S+)
# Matched: 'years'
# Alphanumeric, including underscore
# \w
# 'Iam117yearsold_tomorrow.'
# (y\w*)
# Matched: 'yearsold_tomorrow'
my $text = 'I am 117 years old tomorrow.';
if($text =~ /(y\S+)/) {
print "Matched: '$1'\n";
}
}
main();
T11. Numeric Quantifiers
use strict;
use warnings;
$|=1;
sub main {
# * zero or more of the preceding character, as many as possible
# + one or more of the preceding, as many as possible
# *? zero or more of the preceding character, as few as possible
# +? one or more of the preceding, as few as possible
# {5} five of the preceding
# {3,6} at least three and at most 6
# {3,} at least three
my $text = 'DE$75883';
if($text =~ /(DE\$\d{3,})/) {
print "Matched: '$1'\n";
}
}
main();
Section 2: More on Reading Files Line By Line: Tips, Tricks and Vital Knowledge
T13. Split and reading csv files
use strict;
use warnings;
$|=1;
sub main {
my $input = 'test.csv';
# execute if the condition is FALSE
unless(open(INPUT, $input)) {
die "\nCannot open $input\n";
}
<INPUT>; # get rid of header
# if not use $line, then can use $_
while(my $line = <INPUT>) {
my @values = split ',', $line;
print $values[1] . "\n";
}
close INPUT;
}
main();
T14. Join and Viewing Data Using Data::Dumper
use strict;
use warnings;
use Data::Dumper;
$|=1;
sub main {
my $input = 'test.csv';
unless(open(INPUT, $input)) {
die "\nCannot open $input\n";
}
<INPUT>;
while(my $line = <INPUT>) {
my @values = split ',', $line;
# print join '|', @values;
print Dumper(@values);
}
close INPUT;
}
main();
Execution output:
$VAR1 = 'Isaac Newton';
$VAR2 = '99.10';
$VAR3 = '15051999
';
$VAR1 = 'Albert Einstein';
$VAR2 = '13.20';
$VAR3 = '11062012
';
$VAR1 = 'Carl Scheele';
$VAR2 = '66.23';
$VAR3 = '01012000
';
$VAR1 = 'Rene Descartes';
$VAR2 = '0.57';
$VAR3 = '10072033
';
The problem is that at the end of each line, there is a \n
which is the reason why ';
is printed out at the next line.
T15. Chomp and Removing Spaces in Splits
use strict;
use warnings;
use Data::Dumper;
$|=1;
sub main {
my $input = 'test.csv';
unless(open(INPUT, $input)) {
die "\nCannot open $input\n";
}
<INPUT>;
while(my $line = <INPUT>) {
chomp $line; # chomp
# get rid of space
my @values = split /\s*,\s*/, $line;
print Dumper(@values);
}
close INPUT;
}
main();
chomp()
Using the Perl chomp() function
perl中chomp的使用
T16. “Pushing” Onto Arrays
use strict;
use warnings;
use Data::Dumper;
$|=1;
sub main {
my @array;
push @array, 'apple';
push @array, 'banana';
push @array, 'peach';
foreach my $element(@array) {
print $element . "\n";
}
}
main();
T17. Array of arrays
use strict;
use warnings;
use Data::Dumper;
my @animals = ('dog', 'cat', 'rabbit');
my @fruits = ('apple', 'banana', 'orange');
my $fruits_ref = \@fruits; # create a reference
print $fruits_ref->[0] . "\n";
my @values;
push @values, \@animals;
push @values, \@fruits;
print Dumper(@values);
Output:
apple
$VAR1 = [
'dog',
'cat',
'rabbit'
];
$VAR2 = [
'apple',
'banana',
'orange'
];
use strict;
use warnings;
use Data::Dumper;
$|=1;
sub main {
my $input = 'test.csv';
unless(open(INPUT, $input)) {
die "\nCannot open $input\n";
}
<INPUT>;
my @lines;
while(my $line = <INPUT>) {
chomp $line;
my @values = split /\s*,\s*/, $line;
push @lines, \@values;
}
close INPUT;
print $lines[3][1] . "\n";
foreach my $line(@lines) {
print Dumper($line);
print "Name " . $line->[0] . "\n";
}
}
main();
Output:
0.57
$VAR1 = [
'Isaac Newton',
'99.10',
'15051999'
];
Name Isaac Newton
$VAR1 = [
'Albert Einstein',
'13.20',
'11062012'
];
Name Albert Einstein
$VAR1 = [
'Carl Scheele',
'66.23',
'01012000'
];
Name Carl Scheele
$VAR1 = [
'Rene Descartes',
'0.57',
'10072033'
];
Name Rene Descartes
T18. Hashes: Lookup Tables in Perl
use strict;
use warnings;
use Data::Dumper;
$| = 1;
sub main {
my %months = (
1 => "Jan",
5 => "May",
7 => "Jul",
);
print $months{
5} . "\n";
my %days;
$days{
"Sunday"} = 1;
$days{
"Monday"} = 2;
$days{
"Friday"} = 6;
my $day = $days{
"Friday"};
print "Friday is day $day\n";
}
main();
the order of hash can not be relied on.
T19. Iterating over Hashes
use strict;
use warnings;
use Data::Dumper;
$| = 1;
sub main {
my %foods = (
"mice" => "cheese",
"dogs" => "meat",
"birds" => "seeds",
);
# We can define an array of variables like this:
# round brackets are required
my ($one, $two, $three) = (13, 21, 38);
print "The value of \$two is $two\n";
while( my ($key, $value) = each %foods) {
print "$key: $value\n";
}
# sort the key according to the order of A-Z
foreach my $key(sort keys %foods) {
my $value = $foods{
$key};
print "$key = $value\n";
}
}
main();
Output:
The value of $two is 21
dogs: meat
birds: seeds
mice: cheese
birds = seeds
dogs = meat
mice = cheese
T20. Array of Hashes
use strict;
use warnings;
use Data::Dumper;
$| = 1;
sub main {
my %hash1 = (
"cat" => "meat",
"birds" => "seeds",
"fish" => "worms",
);
my @test;
# We could push a reference to a hash onto an array.
push @test, \%hash1;
# We could also just refer to the element after the end of the array
# and by setting it, create it:
$test[1] = \%hash1;
print $test[0]{
"birds"} . "\n";
print $test[1]{
"fish"} . "\n";
}
main();
Output:
seeds
worms
T21. Storing CSV Data in a Data Structure
use strict;
use warnings;
use Data::Dumper;
$|=1;
sub main {
my $input = 'test.csv';
unless(open(INPUT, $input)) {
die "\nCannot open $input\n";
}
<INPUT>;
my @data;
while(my $line = <INPUT>) {
chomp $line;
my ($name, $payment, $date) = split /\s*,\s*/, $line;
my %values = (
"Name" => $name,
"Payment" => $payment,
"Date" => $date,
);
push @data, \%values;
}
close INPUT;
foreach my $data(@data){
print $data -> {
"Payment"} . "\n";
}
print "Descartes: " . $data[3]-> {
"Name"} . "\n";
print "Descartes: " . $data[3]{
"Name"} . "\n";
}
main();
T22. Validating CSV Data
use strict;
use warnings;
use Data::Dumper;
$|=1;
sub main {
my $input = 'test.csv';
unless(open(INPUT, $input)) {
die "\nCannot open $input\n";
}
<INPUT>;
my @data;
LINE: while(my $line = <INPUT>) {
chomp $line;
my @values = split /\s*,\s*/, $line;
if (scalar(@values) < 3){
print "Invalid line: $line\n";
next LINE;
}
foreach my $value(@values){
if ($value eq ''){
print "Invalid line: $line\n";
next LINE;
}
}
my ($name, $payment, $date) = @values;
my %values = (
"Name" => $name,
"Payment" => $payment,
"Date" => $date,
);
push @data, \%values;
}
close INPUT;
foreach my $data(@data){
print $data -> {
"Payment"} . "\n";
}
print "Descartes: " . $data[3]-> {
"Name"} . "\n";
}
main();
Input:
Name,Payment,Date
Isaac Newton ,99.1,15051999
Albert Einstein,13.2,11062012
Carl Scheele,66.23,1012000
Rene Descartes,0.57,10072033
Sarah,10,
,,
Nicolas,0,
Output:
Invalid line: Sarah,10,
Invalid line: ,,
Invalid line: Nicolas,0,
99.1
13.2
66.23
0.57
Descartes: Rene Descartes
T23. Cleaning CSV Data
use strict;
use warnings;
use Data::Dumper;
$|=1;
sub main {
my $input = 'test.csv';
unless(open(INPUT, $input)) {
die "\nCannot open $input\n";
}
<INPUT>;
my @data;
LINE: while(my $line = <INPUT>) {
# replace the space at the beginning of the line
$line =~ s/^\s*//;
# replace the space at the end of the line
$line =~ s/\s*$//;
# replace the space at the beginning and end of the line
# g stands for global, replace as many as possible
$line =~ s/^\s*|\s*$//g;
$line =~ /\S+/ or next;
chomp $line;
my @values = split /\s*,\s*/, $line;
if (scalar(@values) < 3){
print "Invalid line: $line\n";
next LINE;
}
foreach my $value(@values){
if ($value eq ''){
print "Invalid line: $line\n";
next LINE;
}
}
my ($name, $payment, $date) = @values;
my %values = (
"Name" => $name,
"Payment" => $payment,
"Date" => $date,
);
push @data, \%values;
}
close INPUT;