Perl Learning notes-target operations

Source: Internet
Author: User
Tags glob



1. Move through the directory tree.



When the program runs with the current working directory as the starting point of the relative path, you can use the chdir operator to change the current directory:


chdir "/ etc" or die "Ca n‘t chdir to / etc: $!"; #Because this is a call relative to the operating system, the error message will be returned to $ !.


All processes initiated by the Perl program inherit the working directory of the Perl program. But for the process that started Perl, its working directory does not change with the Perl working directory, so no Perl program can be written in place of the CD command in the shell. Because once you exit the Perl program, you will return to the working directory you started.



If you omit the argument, it goes back to the user's home directory, which is one of the few situations where $_ is not the default parameter.



2. File wildcard name (globbing)



Command line: The shell expands the file name pattern in the command line into all matching file names, called file name wildcard, such as  echo *.txt   The name of Echo does not know how to expand *txt because the shell will expand it and also apply in Perl.



Perl Program: Applicable  glob   operator file name wildcard .   my @ All_files = glob " * "; #所有文件    my @pm_files = glob " *.pm ";  



Glob does not match a file name that begins with a . number, any mode that can be typed on the command line can be passed as a parameter to Glob processing, and if you want to match multiple patterns at once, you can separate the modes with spaces in the parameters: my @all_ Files_including_dot=glob ". * *";



Before the glob operator: Use the angle brackets syntax my @all_files=<*>; Similar to the case of a double-quote string interpolation, the variable inside the angle brackets is replaced with the value of the current variable and then expanded into the corresponding list of file names by the file name wildcard.


 
 
my $dir="/etc";
my @dir_files=<$dir/* $dir/.*>;


Lenovo to <STDIN> is read from the file handle, then how does Perl differentiate between a file name wildcard or a document handle? The + rule: If a Perl identifier condition is satisfied within the angle brackets, it is read as a file handle, otherwise the wildcard operation is used.


 
my @files = <FRED / *>; ## glob
my @lines = <FRED>; ## from file handle
my $ name = "FRED";
my @files = <$ name / *> ## glob
my @ files2 = <$ name> ## $ name is a simple scalar variable, not an array or hash, so it is also a read of the file handle.Perl is determined at compile time, so it has nothing to do with the content of the variable


3. Directory Handle



Similar to file handles, except that operators and content are different



Use opendir instead of file handle operator open



Use Readdir instead of the file handle operator ReadLine



Use Closedir instead of the file handle operator close



Read the file name and subdirectory name in the directory, not the contents of the document.


 
my $dir_to_process = ".";
opendir DH, $dir_to_process or die "Can‘t! $!\n";
foreach $file(readdir DH){
    print "$file\n";
}
closedir DH;


As with the file handle, the directory handle closes automatically at the end of the program, and it is automatically closed before another directory is opened with the handle.



4. File operation



Delete: Use the unlink operator in Perl to put the file into the shredder, similar to the RM command in Linux, the parameter is a list, returns the number of files successfully deleted, can be used in conjunction with the wildcard glob, to achieve the deletion of multiple files at once. Information is saved in variable $! when execution fails



Rename: rename function, similar to MV command, parameter is a list. Execution fails to return false, information is stored in $!, usually using or die (or warn) to report



5. Create and delete directories (only one at a time)



To create a directory mkdir , you can specify permissions when creating a directory, and if the permission is a string, use the OTC function to handle


 
my $name = "roger";
my $perssions = "0755";
mkdir $name , oct($perssions) or warn "Can‘t make directory $name : $!";


Deleting a directory rmdir , deleting a non-empty directory will fail. You can use unlink to delete the directory inside the directory first. Creating a temporary directory is best to include the current process identifier, which is placed in the $$ variable, which prevents conflicts with other processes.



To delete a directory tree: Using the module File::P Ath


 
use File :: Path qw (make_path remove_tree);

# Create a directory tree of any depth
# make_path ($ dir1, dir2, ...)
make_path (‘roger / bar / zoo’, ‘rog / dsd’);
make_path ("roger / bar / zar", "roger / dst / r", {#The actual process is created layer by layer
     verbose => 1,
     error => \ my $ roger, #Why use a backslash
     mode => 0711,
     });

# Delete the directory tree of any depth.If the directory is not empty, the file will be unlinked first.
remove_tree (‘roger / bar / zar’, ‘roger / dst / r’, {
     verbose => 1,
     error => \ my $ err_list,
     });


Modify file or directory permissions: chmod



Change affiliation: chown



Modify Timestamp: utime : Can modify the last access time of a file to fake






Perl Learning notes-target operations


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.