--- nfo/perl/libs/shortcuts/files.pm 2003/03/31 05:47:01 1.3 +++ nfo/perl/libs/shortcuts/files.pm 2003/05/13 09:23:03 1.4 @@ -1,7 +1,10 @@ ## --------------------------------------------------------------------------- -## $Id: files.pm,v 1.3 2003/03/31 05:47:01 janosch Exp $ +## $Id: files.pm,v 1.4 2003/05/13 09:23:03 joko Exp $ ## --------------------------------------------------------------------------- ## $Log: files.pm,v $ +## Revision 1.4 2003/05/13 09:23:03 joko +## pre-flight checks for existance of base directory of to-be-executed script +## ## Revision 1.3 2003/03/31 05:47:01 janosch ## Mis mif gex ## @@ -42,22 +45,36 @@ use Data::Dumper; +use File::Basename; sub s2f { my $filename = shift; my $string = shift; + + # pre-flight checks: Does directory exist? + my $dirname = dirname($filename); + if (not -e $dirname) { + print STDERR __PACKAGE__ . ':' . __LINE__ . ": ERROR: Directory '$dirname' does not exist! (Write attempt)" . "\n"; + return; + } + + # Perform: File write open(FH, '>' . $filename); print FH $string; + # Always inject ending newline? print FH "\n"; close(FH); } sub f2s { my $filename = shift; + + # pre-flight checks: Does file exist? if (! -e $filename) { - print STDERR __PACKAGE__ . ':' . __LINE__ . ": File $filename does not exist!" . "\n"; + print STDERR __PACKAGE__ . ':' . __LINE__ . ": ERROR: File '$filename' does not exist! (Read attempt)" . "\n"; return; } + # read file at once (be careful with big files!!!) open(FH, '<' . $filename); my @buf_arr = ;