<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:arial,helvetica,sans-serif;font-size:10pt"><div>Alex,<br><br>I am trying create a module, say myLog.pm, to override the built-in open function, adding auxiliary abilities (logging the file access information). <br>I am looking for minor modification, adding one line "<span style="font-family: Courier New,courier,monaco,monospace,sans-serif;">use myLog 'open';'</span> in a hundreds of scripts. The developing module works for the scripts without using typeglob filehandle (open FH, $filename); if no using "use stricts", it also works for the typeglob filehandle.<br><br>It seems after overriding, the compiler take the 'FH' in the open function as calling a subroutine. <br><br><br>Tiger,<br></div><div style="font-family: arial,helvetica,sans-serif; font-size: 10pt;"><br><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;"><font
face="Tahoma" size="2"><hr size="1"><b><span style="font-weight: bold;">From:</span></b> Alexander Danel <danel@speakeasy.net><br><b><span style="font-weight: bold;">To:</span></b> Chicago.pm chatter <chicago-talk@pm.org><br><b><span style="font-weight: bold;">Sent:</span></b> Mon, October 25, 2010 3:10:10 PM<br><b><span style="font-weight: bold;">Subject:</span></b> Re: [Chicago-talk] logging in and out files<br></font><br>
<style>
<!--
_filtered {font-family:Tahoma;panose-1:2 11 6 4 3 5 4 4 2 4;}
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;margin-bottom:.0001pt;font-size:12.0pt;font-family:"Times New Roman";}
a:link, span.MsoHyperlink
{color:blue;text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:blue;text-decoration:underline;}
span.EmailStyle17
{font-family:Arial;color:navy;}
_filtered {margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
{}
-->
</style>
<div class="Section1">
<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;">Tiger,</span></font></p>
<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;"> </span></font></p>
<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;">Firstly, I noticed you named your function
“open()†– I wouldn’t use that name, it is the name of
a Perl operator; In fact, you use Perl “open()†within your “open()â€.</span></font></p>
<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;"> </span></font></p>
<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;">Secondly, the error message might be
indicating a simple syntax error in preceding code. Look for a missing
semi-colon, etc., in the code just before the error.</span></font></p>
<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;"> </span></font></p>
<p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial; color: navy;">Alexander Danel</span></font></p>
<div>
<div class="MsoNormal" style="text-align: center;" align="center"><font face="Times New Roman" size="3"><span style="font-size: 12pt;">
<hr tabindex="-1" align="center" width="100%" size="2">
</span></font></div>
<p class="MsoNormal"><b><font face="Tahoma" size="2"><span style="font-size: 10pt; font-family: Tahoma; font-weight: bold;">From:</span></font></b><font face="Tahoma" size="2"><span style="font-size: 10pt; font-family: Tahoma;">
chicago-talk-bounces+danel=speakeasy.net@pm.org
[mailto:chicago-talk-bounces+danel=speakeasy.net@pm.org] <b><span style="font-weight: bold;">On Behalf Of </span></b>tiger peng<br>
<b><span style="font-weight: bold;">Sent:</span></b> Monday, October 25, 2010
12:24 PM<br>
<b><span style="font-weight: bold;">To:</span></b> <a target="_blank" href="http://Chicago.pm">Chicago.pm</a>
chatter<br>
<b><span style="font-weight: bold;">Subject:</span></b> Re: [Chicago-talk]
logging in and out files</span></font></p>
</div>
<p class="MsoNormal"><font face="Times New Roman" size="3"><span style="font-size: 12pt;"> </span></font></p>
<div>
<p class="MsoNormal"><font face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial;">Weird, when I tested the module, with </span></font><font face="Courier New" size="2"><span style="font-size: 10pt; font-family: "Courier New";">open
FH, $filename; </span></font><font face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial;">with </span></font><font face="Courier New" size="2"><span style="font-size: 10pt; font-family: "Courier New";">use strict; It complain with
message: Bareword "FH" not allowed while "<span style="background: none repeat scroll 0% 0% yellow;">strict subs</span>" in use... </span></font></p>
<div>
<p class="MsoNormal"><font face="Courier New" size="2"><span style="font-size: 10pt; font-family: "Courier New";"> </span></font></p>
</div>
<div>
<p class="MsoNormal"><font face="Courier New" size="2"><span style="font-size: 10pt; font-family: "Courier New";"> </span></font></p>
<div>
<div class="MsoNormal" style="text-align: center;" align="center"><font face="Tahoma" size="2"><span style="font-size: 10pt; font-family: Tahoma;">
<hr align="center" width="100%" size="1">
</span></font></div>
<p class="MsoNormal" style="margin-bottom: 12pt;"><b><font face="Tahoma" size="2"><span style="font-size: 10pt; font-family: Tahoma; font-weight: bold;">From:</span></font></b><font face="Tahoma" size="2"><span style="font-size: 10pt; font-family: Tahoma;"> tiger
peng <tigerpeng2001@yahoo.com><br>
<b><span style="font-weight: bold;">To:</span></b> Chicago.pm
chatter <chicago-talk@pm.org><br>
<b><span style="font-weight: bold;">Sent:</span></b> Fri, October 22, 2010
2:11:07 PM<br>
<b><span style="font-weight: bold;">Subject:</span></b> Re: [Chicago-talk]
logging in and out files</span></font></p>
<div>
<div>
<p class="MsoNormal" style="margin-bottom: 12pt;"><font face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial;">I tried out a working version below.
It works for $FH and FH, but not clear why.<br>
<br>
</span></font><font face="Courier New" size="2"><span style="font-size: 10pt; font-family: "Courier New";">sub open{<br>
my ($package, $file, $line_number) = caller();<br>
my $fullpath = Cwd::abs_path($file);<br>
my $host = hostname();<br>
_open_log({host=>hostname(),<br>
script=>Cwd::abs_path($file),<br>
file=>$_[1],<br>
}<br>
);<br>
if ($_[0]) {<br>
no strict 'refs';<br>
CORE::open("::$_[0]", $_[1]) or die
"Cannot open $_[1]: $!\n";<br>
} else {<br>
CORE::open($_[0], $_[1]) or die "Cannot
open $_[1]: $!\n";<br>
}<br>
}</span></font></p>
</div>
<div>
<p class="MsoNormal"><font face="Courier New" size="2"><span style="font-size: 10pt; font-family: "Courier New";"> </span></font></p>
<div>
<div class="MsoNormal" style="text-align: center;" align="center"><font face="Tahoma" size="2"><span style="font-size: 10pt; font-family: Tahoma;">
<hr align="center" width="100%" size="1">
</span></font></div>
<p class="MsoNormal"><b><font face="Tahoma" size="2"><span style="font-size: 10pt; font-family: Tahoma; font-weight: bold;">From:</span></font></b><font face="Tahoma" size="2"><span style="font-size: 10pt; font-family: Tahoma;"> imran javaid
<imranjj@gmail.com><br>
<b><span style="font-weight: bold;">To:</span></b> <a rel="nofollow" target="_blank" href="http://Chicago.pm">Chicago.pm</a> chatter <chicago-talk@pm.org><br>
<b><span style="font-weight: bold;">Sent:</span></b> Thu, October 21, 2010
2:46:29 PM<br>
<b><span style="font-weight: bold;">Subject:</span></b> Re: [Chicago-talk]
logging in and out files<br>
</span></font><font face="Arial" size="2"><span style="font-size: 10pt; font-family: Arial;"><br>
This might get you somewhere:<br>
<br>
use strict;<br>
use Symbol;<br>
<br>
sub myopen {<br>
if (ref(\$_[0]) eq 'SCALAR') {<br>
open ($_[0], $_[1]);<br>
} else {<br>
my $in = qualify_to_ref($_[0]);<br>
open($in, $_[1]);<br>
}<br>
}<br>
<br>
myopen(*FILE, ">file.txt") or die $!;<br>
print FILE "testing\n";<br>
myopen(my $FILE2, ">file2.txt") or die $!;<br>
print $FILE2 "testing2\n";<br>
<br>
If you remove "use strict;" then you can replace "*FILE"
with "FILE",<br>
but can and should are not the same thing.<br>
<br>
-imran<br>
<br>
On Thu, Oct 21, 2010 at 12:34 PM, tiger peng <<a rel="nofollow" ymailto="mailto:tigerpeng2001@yahoo.com" target="_blank" href="mailto:tigerpeng2001@yahoo.com">tigerpeng2001@yahoo.com</a>> wrote:<br>
> Thanks, it looks a good tool and I will try it later.<br>
><br>
> But my problem now is how to pass in and back the FH.<br>
><br>
> It complains with error message:<br>
> Can't use string ("FH") as a symbol ref while "strict
refs" in use at<br>
> /my/lib/myLogger.pm ...<br>
><br>
> When I let it do not complain with "no strict 'refs';", it does
not complain<br>
> but FH seems not opened or not passed back, as the caller read nothing
out.<br>
><br>
> ________________________________<br>
> From: Joshua <<a rel="nofollow" ymailto="mailto:joshua.mcadams@gmail.com" target="_blank" href="mailto:joshua.mcadams@gmail.com">joshua.mcadams@gmail.com</a>><br>
> To: <a rel="nofollow" target="_blank" href="http://Chicago.pm">Chicago.pm</a> chatter <<a rel="nofollow" ymailto="mailto:chicago-talk@pm.org" target="_blank" href="mailto:chicago-talk@pm.org">chicago-talk@pm.org</a>><br>
> Sent: Wed, October 20, 2010 3:29:53 PM<br>
> Subject: Re: [Chicago-talk] logging in and out files<br>
><br>
> I haven't done this myself before, but it looks like some folks have<br>
> and have put their work on CPAN:<br><span>
> <a target="_blank" href="http://search.cpan.org/%7Ecwest/ex-override-1.1/override.pm">http://search.cpan.org/%7Ecwest/ex-override-1.1/override.pm</a></span><br>
><br>
> On Wed, Oct 20, 2010 at 9:55 AM, tiger peng <<a rel="nofollow" ymailto="mailto:tigerpeng2001@yahoo.com" target="_blank" href="mailto:tigerpeng2001@yahoo.com">tigerpeng2001@yahoo.com</a>> wrote:<br>
>> I wrapped the open as below, and it works for the open $fh, $file but
not<br>
>> open FH, $file.<br>
>><br>
>> As I need to apply the wrapping open function to lots of old script, I<br>
>> prefer only add one line: use myLoger 'open'; into the scripts<br>
>><br>
>> Is there any suggestion?<br>
>><br>
>> sub open{<br>
>> my ($package, $file, $line_number) = caller();<br>
>> my $fullpath = Cwd::abs_path($file);<br>
>> my $host = hostname();<br>
>> _open_log({host=>hostname(),<br>
>>
script=>Cwd::abs_path($file),<br>
>>
file=>$_[1],<br>
>>
}<br>
>>
);<br>
>> CORE::open(shift, shift) or die "Cannot open :
$!\n";<br>
>> }<br>
>><br>
>> sub _open_log {<br>
>> ...<br>
>> }<br>
>><br>
>><br>
>> ________________________________<br>
>> From: tiger peng <<a rel="nofollow" ymailto="mailto:tigerpeng2001@yahoo.com" target="_blank" href="mailto:tigerpeng2001@yahoo.com">tigerpeng2001@yahoo.com</a>><br>
>> To: Chicago.pm chatter <<a rel="nofollow" ymailto="mailto:chicago-talk@pm.org" target="_blank" href="mailto:chicago-talk@pm.org">chicago-talk@pm.org</a>><br>
>> Sent: Tue, October 19, 2010 11:41:10 AM<br>
>> Subject: [Chicago-talk] logging in and out files<br>
>><br>
>> Hello all,<br>
>><br>
>> Does anyone have the experience on tracing input/output files used by
Perl<br>
>> script?<br>
>><br>
>> I am think if there is anyway to over write the build-in open
function, so<br>
>> the new open function can log the file name, IO type (R, W, or RW), as<br>
>> well<br>
>> as the open/close timestamp. Any suggestion is highly appreciated.<br>
>><br>
>> Thanks,<br>
>> Tiger<br>
>><br>
>> _______________________________________________<br>
>> Chicago-talk mailing list<br>
>> <a rel="nofollow" ymailto="mailto:Chicago-talk@pm.org" target="_blank" href="mailto:Chicago-talk@pm.org">Chicago-talk@pm.org</a><br><span>
>> <a target="_blank" href="http://mail.pm.org/mailman/listinfo/chicago-talk">http://mail.pm.org/mailman/listinfo/chicago-talk</a></span><br>
>><br>
> _______________________________________________<br>
> Chicago-talk mailing list<br>
> <a rel="nofollow" ymailto="mailto:Chicago-talk@pm.org" target="_blank" href="mailto:Chicago-talk@pm.org">Chicago-talk@pm.org</a><br>
> <a rel="nofollow" target="_blank" href="http://mail.pm.org/mailman/listinfo/chicago-talk">http://mail.pm.org/mailman/listinfo/chicago-talk</a><br>
><br>
> _______________________________________________<br>
> Chicago-talk mailing list<br>
> <a rel="nofollow" ymailto="mailto:Chicago-talk@pm.org" target="_blank" href="mailto:Chicago-talk@pm.org">Chicago-talk@pm.org</a><br>
> <a rel="nofollow" target="_blank" href="http://mail.pm.org/mailman/listinfo/chicago-talk">http://mail.pm.org/mailman/listinfo/chicago-talk</a><br>
><br>
_______________________________________________<br>
Chicago-talk mailing list<br>
<a rel="nofollow" ymailto="mailto:Chicago-talk@pm.org" target="_blank" href="mailto:Chicago-talk@pm.org">Chicago-talk@pm.org</a><br>
<a rel="nofollow" target="_blank" href="http://mail.pm.org/mailman/listinfo/chicago-talk">http://mail.pm.org/mailman/listinfo/chicago-talk</a></span></font></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div></div>
</div></body></html>