Perl tips: File Operations

Source: Internet
Author: User
Tags file separator
  • Translated by saladjonk
  • Audit: Qiang
  • Exit: Chinese Perl Association FPC (Foundation of perlchina)
  • Original Name: Perl circus: File Operation
  • Author: Luke Melia
  • Original article:
  • Table posting: 2002

Perlchina reminds you: please protect the copyright of the author and maintain the crystallization of the work of the author.

Directory

  • 1
    Find: Find a file with the specified features
  • 2
    Search: recursive search for directories
  • 3
    Read: reads the entire file at a time.
  • 4
    Assign value: assign a file handle to another file handle.
  • 5
    Write: write operations to two file handles at the same time
  • 6
    More: find its name from the full path of a file
  • 7
    Change: Change the file owner

 


Find: Find a file with the specified features
$path = "/path/to/dir/";
opendir DIR, $path;
@arr1 = readdir DIR;
@arr2 = grep{-T "$path$_"} @arr1; #text files only
@arr3 = grep{!-d "$path$_"} @arr1; #no directories
@arr4 = grep{-s "$path$_" < 1024} @arr1; #less than 1K

Code explanation: If the directory item to be tested is a text file,-T
The file operator returns the truth. In fact, there are still many test operations for directory items. (Note: Both files and directories are managed in the form of directory items in the system. Therefore, you need to distinguish a directory item from a file.
Or a directory requires corresponding operators ). Note that the preceding readdir function returns all directory items under the specified directory. Because in grep
The test of directory items in the function requires the full path of the file, so we put $ PATH (some paths of directory items are stored) and
$ _ (Stores the name of the Directory item ).


Search: recursive search for directories
use File::Find;

find(/&handleFind, 'imac:documents:code');

sub handleFind{
my $foundFile = $File::Find::name;
print "$foundFile/n" if ($foundFile =~ //.html?$/i);
}

Running result: iMac: Documents: Code: index.html iMac: Documents: Code: Perl: example. htm

Code discussion: Perl programmers who work on UNIX systems can easily use Unix
Provides tools to do a lot of daily work, such as recursively listing all directory items under a specified directory (that is, listing all directory items under a specified directory and a specified directory subdirectory ). However, Perl
One of the biggest features is that it can run on many platforms. So if you happen to work on a non-Unix platform, or
Platform, but do not like to use system tools to write scripts. You can select Perl. To complete these clever tasks, you need to use file: Find in Perl
Module. When you load this module, you can use the find
When calling this function, a sub-function must include a parameter. The first parameter is a reference to a function. This function is created by yourself. Every time a file is found, it will run. The following Parameter
A number is a string of paths you want to search. The sample script I wrote runs on the Macintosh OS 8.x system, so I used the path Separator of the MAC system.
:. If it is in Windows, you can use a backslash. If it is in UNIX, It is a forward slash (as
I don't know what to use in the system ). In short, the find function will call the sub-function you provided every time you find a file and search for the sub-directories. In my
In the handledfind sub-function, I use this module's specific variable $ file: Find: Name to obtain each find
The file name. Then, you can perform any test on the file you want. In the above example, the file name with the. html extension is output.


Read: reads the entire file at a time.
open FH, "< anthem";
$/ = undef;
$slurp = <FH>;
print $slurp;

Running result: All the file content is displayed. You should be very proud of it now. :) Code discussion: Angle brackets <>
Operate the file handle. In the scalar context, it will return the next record of the file, and in the array context it will return all records. By default, records in files are considered to be separated by line breaks.
(For example, press ENTER or another character that represents the start of a new line ). You can reset the default delimiter, and Perl will replace the line break with the delimiter you specified. Global variable $/
The delimiter of the input file is stored. If you set $/to UNDEF, then Perl
The entire file will be considered as a record (because there is no file separator at the moment ). Remember $/
It is a global variable. Never change it elsewhere in the script. This error will be hard to find. You may ask, can we keep changing?
$/, And read all records of the file into an array, and then combine the array into a long string (such as $ slurp =
Join ("", <FH>. Of course, this is also an effective solution, but you will find that it is slow, whether to choose it depends on your application, depending on
Whether you care about the running speed.


Assign value: assign a file handle to another file handle.
open(MYOUT, "> bottle.txt");
*STDOUT = *MYOUT;
print "message";

Running result: the text file bottle.txt contains the message string. Code discussion: You may have used print together before
Functions and file handles, but do you know that even if you do not use a file handle, Perl uses a handle called stdout by default? C programmers know about stdout.
Standard output, that is, the usual screen, or terminal window (or the output end of the CGI program-
Browser ). The job we have done here is to create our own file handle, which points to a given file, and then we do a little bit of work, using the * prefix to put stdout
Convert to the typeglob type. Data of the typeglob type can have aliases, so that a variable may point to a variable of another name. The second line of code above makes
Stdout points to the myout variable. Therefore, the default output object during the print operation becomes the file handle we created.


Write: write operations to two file handles at the same time
use IO::Tee; 
$tee = IO::Tee->new(">> debuglog.txt", /*STDOUT);
print $tee "an error ocurred on ".scalar(localtime)."/n";

Running result: An error ocurred on Fri Feb 23 21:44:20 2001
Code discussion: If you want to write the same string to both locations for various reasons, this is the same as the Unix tee tool. Even if you are not working on UNIX
On the platform, Perl also provides this function through the tee module. The Tee module can be downloaded from CPAN. You should install it to Perl's Io
Library folder. The Tee module is written in OOP mode, so before using it, you should first use its new method to create a tee
The entire process requires two parameters. Each parameter can be a string representing the file handle or a reference to the opened file handle. In the above example, we use a string to represent
File handles opened in append mode. It points to the file named debuglog.txt, and another parameter is the file handle built in the system.
Stdout. The entire handle is automatically created by the system. The print function operates it by default. To get a file handle reference, we need
Type data uses a backslash. Typeglob can represent any named variable, whether it is an array, a hash, or a scalar. Use *
It is necessary because the file handle does not have a prefix. The new operator returns an Instance Object of the tee class, and then assigns the entire instance to $ tee.
Scalar. Now, no matter when we write to $ tee, we write to both locations at the same time.


More: find its name from the full path of a file
use File::Basename;
$path = "/docs/sitecircus.com/html/tricks/trick.of.the.week.html";
$basename = basename($path, ".html");
print $basename;

Running result: Trick. of. The. Week
Code discussion: Well, it's successful. The problem is to find out the file name without any path prefix or any extension. File: basename
The module can make this easy to implement. We only need to pass the full path of the file and the extension to be removed. The above PATH variable is the full path of the file. Note that the file separator is
/. This character is special because it is a reserved character of the operating system. Here you cannot use the system Separator in the file name. You should know that popular operating systems all use their own unique file Separation
Character: Unix/, Windows/, and Macintosh: (by the way, Perl on Windows
In the script, you can use/or use/as the file separator, Perl
). File: basename. Of course, the file name can be correctly found in the full path, regardless of the system in which the file is located.


Change: Change the file owner
($uid, $gid) = (getpwnam($username))[2,3]
or die "$user not in passwd file";
chown ($uid, $gid, $file)
or warn "couldn't chown $file.";

Running result: No output code discussion: Sometimes you may know a user name, and you want to use this user name to do something, such as changing the owner of a file. But unfortunately, the perl
The CHOWN command does not accept the user name as a parameter, but can accept a pair of numbers: userid and groupid. Despite these inconveniences, Perl
We are not in trouble. We can take the user name as the parameter of the getpwnam function and obtain an array containing the userid and
The groupid corresponds to the second and third elements in the number group.

Author: Luke Melia

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.