Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the basic operations of Perl directory handles

Shulou Source: shulou.com Published: 2022-06-02 07:56:52 10月02日 Update

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!

Tags: Directory file handle file name content path path name part basic operation valid more directory name article but thing thing people middle value full name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Microsoft Shulou Tech Info vpn Apple