CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Dynamic Logging of any File used in Script

 



tm4c
New User

Jan 23, 2009, 1:59 PM

Post #1 of 5 (2077 views)
Dynamic Logging of any File used in Script Can't Post

Unless a package already exists, I would ideally like a package that I can simply include into any “alien” script, and know of every file that script opened (processed the file’s text) or altered the file properties( copied, deleted, moved to a different directory, or simply renamed e.g.).

Concerning files opened for text processing, I think the most likely solution involves tying to something in Perl which is considered the “generic” FILEHANDLE from which all other FILEHANDLES descend from. I just don’t know what you would call this Alpha FILEHANDLE. Or is there something built in the debugger that would easily accomplish this?

Am I on the right track? Any thought or suggestions are greatly appreciated.

Thank you and best Wishes,


David W.


FishMonger
Veteran / Moderator

Jan 23, 2009, 3:03 PM

Post #2 of 5 (2072 views)
Re: [tm4c] Dynamic Logging of any File used in Script [In reply to] Can't Post

I'm not sure I understand your question; please rephrase.


tm4c
New User

Jan 24, 2009, 9:03 AM

Post #3 of 5 (2065 views)
Re: [FishMonger] Dynamic Logging of any File used in Script [In reply to] Can't Post

If a script opens several different files:


Code
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?

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:

Code
IO\ 
\
<Generic Filehandle> <--Does this exist?
|
|
|
/|\

$FH_1 $FH_2 $FH_3



(This post was edited by tm4c on Jan 24, 2009, 9:08 AM)


FishMonger
Veteran / Moderator

Jan 24, 2009, 9:44 AM

Post #4 of 5 (2059 views)
Re: [tm4c] Dynamic Logging of any File used in Script [In reply to] Can't Post


In Reply To
If a script opens several different files:


Code
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?



In Reply To
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:

Code
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.

Code
my %filehandle; 
my @filenames = qw(file1 file2 file3);

foreach my $file (@filenames) {
open $filehandle{$file}, '>', $file or warn "can't open '$file' $!";
}



KevinR
Veteran


Jan 24, 2009, 12:01 PM

Post #5 of 5 (2057 views)
Re: [tm4c] Dynamic Logging of any File used in Script [In reply to] Can't Post

My first thought was there might be a perl variable that holds the name of the current/last file opened, but I did not see one. You can search through the list, maybe I missed something:

http://perldoc.perl.org/perlvar.html

It sounds like you are trying to monitor what files users are opening. There is no easy way I can think of to do this. All the file IO would have to handled by a dedicated module that gets loaded at compile time. The main script passes all file IO operation to the dedicated module which logs all file IO activity or certain activity only. That would require a rewrite of existing perl scripts as well as writing the dedicated module that handles the file IO requests. The module should not be hard to write, but rewriting all perl script to use it might be.
-------------------------------------------------


(This post was edited by KevinR on Jan 24, 2009, 12:01 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives