Perl Syntax-Advanced features

Source: Internet
Author: User
Tags scalar



Summarize:



Q (), QQ (), QW (), QX (), respectively, are single quotes, double quotes, create string lists, and capture command output.





9th hours other functions and operators


One thing can be done in a variety of ways.



The main contents of this section:


    1. How do I perform a simple string search on a scalar? (Before using regular expressions)
    2. How do I replace a character?
    3. How to use the print function
    4. How to use arrays as stacks and queues




10th UI files and directories
    • Get directory Listing
    • Creating and deleting files
    • Creating and deleting Catalogs
    • Get file information


How do I get a list of directories? (Open Directory read directory) the limitation of this step is that you can only view the file directory list information under the specified directory .



Open the file and open the directory is not the same, open the directory is to obtain a list of information in the directory, and can not be written or modified.

# open Directory
opendir (DIR, ‘./‘) or die $ !;
# Read the directory in scalar form
while (readdir (DIR)) {
    print $ _. "\ n";
}
# Read the directory as an array
@dir = readdir (DIR);
foreach (@dir) {
    print $ _. "\ n";
}
# The directory handle must be closed after it is used up
close (DIR);
# Usually use grep to filter out (.) And (..) directories
@files = grep (! / ^ \. \.? $ /, readdir (DIR));
 

How to browse directory, switch directory, create directory, delete directory?

# Use the Cwd package to get the current working directory
use Cwd;
print cwd, "\ n";
chdir ‘../‘ or warn $ !; # change the current directory
print getcwd;
print "DIrectory to create?";
my $ newdir = <STDIN>;
chomp $ newdir;
mkdir ($ newdir, 0755) or die $ !; # Create a directory with permission 755
rmdir ($ newdir) or die $ !; #Delete directory
How to delete files? Rename the file?

unlink <*. bat>; #Delete matching files
$ erased = unlink ‘old.exe’, ‘a.out’, ‘personal.txt’; #Delete the list file and return the deleted quantity
unlink @badfiles; #Delete files in the list
unlink; #Delete files in $ _
if (! rename "myfile.txt", "archive.txt") {
    warn $ !;
}
 

11th hour Interoperability between systems
system () function

Capture output

 

 

 

 

 

 

The 12th class hour uses the command line tool of Perl
 

 

 

 

 

 

 

13th class hours Citation and Structure
 

 

 

 

 

 

14th hour using modules
 

 

 

 

 

 

15th class hour Understand the running performance of the program
 

 

 

 

 

The 16th class hour
 

 

 

 

 

 

 

other:

use Cwd ‘abs_path’; #Get the absolute address of the specified file and directory
use warnings;
$ myfile = "fastq_R1.txt";
$ mydir = "./ 1";
print (abs_path ($ myfile). "\ n"); #Use abs_path function
print (abs_path ($ 0). "\ n");
print (abs_path ($ mydir). "\ n");
Following is the syntax to open file.txt in read-only mode. Here is less than <signe indicates that the file must be run in read-only mode to end

open (DATA, "<file.txt");
 
 
 
 
doubt:
1. The list connected by qq () seems to have only one element, because only one element is found when connected by scalar () and join () ? ?
 
Perl syntax-advanced features

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.