
FishMonger
Veteran
/ Moderator
Jan 24, 2009, 9:44 AM
Post #4 of 5
(3449 views)
|
Re: [tm4c] Dynamic Logging of any File used in Script
[In reply to]
|
Can't Post
|
|
If a script opens several different files: open( $FH_1, '>', $file_name_1 ); open( $FH_2, '>', $file_name_2 ); open( $FH_3, '>', $file_name_3 ); What would be the easiest way for me to know the name of the files opened by the script? Well, the name would be in $file_name_x or am I missing something in ypur question?
If the script used only one filehandle, $FH, then I could simply watch this variable for any changes, and update the log accordingly. In the real world, however, there is no guarantee some one will use only a single filehandle variable. The main point is that I want this script to "plug" into any existing Perl script without needing to modify either script too much; therefore, I'm essential ignorant of the actual name of the variables used. I would have to realize on a either a global variable, or generic data type that all filehandles fall under. Perl Hierarchy: IO\ \ <Generic Filehandle> <--Does this exist? | | | /|\ $FH_1 $FH_2 $FH_3 If you're asking if a single lexical var used for a filehandle could refer to multiple files (which could be a mix of read, write or append modes) then the answer is NO. However, you could use a hash of filehandles and the keys would be the names of the files.
my %filehandle; my @filenames = qw(file1 file2 file3); foreach my $file (@filenames) { open $filehandle{$file}, '>', $file or warn "can't open '$file' $!"; }
|