① 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