Example: A Module to Handle Common File Information Requests

Previous Table of Contents Next

Example: A Module to Handle Common File Information Requests

For this example, you're going to create a module to present information about a file. All of the information presented by this module is readily available in Perl through a variety of interfaces. In this module you're going to create functions for retrieving this information with convenient function names.

The module is presented in Listing 17.7.

Listing 17.7. File Information Module

1:  #!/usr/bin/perl -w


3:  package TYPFileInfo;

4:  use strict;


6:  use Exporter;

7:  our @ISA=qw(Exporter);

8:  our @EXPORT=qw(bytes lines name extension modified $FileInfoName);


10: our $FileInfoName = "";


12: sub bytes {    # Size of the file in bytes

13:     return -s $FileInfoName;

14: }

15: sub lines {    # Number of text lines in the file

16:     my $count = 0;

17:     open(FH, $FileInfoName) || die "Can't open $FileInfoName: $!";

18:     while(<FH>) {

19:         $count++;

20:    }

21:    close(FH);

22:    return $count;

23: }

24: sub name {    # Filename portion of the path

25:     if ($FileInfoName =~ m/([\w\.]+)$/) {

26:         return $1;

27:     }

28:     return $FileInfoName;

29: }

30: sub extension {# Extension portion of the filename

31:     if ($FileInfoName =~ m/\.(.*?)$/) {

32:         return $1;

33:     }

34:     return "";

35: }

36: sub modified {    # Last modified time, suitable for localtime()

37:     my @stats = stat($FileInfoName);

38:     return $stats[9];  # Modified time.

39: }


41: 1;

Lines 3Ц8: This is the normal preamble for establishing a module. Five subroutine names will be imported into the calling program. In addition, one variable name will be imported into the caller: $FileInfoName.

Line 10: The variable $FileInfoName is cleared. All of the functions will use this for the filename to operate on.

Line 13: The Цs operator is used to find the size of the file and return it.

Lines 16Ц22: The entire file is read, and then the number of lines read is returned.

Lines 25Ц28: The regular expression matches word-characters or periods at the end of the filename and returns that portion. This is an overly simplified way of matching filenames and will not work for files containing punctuation or spaces. If no match is found, the entire filename is returned.

Lines 31Ц34: The regular expression matches word-characters following a period at the end of the file name. Again, this is overly simplified and will not match odd characters in extensions. If no match is found, the empty string is returned.

Lines 37Ц38: The stat function (see Hour 10, "Files and Directories") is used to retrieve information on the file. The ninth value in the returned list is the last modified time, and that value is returned.

Line 41: This is the normal "return true" line that modules require.

The program in Listing 17.8 will use the module from Listing 17.7.

Listing 17.8. Sample Usage of the File Information Module

1:  #!/usr/bin/perl -w


3:  use strict;

4:  use TYPFileInfo;


6:  $FileInfoName = "/temp/message.txt";


8:  print "\nFilename: "  . name();

9:  print "\nExtension: " . extension();

10: print "\nModified: " . localtime(modified());

11: print "\nBytes: " . bytes();

12: print "\nLines: " . lines();

Line 4: This line reads the module code, importing all of the default names (five subroutine names and one variable name).

Line 6: This sets the name of the file that the functions will work on. The name $FileInfoName has been imported by the module, so it's now equivalent to $TYPFileInfo::FileInfoName.

Lines 8Ц12: These simply call the functions in the TYPFileInfo module and display their values.

For this example, I set $FileInfoName to the name of a file that contained a mail message. The results looked something like this:

Previous Table of Contents Next
© 2000- NIV