up
Perl Lecture Notes - November 23 (Loose Ends)
- Questions
- Comparison of Objects in Java and Perl
- Things to notice:
- class data : "static" in java VS global to package file in perl
- class method : "static" in java VS how args are handled in perl
- instance data : obj.data in java VS typically within $obj->{} in perl
- instance methods : obj.meth() in java VS $obj->meth() in perl
- Java:
- Perl:
- Files and Directories
- Filehandles
- filewrite.pl example,
- read the "open" description in the camel, pg 747
- also see "sysopen" function for a more C-ish interface, pg 808 in camel
- fileread.pl example
- Filetests - filetest.pl example
- File and directory functions
- chdir $dirname; # change working directory
- my @list = glob "*.jpg" ; # get a list of some files in a directory
(Note: NOT regular expression syntax)
- unlink $file; # delete (rm) a file
- rename $oldname, $newname; # rename a file
- mkdir , rmdir, chmod, chown - just like unix command line
- Absolute vs relative names on different platforms : File::Basename
use File::Basename; my $dirname = dirname("/usr/bin/perl");
- Also see File::Copy, File::Find, File::Path, File::Compare
(all standard in Perl. Look in directory given by "perldoc -l File::Basename")
- Also see many File:: modules on CPAN
- Talking to the Operating System / Processes Managemnet
- system("command") function - spawn a child process and run it.
- my $output = `ls`; # backquotes mean "run that, get the output, and put it here."
- exec("command") - leave perl and go do this instead.
- $ENV{} hash - process environment variables
- proctest.pl example
- filehandle as forked process :
open PRINT "|lpr" or die "cannot pipe to lpr printer: $!";
- threads (java) vs forking (perl)
- fork.pl example
- Trapping Errors
- Java: Throw, Catch - exception handling mechanism
- Perl: eval { try }; if ($@) { catch }
- eval.pl example
- Assignment
(This is your last perl assignment of the first trimester.
You should have everything in by the next class - if in doubt,
please email or talk to me. I've left this assignment light
to allow folks time to catch up who need to.)
- Reading
- Files and directories: read 11, 12, 13 in Learning Perl. (They're short.)
- Processes: browse chapter 14 in Learning Perl
- Trapping errors: Read the very first part of chapter 17 in Learning Perl.
- For more detail on any of these topics, browse the corresponding parts of the camel.
- Browse any parts of the llama you haven't seen yet - this is
our last assignment in this book.
- Coding
- Write a perl program that finds the total size of
all the files in a given directory. If no directory is given,
it should report on the current directory.
If the given directory doesn't exist, it should report
that gracefully and prompt the user for another choice.
- Optional - report seperate size totals for different types
of files. How you break out the types is up to you, but
perhaps executable vs text would be reasonable.
- Optional - include the sizes of all directories below
the given directory as well. Don't follow symbolic links.
(Since the directory structure is recursive, the simplest
way to do this is with a recursive subroutine...)
So there you are.