File Tips & Trick in Perl

Discussion in 'Perl' started by pradeep, Mar 31, 2008.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Here are a few tips & tricks which you may find useful when dealing with files/directories in Perl.

    Perl Test Operators



    Code:
     +-------------+------------------------------------+
     | Operator    | Function                           |
     +-------------+------------------------------------+
     |-e           | Tests if OPERAND exists.           |
     +-------------+------------------------------------+
     |-f           | Tests if OPERAND is a regular file |
     |             | as opposed to a directory,         |
     |             | symbolic link or other type of file|
     +-------------+------------------------------------+
     |-l           | Tests if OPERAND is a symbolic link|
     +-------------+------------------------------------+
     |-s           | Returns size of OPERAND in bytes   |
     +-------------+------------------------------------+
     |-d           | Tests if OPERAND is a directory.   |
     +-------------+------------------------------------+
     |-T           | Tests if OPERAND is a text file.   |
     +-------------+------------------------------------+
     
    Example:
    Code:
     #!/usr/bin/perl
     
     if(-e "/tmp/test.pl")
     {
         print "File/directory exists.";
     }
     
     if(-f "/tmp/test.pl")
     {
         print "File exists";
     }
     
     $size = -s "/tmp/test.pl";
     print "Size of /tmp/test.pl is $size bytes";
     

    Filtering Files With Some Characteristics



    We can use a combination of grep and perl test operators to filter some files out, to match some criteria of ours, like getting only text files.

    Code:
     #!/usr/bin/perl
     
     $path = "/tmp/test/";
     opendir(DIR, $path);
     
     @arr1 = readdir DIR;
     @arr2 = grep{-T "$path$_"} @arr1; #only text files only
     @arr3 = grep{!-d "$path$_"} @arr1; #no directories
     @arr4 = grep{-s "$path$_" < 2048} @arr1; #less than 2KB
     @arr5 = grep{/^a/} @arr1; #filenames starting with 'a'
     

    Finding the basename of a file from its full path-name



    Code:
     #!/usr/bin/perl
     
     use File::Basename;
     
     $path = "/home/htdocs/go4expert.com/cgi-bin/index.cgi";
     $basename = basename($path, ".cgi");
     
     print $basename;
     
    Output:
    Code:
     index
     
    To find just the name of the file, without its preceding path, and minus the "dot extension" using File::Basename module is pretty easy. We just pass it the whole path, and a string that specifies what extension we want removed and presto we're done.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice