Perl: file operations, my, time conversion, log monitoring

Source: Internet
Author: User
Tags epoch seconds

① File Operations

To read a string from a file using Perl, you can use either of the following methods:
1. Read all the content in the file into an array at a time (this method is suitable for small files ):

  Open  (File  ,  "  Filename  "  )  |  Die  "  Can not open the file: $! "  ;
@ Filelist = < File > ;

Foreach $ Eachline ( @ Filelist ){
Chomp $ Eachline ;
}
Close File;

When the file is large, the "out of memory" error may occur. You can use the following method to read a row at a time.

2. Read and process one row from a file and one row at a time (it is more convenient to read large files ):

  Open  (File  ,  "  Filename  "  )  |  Die "  Can not open the file: $!  "  ;

While ( Defined ( $ Eachline = < File > )){
Chomp $ Eachline ;
# Do what u want here!
}
Close File;

② Differences between our, my, and local

Http://www.cppblog.com/converse/archive/2008/07/31/57636.html

Http://topic.csdn.net/t/20000223/18/2737.html

Our: global variable definition. If the global variable has been defined in the function, it is equivalent to referencing the global variable. (All Access: upper-level, current, lower-level)

Local: localized the global variable (equivalent to a local global variable). After localization, all modifications to this variable derived from this local variable only affect this local variable. (Access: Current, lower-level)

My: a real local variable. This variable is valid only in this local environment and is invalid in subprocesses. (Access: current)

 

③ Time conversion (conversion between the date and time string formats used in daily use and epoch seconds)

Epoch to daily: $ common = localtime (time); # Second, minute, hour, day, month, year, day of the week, day of the year, and hour

Daily to epoch: $ epoch_seconds = timelocal ($ S, $ M, $ H, $ mday, $ Mon, $ year );

Http://muent.com/a/develop/AllDL/201004303575.html

 

④ Log monitoring

Simulate log writing:

 Use  Io  ::  Handle;
Open (FD , " > Test. Log " ) Or Die $ ! ;
FD -> Autoflush ( 1 );
While ( 1 ){
My $ Now = Time ;
Print FD $ Now , " \ N " ;
Sleep 1 ;
}
Close FD;

Simulate monitoring log:

 Open  (FD  ,     "  <Test. Log  "  ) Or  Die  $  !  ;
While ( 1 ){
My $ In = < FD > ;
Print $ In ;
}
Close FD;

TIPS: You can enter <tail-F test. log | "during read"

 

⑤ Sub-process

Http://blog.csdn.net/aldenphy/archive/2009/11/03/4761585.aspx

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.