#!/usr/bin/perl -w ############################################# # This document explains Perl facilities for # file testing, directories etc... # AAK: Last Modification # Tue Sep 11 22:07:39 PDT 2012 # ########################################### use 5.010; $filename = "mydata.dat"; if (-e $filename) { warn "file $filename exists!"; $mdpopt=0; } else { open OUTPUT1, ">mydata.dat"; $mdpopt=1; } #flag -e checks if file or directory exists: #other useful flags are: # -r -R file readable by effective/real user # -w -W file writable """""""""""""""""""""" # -x -X """" exectutable """"""""""""""""""" # -o -O """" owened """""""""""""""""""""""" # -z file exists with zero size # -s file exists with nonzero size (actually returns file size) # -d entry is a directory # -f entry is a plain file # -M returns modification age (in days) if (-M $filename > 20) {say "pretty old file!";} # -A access age (in days) # -T file looks like a text file # -B file looks like a binary file #you can combine several attributes using and: if ( -r $filename and -w $filename ) { print "both readable and writable\n";} #or you can stack them (need perl v 5.010:) use 5.010; if (-w -r $filename) { say" stacking example";} #to get pretty much everything about a file, use stat #which returns almost everything that stat command returns in Unix: @fileinfo = stat($filename); say "@fileinfo"; my $timestamp = 0; my $date = localtime $timestamp; say "the beginning of time for computer is: $date"; $timestamp = 40*3.14E+7; my @date = localtime $timestamp; say $date[0], $date[1]; say "@date"; #chdir changes the "working directory" to the desired location: # "ENV" is a hash that stores the enviroument variables # See hash section of these files for more detail say "your home is: $ENV{HOME}"; chdir "$ENV{HOME}" or die "cannot chdir to $ENV{HOME}: $!"; #globbing is done by use of glob operator: #following will return all of the files in current working directory into #the array all_files: my @all_files = glob "*"; say "@all_files"; if ($frt = glob "*.f") {say "you got some fortran file here!";} else { say "no .f file found";} #directory handles are the same as file handles, you open a directory #and work with it: my $dir_name = "/etc"; opendir ETC, $dir_name or die "cannot open $dir_name: $!"; foreach $file (readdir ETC) { print "file $file is in $dir_name\n"; } closedir ETC; #using unlink you can delete files in Perl: $nfd = unlink "filetmp29382", "filetmp3827321", "filetmp3827172"; #the return of unlink is number of files successfully deleted say "I deleted $nfd files"; #renaming a file is as following: rename "filetmp28321", "filetmpds23721" or warn "unable to rename: $!"; #link and symlink are to create hard and symbolic links to a file: # link "linkname", "targetfile"; # symlink "linkname". "targetfile"; #using mkdir you can create directory: chdir $ENV{HOME}; mkdir "newdirtmp123", 0755 or warn "cannot create directory: $!"; # 0755 is the initial permission of the newly created directory (unix standards) #and change of permission is as following: #chmod 0755, "file1", "file2"; #to run other programs in perl one way is to use system followed by unix command: system "date"; system "ls -l $ENV{HOME}"; #you can also send a program to background and let the script keep running: #using standard & at the end: # system "some_program_that_takes_log &"; #you can also run other scripts: system "somescript"; #you can also use the backquotes similar to unix standard backquote #to capture the output of a program as argument: #the only difference is that perl's `` will also return \n at the end #so it is better to chomp it: chomp (my $time = `date`); say $time; #if the returning variable is an array, then you can have various lines: @processes =`ps`; foreach $i (@processes) { print $i; } #and you can use I/O of a command as filehandle: open DT, "date|" or die "cannot pipe from date :$!"; open MP, "somescript|" or warn "cannot pipe from somescript :$!"; open AP, "|anotherscript" or warn "cannot pipe to anotherscirpt: $!"; $x=`somescript`; print $x; #and you can now read and write from/to the file handles: print AP "this will be sent to anotherscript if exist" or warn "anotherscript is not available"; my $now = <DT>; print $now;