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 basic operations of Perl directory handles

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the basic operation of Perl directory handle, which is very detailed and has a certain reference value. Friends who are interested must read it!

Perl directory handle operation (opendir,readdir,closedir)

The method of getting a list of its file names from a given directory can also use the Perl directory handle (directoryhandle). Perl directory handles look and behave much like file handles. Open (use opendir instead of open), read in (use readdir instead of readline), and close (use closedir instead of close). Instead of reading the contents of the file, read the file name (and other things) in a directory, such as the following example:

My$dir_to_process= "/ etc"; opendirDH,$dir_to_processordie "Cannotopendom dirt dirt to process readdirDH!"; foreach$file (readdirDH) {print "onefilein$dir_to_processis$file\ n";} closedirDH

Like a file handle, the Perl directory handle closes automatically at the end of the program, or when the Perl directory handle is reopened to point to another directory.

In previous versions of Perl, globbing would call other processes; Perl directory handles would not, so it would be more efficient and use system resources more efficiently. However, this is a lower-level operation, which means that we need to deal with more things ourselves.

While ($name=readdirDIR) {nextunless$name=~/\ .pm$/;... Moreprocessing... }

The above syntax is a regular expression rather than a glob. If you want all non-dot files (not files that start with a dot), you can use:

Nextif$name=~/ ^\. /

If you want all files except the usual dot (current directory) and dot (parent directory), we can explicitly use the following statement:

Nextif$nameeq "." or$nameeq "..

Now let's discuss the most confusing part, please cheer up. The file name returned by the readdir operation has no pathname part, but just the file name. So what we get is not / etc/passwd but passwd. Because this is another place that is different from globbing, people often get confused here.

You need to add the pathname to get the full name of the file (pathname + file name):

OpendirSOMEDIR,$dirnameordie "canopy dirname"; while (my$name=readdirSOMEDIR) {nextif$name=~/ ^\. /; # Skip the point file $name= "$dirname/$name"; # add the directory name nextunless-f$nameand-rname;# unless it is readable. If no directory name is added, the test section will only detect the files under the current directory, not the files under $directory. The above is all the contents of the article "what are the basic operations of Perl directory handles?" Thank you for reading! Hope to share the content to help you, more related 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