|
File.pm - interface to
|
Routine |
Function |
File::Do( $string ) | Conditional file manipulation |
File::Find( $root, $name, $opt, $act ) | Returns list of files matching name, in directory tree below root |
File::InDir( $dir ) | Returns list of files in $dir |
File::lc( $file ) | Returns number of lines in $file |
File::Name( @path ) | Returns a file name from array @path |
File::Tmp( $name, @lines ) | Creates temporary file $name, containing the data in @lines |
File::Dir_nm( $file ) | Returns ( $path, $filename ) for $file |
File::sed( $old, $new, $str1, $str2 ) | Copies $old to $new, substituting $str2 for $str1 |
This example is the test routine File::Test
sub File::Test{ #... Test File routines # my $tdir = "FileTest" ; File::Do( "-d $tdir ? cd $tdir ; W : mkdir -v $tdir ; cd $tdir ; pwd " ); # my( @tmptxt ) = ( "This is a ", "Temporary File " ); File::Tmp( "test.dat", @tmptxt ); File::Do( "-f test.dat ? mv -v test.dat temp.dat : W " ); # File::Do( "chdir .." ); my( @files ) = File::Find( "./", "*.dat","", "-ls" ); # my $file ; foreach $file ( @files ) { print "\n".Err::I( "Found $file" ); } # my $Tmp = File::Name( $tdir, "temp.dat" ); File::Do( "-f $Tmp ? rm -v $Tmp : E " ); # File::Do( "-d $tdir ? rmdir -v $tdir "); # }
File::Do( $string )
File::Do allows commonly recurring command sequences to be replaced by conditional commands - eg
File::Do( "-f test.dat ? mv -v test.dat temp.dat : W " );replaces the command sequence
if( -f "test.dat" ) { `mv -v test.dat temp.dat` } else { print "\n".Err::W( "test.dat does not exist " ) }File::Do also formats the command verification messages ( activated by the switch -v ) , inserting line breaks between file names.
Four standard tests are recognised - -f , -d , -l, -e for files, directories, links, or any file system object.
It is not necessary to use the conditional construction ;
File::Do( "mv -v test.dat temp.dat " )is also acceptable.
Commands
The characters I, W, E, F cause information, warning, error or fatal messages to be printed. File::Do returns with status 0, 1, 2, 3 for I,W,E,F. The messages are identified with the name of the routine which calls Do .
If a number is given as a conditional command, then Do will return the number - eg
File::Do( -f $file ? 8 : 12 )returns 8 if $file exists, otherwise 12.
- cd, chdir
- invoke the Perl chdir function
- mkdir, rmdir
- execute the corresponding unix commands, but translate the standard verification flag '-v' to the non-standard '--verbose' required by these commands ( Redhat 7 now accepts -v as well )
Anything else will be executed ( backticks ) and the output , if any , printed. The two command sections ( before/after : ) may each contain more than one unix command, separated by ;
File::Find( $root, $name, $opt, $act )
Find is an interface to the find command, returning an array of file names matching $name in the directory tree below $root.
my( @files ) = File::Find( "./", "*.dat","", "-ls" );The 3rd argument ( which is optional ) may be used to pass additional arguments to find - eg
- -maxdepth,1
- restricts the search to the root directory & its immediate descendents
- -follow
- allows directories linked to the tree below root to be searched
For other options , see man find .
The fourth argument allows different actions to be specified, in place of the default ( -print ) . Only -ls appears particularly useful..File::InDir( $dir )
returns a list of the files in $dir. The up and self links ( /.. , /. ) are excluded.