20070227
From Wsms
previous next
GO TO:
Parent class notes: Perl Programming Language class notes
Back to: Perl programming language
Contents |
[edit]
Our next book
It will be the MySQL 3th edition by Paul DuBois. The ISBN will is 0-672-32673-6.
[edit]
More on Perl
[edit]
The ternary operator
- It is a replacement for an if...else statement:
[edit]
Excercise with files
Here is an excercise from the book. Hope you like it:
#!/usr/bin/perl
# Can you overwrite an existing file (not the same as the input file)?
# Can you use regular expression metacharacters in the search pattern?
# (That is, can you enter (fred|wilma) flintstone to search for either name?)
# Answer: Yes It is possible
# Can you use the memory variables and backslash escapes in the replacement string?
# (That is, can you use \u\L$1\E Flintstone as the replacement string
# to properly capitalize the names of Fred and Wilma?)
# Answer: No, it is not possible
sub ClearScreen {
for ($i=1;$i<50;$i++) {
print "\n";
};
}
&ClearScreen;
# Make a program which asks the user
# for a source file name, a destination
# file name, a search pattern, and a
# replacement string.
print "Please write the name of the source file: \n";
chomp (my $srcfn = <STDIN>);
while (!(open SOURCE, "<".$srcfn)) { # Asking the source file name
print "Failed attempt to open $srcfn for reading\n";
print "Please write the name of the source file: \n";
chomp ($srcfn = <STDIN>);
};
print "File $srcfn succesfully opened for source\n";
print "Please write the name of the destination file: \n";
chomp (my $destfn = <STDIN>);
while (!(open DEST, ">".$destfn)) { # Asking the destination file name
print "Failed attempt to open $destfn for writing\n";
print "Please write the name of the destination file: \n";
chomp ($destfn = <STDIN>);
};
print "File $destfn succesfully opened for destination\n";
print "Please, enter the search pattern:\n";
chomp(my $searchp = <STDIN>);
print "Please, enter the replacement string:\n";
chomp(my $repstr = <STDIN>);
# Your program should read the source
# file and write it out as the
# destination file, replacing the
# search pattern with the replacement
# string wherever it appears.
# That is, the destination file will
# be a modified duplicate of the
# source file.
print "Procesing $srcfn...\n";
while (<SOURCE>) {
$_ =~ s#$searchp#$repstr#g;
print DEST;
}
print "Procese complete\n";
[edit]
Another excercise with files
#!/usr/bin/perl
# Make a program which takes a list of files
# named on the command line and reports
# for each one whether it's readable,
# writable, executable, or doesn't exist.
# In most shells, use a star as the
# argument to mean all of the normal
# files in the current directory.
# That is, you could type something like
# ./ex11-2 * to ask the program for the
# attributes of many files at once.
sub ClearScreen {
for ($i=1;$i<50;$i++) {
print "\n";
};
}
sub SeeAttrib { # This routine checks the attributes of a file
my $fn = $_[0]; # Captures the name of the file
my %attr = ("exists" =>"NO"); #This is the hash for the state of each atribute
(!(-e $fn) && (return %attr)) || ($attr{'exists'}="YES"); # Checks if the file exists
-r $fn && ($attr{'readable'}="YES") ; # Checks if it is readable
-w $fn && ($attr{'writable'}="YES"); # Checks if it is writable
-x $fn && ($attr{'executable'}="YES"); # Checks if it is executable
return %attr;
}
&ClearScreen;
my $r_flattrs; # Refernce to a hash for the files and its attributes
foreach (@ARGV) {
%{${$r_flattrs}{$_}} = &SeeAttrib($_) ;
}
foreach (keys %{$r_flattrs}) {
$fn = $_;
print "Information of file $fn:\n";
$r_fatribs = ${$r_flattrs}{$fn};
foreach (sort keys %{$r_fatribs}){
print " $_: ${$r_fatribs}{$_}\n";
}
}
[edit]
Another excercise: test file ages
#!/usr/bin/perl
# Make a program to identify the oldest file
# named on the command line and report its age in days.
# What does it do if the list is empty?
# (That is, if no files are mentioned on the command line.)
sub ClearScreen {
for ($i=1;$i<50;$i++) {
print "\n";
};
}
sub checkage { # This routine checks the age of a file
my $fn = $_[0]; # Captures the name of the file
!(-e $fn) && die "The file $fn doesn't exist!!\n"; # Checks if the file exists
return -M $fn; # Returns the age in days
}
&ClearScreen;
die "No files to test!!!\n" unless @ARGV;
my $oldest_name = $ARGV[0];
my $oldest_age = &checkage($oldest_name);
foreach (@ARGV) {
my $oldest_age1 = &checkage($_);
($oldest_age1 > $oldest_age) && (($oldest_age, $oldest_name) = ($oldest_age1,$_));
}
printf ("The oldest file is %s, and it is %2f days old\n",$oldest_name,$oldest_age);
