Example: File Information Class

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.

Listing 18.6. The FileInfo Module, as a Class.
 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 ...

Get SAMS Teach Yourself Perl in 24 Hours THIRD EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.