[Chicago-talk] logging in and out files

imran javaid imranjj at gmail.com
Thu Oct 21 12:46:29 PDT 2010


This might get you somewhere:

use strict;
use Symbol;

sub myopen {
  if (ref(\$_[0]) eq 'SCALAR') {
    open ($_[0], $_[1]);
  } else {
    my $in = qualify_to_ref($_[0]);
    open($in, $_[1]);
  }
}

myopen(*FILE, ">file.txt") or die $!;
print FILE "testing\n";
myopen(my $FILE2, ">file2.txt") or die $!;
print $FILE2 "testing2\n";

If you remove "use strict;" then you can replace "*FILE" with "FILE",
but can and should are not the same thing.

-imran

On Thu, Oct 21, 2010 at 12:34 PM, tiger peng <tigerpeng2001 at yahoo.com> wrote:
> Thanks, it looks a good tool and I will try it later.
>
> But my problem now is how to pass in and back the FH.
>
> It complains with error message:
> Can't use string ("FH") as a symbol ref while "strict refs" in use at
> /my/lib/myLogger.pm ...
>
> When I let it do not complain with "no strict 'refs';", it does not complain
> but FH seems not opened or not passed back, as the caller read nothing out.
>
> ________________________________
> From: Joshua <joshua.mcadams at gmail.com>
> To: Chicago.pm chatter <chicago-talk at pm.org>
> Sent: Wed, October 20, 2010 3:29:53 PM
> Subject: Re: [Chicago-talk] logging in and out files
>
> I haven't done this myself before, but it looks like some folks have
> and have put their work on CPAN:
> http://search.cpan.org/~cwest/ex-override-1.1/override.pm
>
> On Wed, Oct 20, 2010 at 9:55 AM, tiger peng <tigerpeng2001 at yahoo.com> wrote:
>> I wrapped the open as below, and it works for the open $fh, $file but not
>> open FH, $file.
>>
>> As I need to apply the wrapping open function to lots of old script, I
>> prefer only add one line: use myLoger 'open'; into the scripts
>>
>> Is there any suggestion?
>>
>> sub open{
>>     my ($package, $file, $line_number) = caller();
>>     my $fullpath  = Cwd::abs_path($file);
>>     my $host = hostname();
>>     _open_log({host=>hostname(),
>>                script=>Cwd::abs_path($file),
>>                file=>$_[1],
>>               }
>>               );
>>     CORE::open(shift, shift) or die "Cannot open : $!\n";
>> }
>>
>> sub _open_log {
>> ...
>> }
>>
>>
>> ________________________________
>> From: tiger peng <tigerpeng2001 at yahoo.com>
>> To: Chicago.pm chatter <chicago-talk at pm.org>
>> Sent: Tue, October 19, 2010 11:41:10 AM
>> Subject: [Chicago-talk] logging in and out files
>>
>> Hello all,
>>
>> Does anyone have the experience on tracing input/output files used by Perl
>> script?
>>
>> I am think if there is anyway to over write the build-in open function, so
>> the new open function can log the file name, IO type (R, W, or RW), as
>> well
>> as the open/close timestamp. Any suggestion is highly appreciated.
>>
>> Thanks,
>> Tiger
>>
>> _______________________________________________
>> Chicago-talk mailing list
>> Chicago-talk at pm.org
>> http://mail.pm.org/mailman/listinfo/chicago-talk
>>
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>


More information about the Chicago-talk mailing list