June 2005
Beginner
480 pages
10h 31m
English
If you recall from last hour, you built a module to give information on files. After building the module, it had a series of defects. Each of those defects can easily be fixed by using a class instead of a regular module.
The object-oriented version of the file information module is presented in Listing 18.6.
1: #!/usr/bin/perl -w 2: 3: package TYPFileInfoOO; 4: use strict; 5: 6: sub new { 7: my($class,$filename)=@_; 8: if (! -e $filename) { 9: die "$filename doesn't exist!"; 10: } 11: bless { filename => $filename }; 12: } 13: sub bytes { # Returns the size of the file in bytes 14: my($self)=@_; 15: return -s $self->{filename}; 16: } 17: sub lines { # Returns the ... |
Read now
Unlock full access