Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the operating skills of Perl files?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "Perl file operation skills are what", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what are the Perl file operation skills" this article.

Operation skills of Perl files

Perl file operation 1: find the file with the specified characteristics

$path= "/ path/to/dir/"; opendirDIR,$path; @ arr1=readdirDIR; @ arr2=grep {- T "$path$_"} @ arr1;#textfilesonly @ arr3=grep {!-d "$path$_"} @ arr1;#noDirectories @ arr4=grep {- s "$path$_" new ("> > debuglog.txt",\ * STDOUT); print$tee "anerrorocurredon" .scalar (localtime). "\ n"

Run result: anerrorocurredonFriFeb2321:44:202001 Code discussion: if, for a variety of reasons, you want to write the same string to two locations at the same time, this is the same as the tee tool under UNIX. Even if you are not working on the Unix platform, Perl provides this feature for you through the Tee module. The Tee module can be downloaded from CPAN, and you should install it into the IO library folder of Perl. The Tee module is written in OOP, so you should first use its new method to create a Tee object before using it. The whole process requires two parameters, each of which can be either a string representing a file handle or a reference to an open file handle. In the above example, we use a string to represent a file handle opened in additional mode, which points to a file named debuglog.txt, and another parameter is the system's built-in file handle STDOUT, the entire handle is automatically created by the system, and the print function operates on it by default. To get a reference to a file handle, we need to use a backslash on data of type typeglob. Typeglob can represent any named variable, whether it is an array, a hash, a scalar, etc. Using * is necessary because the file handle does not have a prefix symbol of its own. The new operator returns an instance object of the Tee class, and then we assign the entire instance to the $tee scalar. Now whenever we write to $tee, we write to both locations at the same time.

Perl file operation 6: more file operations-find out the name of a file from its full path

UseFile::Basename; $path= "/ docs/sitecircus.com/html/tricks/trick.of.the.week.html"; $basenamebasename=basename ($path, ".html"); print$basename

Run result: trick.of.the.week code discussion: all right, it worked. The problem is to find out the name of the file without any path prefix or extension. The File::Basename module makes this easy to do, just pass it the full path of the file and the extension to be deleted. The path variable above is the full path to the file. Note that the file delimiter is /, which is special because it is a reserved character of the operating system. You cannot use the system delimiter in the file name here. You should know that today's popular operating systems use their own unique file separator: Unix uses /, Windows uses\, Macintosh uses: (by the way, in Perl scripts on Windows, you can use either\ or as a file separator, Perl's interpreter can understand what you mean). File::Basename, of course, can correctly find the file name in the full path, no matter what system it is on.

Perl file operation 7: change the owner of the file

($uid,$gid) = (getpwnam ($username)) [2p3] ordie "$usernotinpasswdfile"; chown ($uid,$gid,$file) orwarn "couldn'tchown$file."

Run result: no output code discussion: sometimes, you may know a user name, and you want to do something with that user name, such as changing the owner of a file. Unfortunately, Perl's chown command cannot take a user name as an argument, but it can accept a pair of numbers: userid and groupid. Despite these inconveniences, Perl doesn't get us into trouble. We can take the user name as an argument to the getpwnam function and get an array containing the userid and groupid of the user name, corresponding to the second and third elements of the array, respectively.

The above is all the contents of the article "what are the skills for operating Perl files?" Thank you for your reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report