Solved

File :: modules

Posted on 2000-05-05
15
287 Views
Last Modified: 2010-03-05
What are the options/functions etc in the different File:: modules ?
0
Comment
Question by:ceenamat
  • 8
  • 5
  • 2
15 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 25 total points
ID: 2780902
File::CheckTree

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.
=================
File::Compare

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.
=================
File::Copy

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:

     source

          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.

     dest

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

     buffsize

          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"
=================
File::DosGlob

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.
=================
File::Find

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:

     $File::Find::dir

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

     $File::Find::name

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

     $File::Find::prune

          If true, find does not descend into any directories.

     $File::Find::dont_use_nlink

          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
subroutine.
=================
File::Path

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:

     path

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

     bool

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

     perm

          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

          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.

     prt

          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.

     skip

          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.
=================
File::Spec

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.

          File::Spec::Mac
          File::Spec::Os2
          File::Spec::Unix
          File::Spec::VMS
          File::Spec::Win32

NOTE: There are quite a few functions!!
=================
File::Stat

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:


Field
          Meaning
 dev
          Device number of filesystem
 ino
          Inode number
 mode
          File mode
 nlink
          Number of links to the file
 uid
          Numeric user ID of owner
 gid
          Numeric group ID of owner
 rdev
          Device identifier
 size
          Size of file, in bytes
 atime
          Last access time
 mtime
          Last modified time
 ctime
          Inode change time
 blksize
          Preferred blocksize for filesystem I/O
 blocks
          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
ways:

     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.
=================
0
 

Author Comment

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

Expert Comment

by:maneshr
ID: 2782372
sure.
take your time.

Have a nice weekend. :-)
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2792725
Any luck trying the document??

let me know.
0
 

Author Comment

by:ceenamat
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?

0
 
LVL 16

Expert Comment

by:maneshr
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 (http://www.cpan.org/authors/id/RJRAY/Image-Size-2.903.tar.gz)

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 CGI.pm 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 (http://www.ntua.gr/cgi-bin/man-cgi) for more info.
0
 

Author Comment

by:ceenamat
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 84

Expert Comment

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

          SYNOPSIS
                     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 CGI.pm
                     @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);

0
 

Author Comment

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

Author Comment

by:ceenamat
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?
0
 

Author Comment

by:ceenamat
ID: 2832183
ozo?
0
 
LVL 84

Expert Comment

by:ozo
ID: 2832392
perldoc Image::Size
                 imgsize(stream)
                      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
                      unknown.

                 Input Types

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

                 string
                      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);
0
 

Author Comment

by:ceenamat
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!
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2836996
most welcome :-)
0
 

Author Comment

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

http://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=10354779

Do post a comment there!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now