Perl Study Notes No. 12

Source: Internet
Author: User
Tags glob

1. chdir change the working directory... chdir "/etc" or die "cannot chdir to/etc: $! "; If the parameter is null, change it to the main directory.

2. My @ all_files = glob "*"; my @ bz_file = glob "*. bz2"; only

3. My @ all_files = <*>; my $ dir = "/etc"; my @ dir_files = <$ DIR/* $ DIR/. *>;

4. If there is a strict (simple) identifier between the angle brackets, it is the file handle reading operation; otherwise, it is the globbing operation.

5. If the directory name is not added, the test part will only detect the files in the current directory, rather than the files under $ dirname. This is the most common use of directory handles.
Errors

6. Use unlink with caution when deleting files...

7. Rename, rename "old", "new ";

8.Foreach my $ file (glob "*. Old") {<br/> my $ newfile = $ file; <br/> $ newfile = ~ S //. old $ /. new/; <br/> If (-e $ newfile) {<br/> warn "can't rename $ file to $ newfile: $ newfile exists/N "; <br/>} elsif (rename $ file, $ newfile) {<br/>## success, do nothing <br/>} else {<br/> warn "RENAME $ file to $ newfile failed: $! /N "; <br/>}< br/>}
Batch rename

9. In rare cases, you can use the utime function to trick other programs into the latest file modification time and access time.

The first two parameters provide the recent access time and modification time. The remaining parameters are the list of files whose values need to be changed.

My $ now = time;

My $ ago = $ now-24*60*60;

Utime $ now, $ ago, glob "*";

Update all files in the current directory so that they seem to have been modified yesterday, and the access time is now.

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.