File :: modules

Posted on 2000-05-05
Last Modified: 2010-03-05
What are the options/functions etc in the different File:: modules ?
Question by:ceenamat
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 2
LVL 16

Accepted Solution

maneshr earned 25 total points
ID: 2780902

Runs file tests on a set of files. Exports one function, validate, which takes a single multi-line string as input. Each line of the
string contains a filename plus a test to run on the file. The test can be followed with || die to make it a fatal error if it fails.
The default is || warn. Prepending ! to the test reverses the sense of the test. You can group tests (e.g., -rwx); only the first
failed test of the group produces a warning. For example:

     use File::CheckTree;
     $warnings += validate( q{
         /vmunix            -e || die
         /bin               cd
             csh            !-ug
             sh             -ex
         /usr               -d || warn "What happened to $file?\n"

Available tests include all the standard Perl file-test operators except -t, -M, -A, and -C. Unless it dies, validate returns the
number of warnings issued.

Compares the contents of two sources, each of which can be a file or a filehandle. Returns 0 if the sources are equal, 1 if they
are unequal, and -1 on error. File::Compare provides two functions:

* compare (file1, file2[, buffsize])

Compares file1 to file2. Exported by default. If present, buffsize specifies the size of the buffer to use for the comparison.

* cmp (file1, file2[, buffsize])

cmp is a synonym for compare. Exported on request.

Copies or moves files or filehandles from one location to another. Returns 1 on success, 0 on failure, or sets $! on error.

* copy (source, dest[, buffsize])

Copies source to dest. Takes the following arguments:


          The source string, FileHandle reference, or FileHandle glob. If source is a filehandle, it is read from; if it's a
          filename, the filehandle is opened for reading.


          The destination string, FileHandle reference, or FileHandle glob. dest is created if necessary and written to.


          Specifies the size of the buffer to be used for copying. Optional.

* cp (source, dest[, buffsize])

Like copy, but exported only on request.

     use File::Copy "cp"

* move (source, dest)

Moves source to dest. If the destination exists and is a directory, and the source is not a directory, then the source file is
renamed into the directory specified by dest. Return values are the same as for copy.

* mv (source, dest)

Like move, but exported only on request.

     use File::Copy "mv"

Provides a portable enhanced DOS-like globbing for the standard Perl distribution. DosGlob lets you use wildcards in
directory paths, is case-insensitive, and accepts both backslashes and forward slashes (although you may have to double the
backslashes). Can be run three ways:

     From a Perl script:

          require 5.004
          use File::DosGlob 'glob';

          @perlfiles = glob  "..\pe?l/*.p?";
          print <..\pe?l/*.p?>;

     With the perl command, on the command line:

          # from the command line (overrides only in main::)
          % perl -MFile::DosGlob=glob -e "print <../pe*/*p?>"

     With the perlglob.bat program on the DOS command line:

          % perlglob ../pe*/*p?

When invoked as a program from the command line, File::DosGlob prints null-separated filenames to STDOUT.

Looks for files that match a particular expression. Exports two functions:

* find (\&wanted, dir1[, dir2 ...])

Works like the Unix find command; traverses the specified directories, looking for files that match the expressions or actions
you specify in a subroutine called wanted, which you must define. For example, to print out the names of all executable files,
you could define wanted this way:

     sub wanted {
         print "$File::Find::name\n" if -x;

Provides the following variables:


          Current directory name ($_ has the current filename in that directory).


          Contains "$File::Find::dir/$_". You are chdired to $File::Find::dir when find is called.


          If true, find does not descend into any directories.


          Set this variable if you're using the Andrew File System (AFS).

* finddepth (\wanted, dir1[, dir2...])

Like find, but does a depth-first search.

The standard Perl distribution comes with a Perl script, find2perl, which takes a Unix find command and turns it into a wanted

Creates and deletes multiple directories with specified permissions. Exports two methods:

* mkpath (path, bool, perm)

Creates a directory path and returns a list of all directories created. Takes the following arguments:


          Name of the path or reference to a list of paths to create.


          Boolean. If true, mkpath prints the name of each directory as it is created. Default is false.


          Numeric mode indicating the permissions to use when creating the directories. Default is 0777.

* rmtree (root, prt, skip)

Deletes subtrees from the directory structure, returning the number of files successfully deleted. Symbolic links are treated as
ordinary files. Takes the following arguments:


          Root of the subtree to delete or reference to a list of roots. The roots, and all files and directories below each
          root, are deleted.


          Boolean. If true, rmtree prints a message for each file, with the name of the file and whether it's using rmdir or
          unlink to remove it (or if it's skipping the file). Default is false.


          Boolean. If true, rmtree skips any files to which you do not have delete access (under VMS) or write access
          (under other operating systems). Default is false.

Performs common operations on file specifications in a portable way. To do that, it automatically loads the appropriate
operating-system-specific module, which is one of File::Spec::Mac, File::Spec::OS2, File::Spec::Unix, File::Spec::VMS, or
File::Spec::Win32. The complete reference of available functions is given in File::Spec::Unix; the functions are inherited by the
other modules and overridden as necessary. Subroutines should be called as class methods, rather than directly.


NOTE: There are quite a few functions!!

Provides the same file status information as the Perl functions stat and lstat. Exports two functions that return File::stat
objects. The objects have methods that return the equivalent fields from the Unix stat(2) call:

          Device number of filesystem
          Inode number
          File mode
          Number of links to the file
          Numeric user ID of owner
          Numeric group ID of owner
          Device identifier
          Size of file, in bytes
          Last access time
          Last modified time
          Inode change time
          Preferred blocksize for filesystem I/O
          Number of blocks allocated

You can access the status fields either with the methods or by importing the fields into your namespace with the :FIELDS
import tag and then accessing them by prepending st_ to the field name (e.g., $st_mode). Here are examples of doing it both

     use File::stat;

     $stats = stat($file);
     print $stats->uid;
     print $st_uid;

* stat (file)

Returns status information for the file or filehandle pointed to by file. If file is a symbolic link, returns the information for the file
that the link points to.

* lstat (file)

Returns the same information as stat, but if file is a symbolic link, returns the status information for the link.

Author Comment

ID: 2781617
Wwowwwwwww !!!
Thanks, maneshr. But do give me some time to go thro' this. I'll get back after this weekend.
LVL 16

Expert Comment

ID: 2782372
take your time.

Have a nice weekend. :-)
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 16

Expert Comment

ID: 2792725
Any luck trying the document??

let me know.

Author Comment

ID: 2796020
Yes, but is there any way in which the properties of a file, like its type (exe, jpeg etc) can be obtained ? Or perhaps the width & height values of an image file? More, the header info?

LVL 16

Expert Comment

ID: 2797344
i dont think you can do that using any of the File::* modules.

Also i dont think there is one big module which can get/read heder  info for all possible file types..

"....perhaps the width & height values of an image file? ..."

For images you can use the Image::Size module (

Image::Size is a library based on the image-sizing code in the wwwimagesize
script, a tool that analyzes HTML files and adds HEIGHT and WIDTH tags to
IMG directives. Image::Size has generalized that code to return a raw (X, Y)
pair, and included wrappers to pre-format that output into either HTML or
a set of attribute pairs suitable for the library by Lincoln Stein.
Currently, Image::Size can size images in XPM, XBM, GIF, JPEG, PNG, TIFF
and the PPM family of formats (PPM/PGM/PBM).

Also if you are on UNI* systems you can use the file command to help you determine the file type.

The file  utility performs a series of tests  on  each  file
     supplied  by  file  and,  optionally, on each file listed in
     ffile in an attempt to classify it. If the  file  is  not  a
     regular  file,  its file type is identified.  The file types
     directory, FIFO, block special, and  character  special  are
     identified  as  such.  If the file is a regular file and the
     file is zero-length, it is identified as an empty file.

check the man pages at ( for more info.

Author Comment

ID: 2798775
> adds HEIGHT and WIDTH tags to IMG directives

But that's not what I want actually. I have an image file uploaded to my server. I want to find its actual width & height. I guess it must be available in the header somewhere.
LVL 84

Expert Comment

ID: 2799048
                 Image::Size  -  read the dimensions of an image in several
                 popular formats

                     use Image::Size;
                     # Get the size of globe.gif
                     ($globe_x, $globe_y) = imgsize("globe.gif");
                     # Assume X=60 and Y=40 for remaining examples

                     use Image::Size 'html_imgsize';
                     # Get the size as "HEIGHT=X WIDTH=Y" for HTML generation
                     $size = html_imgsize("globe.gif");
                     # $size == "HEIGHT=40 WIDTH=60"

                     use Image::Size 'attr_imgsize';
                     # Get the size as a list passable to routines in
                     @attrs = attr_imgsize("globe.gif");
                     # @attrs == ('-HEIGHT', 40, '-WIDTH', 60)

                     use Image::Size;
                     # Get the size of an in-memory buffer
                     ($buf_x, $buf_y) = imgsize($buf);


Author Comment

ID: 2809417
Bit busy at the moment, will get back soon!

Author Comment

ID: 2824314
imgsize gets the size of the image residing in the server, does it? Yes, that's what I wanted.

But I didn't get the last example:

use Image::Size;
# Get the size of an in-memory buffer
($buf_x, $buf_y) = imgsize($buf);

What does it do?

Author Comment

ID: 2832183
LVL 84

Expert Comment

ID: 2832392
perldoc Image::Size
                      Returns  a  three-item list of the X and Y dimensions
                      (width and height, in that order) and image  type  of
                      stream.  Errors are noted by undefined (undef) values
                      for the first two elements, and an  error  string  in
                      the third.  The third element can be (and usually is)
                      ignored, but is useful when sizing data whose type is

                 Input Types

                 The  sort  of  data  passed  as stream can be one of three

                      If an ordinary  scalar  (string)  is  passed,  it  is
                      assumed  to  be  a  file  name  (either  absolute  or
                      relative to the  current  working  directory  of  the
                      process) and is searched for and opened (if found) as
                      the source of data.   Possible  error  messages  (see
                      DIAGNOSTICS  below) may include file-access problems.

                 scalar reference
                      If the passed-in stream is a scalar reference, it  is
                      interpreted   as  pointing  to  an  in-memory  buffer
                      containing the image data.
                              # Assume that &read_data gets data somewhere (WWW, etc.)
                              $img = &read_data;
                              ($x, $y, $id) = imgsize(\$img);
                              # $x and $y are dimensions, $id is the type of the image

                 Open file handle
                      The third option is to pass  in  an  open  filehandle
                      (such  as  an  object  of  the  IO::File  class,  for
                      example) that has already been  associated  with  the
                      target  image file. The file pointer will necessarily
                      move, but will be restored to its  original  position
                      before subroutine end.
                              # $fh was passed in, is IO::File reference:
                              ($x, $y, $id) = imgsize($fh);
                              # Same as calling with filename, but more abstract.

#So, imgsize("globe.gif") would be approximately equivalent to:
open FILE,"<globe.gif" or return (undef,undef,"Can't open image file globe.gif: $!");
binmode FILE;
$buf = \join'',<FILE>;
close FILE;
return imgsize($buf);

Author Comment

ID: 2836199
Thanks, ozo. I am posting a question for you in this area, with the same amount of points. I am accepting maneshr's comment cos my original question was answered by him.
Thanks maneshr!
LVL 16

Expert Comment

ID: 2836996
most welcome :-)

Author Comment

ID: 2857691
ozo, u didn't claim ur points yet.
It's at: 

Do post a comment there!

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl program to obtain a machine's memory usage 6 26
Strange perl issue 6 132
pattern matching in perl 2 111
remove duplicates from the csv file 13 118
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question