perl fileio

 
#################################### 
#####       perl fileio        ##### 
#################################### 
 
 
 
while(<>)             # same as while($_ = <STDIN>) 

   print $_;          # accepts cmd line input, and echo 

 
 
#### 
####   open()  close() 
#### 
 
e.g. 
--------------------------- // print.pl 
#!/usr/bin/perl 
 
open(FH, "</usr/share/dict/words"); 
 
while(<FH>) 

#    chomp $_; 
     print $_; 

 
close FH; 
-------------------------- 
 
 
#### 
####    read()  print() 
#### 
 
 
 
 
 
 
#### 
####   auto-flush 
#### 
 
$|=1    # enable.  same as $|++ 
$|=0    # disable. default. 
 
====> useful when you want to force the flush. e.g. a loop in which you keep printing process status, as in progress bar. you dont want the status to be buffered in mem to be flushed later at once. 
 
 
 
#### 
####   flock() 
#### 
 
flock( <file_handler> , N ); 
 
N=1  #  block write. allow read. if somebody tries, he has to wait. 
N=2  #  block write/read. if somebody tries, he has to wait. 
N=5  #  block write. allow read. if somebody tries, returns 0. 
N=6  #  block write/read. if somebody tries, returns 0. 
N=8  #  unlock --->  same as close(FH) 
 
e.g. 
 
$afile="./access_counter"; 
open(FH,"+< $afile") or die print "Error opening $afile"; 
flock(FH,2); 
.. 
.. 
close(FH); 
 
 
(URL)   http://homepage1.nifty.com/glass/tom_neko/web/web_04.html 
 
 
#### 
####   seek(FH, pos, whence) 
#### 
 
pos -- bytes 
whence --  0:BOF, 1:current, 2:EOF 
 
e.g.   # typical access counter 
------------------------------ 
$data_file="c.dat"; 
open(DATA,"+< $data_file") or die print "Error"; 
flock(DATA,2); 
seek(DATA,0,0);   ## in case you have to wait, then you should explicitly make your pointer point the beginning 
$data=<DATA>; 
$data++; 
seek(DATA,0,0);   ## this is for writing from the beginning of the file 
print DATA $data; 
close(DATA); 
------------------------------- 
 
 
 
#### 
####  unary operator 
#### 
 
(ref)  http://perldoc.perl.org/functions/-X.html 
 
copied from the above ref 
------------------------------------------------ 
    -r  File is readable by effective uid/gid. 
    -w  File is writable by effective uid/gid. 
    -x  File is executable by effective uid/gid. 
    -o  File is owned by effective uid. 
    -R  File is readable by real uid/gid. 
    -W  File is writable by real uid/gid. 
    -X  File is executable by real uid/gid. 
    -O  File is owned by real uid. 
    -e  File exists. 
    -z  File has zero size (is empty). 
    -s  File has nonzero size (returns size in bytes). 
    -f  File is a plain file. 
    -d  File is a directory. 
    -l  File is a symbolic link (false if symlink is not supported by the file system). 
    -p  File is a named pipe (FIFO), or Filehandle is a pipe. 
    -S  File is a socket. 
    -b  File is a block special file. 
    -c  File is a character special file. 
    -t  Filehandle is opened to a tty. 
    -u  File has setuid bit set. 
    -g  File has setgid bit set. 
    -k  File has sticky bit set. 
    -T  File is an ASCII or UTF-8 text file (heuristic guess). 
    -B  File is a "binary" file (opposite of -T). 
    -M  Script start time minus file modification time, in days. 
    -A  Same for access time. 
    -C  Same for inode change time (Unix, may differ for other platforms) 
------------------------------------- 
 
e.g. 
--------------- 
print (-s $file);              # file size 
print "readable" if -r $file;  # if arg omitted, then it tests "$_" 
print "binary" if -B $file; 
print "exist" if -e $file; 
--------------- 

  1. 2015-01-17 00:39:55 |
  2. Category : perl
  3. Page View:

Google Ads