[22082] in Perl-Users-Digest
Perl-Users Digest, Issue: 4304 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Dec 23 00:05:51 2002
Date: Sun, 22 Dec 2002 21:05:07 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 22 Dec 2002 Volume: 10 Number: 4304
Today's topics:
Re: Automate FTP access accounts? <toni@stderror.at>
compile problems in Perl 5.8 <z.atlas@verizon.net>
Re: Design Tools and Resources on one page . . . it is <uri@stemsystems.com>
Expat and xml::parser on windows server. (stew dean)
Re: Expat and xml::parser on windows server. <bart.lateur@pandora.be>
File::Find module ; want to resume search from point of (Mitchell Laks)
Re: Help with require problem (J.B. Moreno)
Re: Line delimiters in Win32 Perl <bart.lateur@pandora.be>
Re: Modifying E-mail header <spamtrap@bd-home-comp.no-ip.org>
Need help with octal numbers in substitution <No_Mail_Address@cox.net>
Re: php and perl <bwalton@rochester.rr.com>
regexp for hostnames <zmeneti@chello.hu>
Re: regexp for hostnames (Jay Tilton)
Re: regexp for hostnames <mgjv@tradingpost.com.au>
Re: regexp for hostnames (Tad McClellan)
Re: regexp for hostnames <Jodyman@hotmail.com>
Re: regexp for hostnames <mgjv@tradingpost.com.au>
Re: Tied Filehandles <ahamm@mail.com>
Re: Tied Filehandles <ahamm@mail.com>
Re: Tied Filehandles <ahamm@mail.com>
Re: Tied Filehandles <ahamm@mail.com>
Re: Tied Filehandles <ahamm@mail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 22 Dec 2002 04:14:08 +0000
From: Toni Schmidbauer <toni@stderror.at>
Subject: Re: Automate FTP access accounts?
Message-Id: <slrnb0af0g.1vd8.toni@devil.stderror.at>
On Fri, 20 Dec 2002 01:04:14 GMT, Netshark <netsharkserver@hotmail.com> wrote:
> I am trying to provide my visitors with free web space and want them to be
> able to auto sign themselves up on the ftp server. My question is this. Is
you could store your usernames + password in a sql table and use a ftp
server which supports authentication via sql.
we are doing that with proftpd + mysql + perl.
toni
--
Terror ist der Krieg der Armen, | toni@stderror.at
Krieg ist der Terror der Reichen. | Toni Schmidbauer
- Sir Peter Ustinov |
------------------------------
Date: Mon, 23 Dec 2002 02:21:20 GMT
From: ZAC <z.atlas@verizon.net>
Subject: compile problems in Perl 5.8
Message-Id: <1osc0vke8hpaejms7evplr80vmplvssth7@4ax.com>
Hi
I've just installed the latest Perl 5.8.x from Activeperl. I run the
thing under the latest Open Perl IDE 1.0.x. I had not have any
problems when I used Perl 5.6. Now I try to compile a program that
was compiled with no problems previously.
I get compile errors in some internal program "package Cwd;" the
statement says "use Carp;" and the errors are:
" No DB::DB routine defined at C:/Perl/lib/Cwd.pm line 151.
BEGIN failed--compilation aborted at C:/Perl/lib/Cwd.pm line 151.
Compilation failed in require at ..\Perl/perl5db.pl line 39.
BEGIN failed--compilation aborted at ..\Perl/perl5db.pl line 39.
Compilation failed in require.
BEGIN failed--compilation aborted."
could somebody guide me what should I do\/
Is 5.8 a stable and reliable version or should I go back to 5.6 (I use
Perl for production support - I do not do experiments)
Thanks
ZA
------------------------------
Date: Sun, 22 Dec 2002 21:14:49 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Design Tools and Resources on one page . . . it is fairly extensive!
Message-Id: <x71y49g3i3.fsf@mail.sysarch.com>
>>>>> "g" == gbd <asg779e@earthlink.net> writes:
g> I spent some time researching these sites and this page is the
g> results of that research. This information will aid anybody in web
g> design/development. I am not selling anything - I just want to
g> share this information. I found alot of sites that were a waste of
g> time and this collection of links is the best of what I found
g> available. I hope it benefits all.
and your perl question is?
in case you didn't realize, perl is not the web/cgi.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: 22 Dec 2002 14:18:21 -0800
From: stewart@webslave.dircon.co.uk (stew dean)
Subject: Expat and xml::parser on windows server.
Message-Id: <2b68957a.0212221418.7f3cfbfe@posting.google.com>
Got a simple job I'm trying to do.
I'm trying to parse XML. I need my final solution to work on a windows
server and also be able to develop it locally.
Okay so I've got perl working, activestate/IIs on the target machine
and indigo perl/apache locally. I know this appears to be complicated
but just take it that's the set up.
I did my home work and found the module that will do the job is
xml::simple, which in turn requires xml::parser which sits on Expat.
Now I thought it would just be a matter of copying a file into the lib
library. I download xml::parser and it says I have to make the file.
I'm on a windows machine so this is no good.
So bit by bit.
1) So far I have downloaded Expat and have a bin file with win32 dlls
- what do I do with these - where do I put them.
2) Using the Indigo GUI packadge module handler and the modules found
at http://theoryx5.uwinnipeg.ca/ppmpackages/ I can see xml::parser and
xml::simple but get an error of
'ERROR: PPD does not contain an implementation section for OS=MSWin32
Module XML-Parser not found'
For xml::parser.
Last time I wrote perl was two years ago and it was simple form
handling. I need some help to work out how just to set up my
environments so I can then use these modules to pass the HTML files I
then am going to output to various HTML files (not just a simple
output but formatted with indexes).
Once I get the xml into perl I can work out the rest - but I need to
get there first.
Can anyone help me?
Stew Dean
------------------------------
Date: Sun, 22 Dec 2002 22:54:31 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Expat and xml::parser on windows server.
Message-Id: <2lfc0vgk6n2oe4km1n6lkjta1m7g4eh2ht@4ax.com>
stew dean wrote:
>Okay so I've got perl working, activestate/IIs on the target machine
>and indigo perl/apache locally. I know this appears to be complicated
>but just take it that's the set up.
XML::Parser (and XML::Parser::Expat) come with ActivePerl, but not with
IndigoPerl.
>I did my home work and found the module that will do the job is
>xml::simple, which in turn requires xml::parser which sits on Expat.
>Now I thought it would just be a matter of copying a file into the lib
>library. I download xml::parser and it says I have to make the file.
>I'm on a windows machine so this is no good.
You're still not familar with the way things are done on
ActivePerl/IndigoPerl? Basically, for those popular modules that need
compilation, somebody has already done it for you, and made the package
available together with a so-called PPD file (that's the file
extension). Activestate's basic repositiory is at
<http://www.activestate.com/PPMpackages/5.6/> and
<http://www.activestate.com/PPMpackages/5.6plus/>. The binaries
themselves are in a subdirectory with the name
"MSWin32-x86-multi-thread". Both ActivePerl and IndigoPerl know how to
install these.
On ActivePerl, go to the DOS prompt, and use PPM (a Perl script wrapped
in a .bat file) to install new modules. Just like CPAN.pm, it has a
built-in shell. Try the included documentation to see what's possible.
On IndigoPerl, start "PerlConsole", which launches Apache on a special
port, and starts up your browser and points it to its home page. In the
left frame, click on the third link: "GUI Package Manager". An interface
opens in the frame on the right. You can select and install modules from
any repository selected, of which the script knows how to handle it. I
don't know if it got fixed already, but it used to have problems with
the format of the PPD files from Activestate's "5.6plus" repository. I
have more or less patched it for myself, but...
For XML::Parser, add a repositiory: <http://www.xmlproj.com/PPM/>.
You'll see, when you select it, "XML-Parser-2_30" among the options. OK,
now that I tried it, I know it refuses to just install. I'll have to get
back to you on it. Again, it's likely something that has changed in the
PPD file format, that causes this error. Custom written parsers suck.
--
Bart.
------------------------------
Date: 22 Dec 2002 13:47:54 -0800
From: mlaks2000@yahoo.com (Mitchell Laks)
Subject: File::Find module ; want to resume search from point of failure.
Message-Id: <ab3b13db.0212221347.2f5c4446@posting.google.com>
Hi -
Dear Perl Gurus
I am using File::Find to execute a command on the files in a directory
(i call the command &wanted for cultural reasons :) ).
However if an exception is raised, and my search gets aborted, and if
i keep track of what file was acted upon when the search was
cancelled, is there a way to resume the search and execution from the
point of failure and avoid searching through all of the (530,000)
files again from the beginning?
Thanks millions - i guess i want to be able to say is
do the file::find algorithm on a directory tree - but begin at
specified file xyz in the tree... instead of the beginning...
Thanks,
Mitchell
------------------------------
Date: Sun, 22 Dec 2002 15:03:48 -0500
From: planB@newsreaders.com (J.B. Moreno)
Subject: Re: Help with require problem
Message-Id: <1fnlk3p.1tyv93z1rrkoldN%planB@newsreaders.com>
Philip Taylor <philip@zaynar.demon.co.uk> wrote:
> "J.B. Moreno" <planB@newsreaders.com> writes
> >fakessi.lib (now) consists of just:
> >==
> >sub testlib {
> > print "Content-type: text/html\n\n";
> > }
> >==
>
> If that's all it contains, you'll get a "fakessi.lib did not return a
> true value" error -- try adding
> 1;
> at the end. If it still doesn't work, it would possibly help to either
> check the web server's error log or add "use CGI::Carp
> qw(fatalsToBrowser);" at the beginning to print error messages to the
> browser [since you appear to be running it in a CGI context].
Thanks, that did it -- apparently the original problem was the path not
being set properly when called from php so that the require wasn't
finding the library (I ran it from the command line at that point, no
problem), then when I stripped it down to make a fake library, I left
off the 1; at the end which created another problem, so that when I
tried it with a full path, it still didn't work.
Again, thanks a lot.
--
JBM
"Your depression will be added to my own" -- Marvin of Borg
------------------------------
Date: Sun, 22 Dec 2002 20:25:47 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Line delimiters in Win32 Perl
Message-Id: <0d7c0vo3fcf47ok5bjel6asfq50kh1jjkn@4ax.com>
T Conti wrote:
>I have run into an anomaly that I am seeking some explanation of in
>Win32 perl. I am writing a service that reads in a file. This could
>be a Unix or windows file. The lines in the Unix file will be
>terminated by \n (OA) and the lines in the windows file are delimited
>by \r\n (0D 0A).
Goo. Read it as a text file on Windows, and Perl will treat them as
equivalent. Unfortunately, not on Unix. There, you can still get rid of
any remaining CR characters using
tr/\r//d;
If you use
tr/\r\n//d;
you no longer need the chomp().
>I use chomp to rip the delimiter from the line and I
>noticed that it is removes the \n from the first file as expected, but
>it also hack off \r\n from the second file. This is confusing.
It's the file reading routines that convert CR+LF to bare "\n" (LF) in
text mode. To prevent that (why should you), use binmode() on that
handle.
> I
>append a \n to the end of the line and when I view the file in hex the
>lines are delimited by \r\n. Does anyone have any input on this
>behavior? Do I need to set a property in Perl to modify this behavior?
perldoc -f binmode
You may skip the paragraph(s) on disciplines.
--
Bart.
------------------------------
Date: Mon, 23 Dec 2002 01:39:24 GMT
From: bd <spamtrap@bd-home-comp.no-ip.org>
Subject: Re: Modifying E-mail header
Message-Id: <vfp5ua-vul.ln@ID-151211.user.cis.dfn.de>
Steve C wrote:
> Hi,
> I'm running Exim and have a requirement to re-write e-mail message headers
> when mail is sent to certain recipients.
> Exim has some rules for doing address rewriting but the needs are quite
> specific so I'm going to have to do it with perl.
> The e-mail is passed to a perl script which takes the e-mail on its STDIN.
>
> What I'm trying to do, is take the From: header and replace it with an
> arbitary e-mail address, then seek to put the original From: recipient
> e-mail address either on the end of the subject line OR in the first line
> of the message body.
> This gets difficult if the e-mail contains html aswell as plain/text.
> Anyway I need to make the changes and then pass the whole message to the
> STDOUT which will return it to Exim for delivery.
Hm, how about:
my $line;
my %headers;
while (($line = <>) =~ /^([^: ]+): (.*)$/){
$headers{$1} = $2;
}
$headers{'Subject'} .= " From: " . $headers{'From'};
$headers{'From'} = 'John Q. Smith';
foreach $header (keys %headers){
print "$header: $headers{$header}\n";
}
print $line;
while(<>){
print $_;
}
This is rather hackish, but it works. Also note that the order of the
headers in the output is undefined, but that shouldn't matter.
--
Replace spamtrap with bd to reply.
It's gonna be alright,
It's almost midnight,
And I've got two more bottles of wine.
------------------------------
Date: Sun, 22 Dec 2002 20:42:05 GMT
From: Fred <No_Mail_Address@cox.net>
Subject: Need help with octal numbers in substitution
Message-Id: <3E0623FF.D550B0A7@cox.net>
Hello there,
I want to remove all 8-bit characters from a file, but keep ü ä ö
(decimal 129, 132, 148)
From
$a = 129;
$b = sprintf "%o", $a;
print $b ;
I got the octals
ü = octal 201
ä = octal 204
ö = octal 224
When I use code like
my $cc2 = '[\200-\377]' ;
while(<>){
s/$cc2+/ /g ;
}
I get rid of all 8-bit characters, as expected.
However, when I want to keep octal 201, 204 and 224 with
my $cc2 = '[\205-\223\225-\377]' ;
then ü ä ö are removed.
And when I set
my $cc2 = '[\200-\343\345-\377]' ;
then ä is not removed.
Question: How do I determine the "working" values? What am I doing
wrong?
---
Fred
------------------------------
Date: Mon, 23 Dec 2002 02:13:13 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: php and perl
Message-Id: <3E067121.7080506@rochester.rr.com>
J.B. Moreno wrote:
> In article <201220022305395391%planb@newsreaders.com>,
> J.B. Moreno <planb@newsreaders.com> wrote:
...
> putenv("QUERY_STRING=section=header&pagetitle=Downloads");
> passthru('../ssi/testssi.cgi');
>
> Which seems to do just what I want, except for one slight detail...
>
> The "testssi.cig" requires another file and it seems to stop dead
> afterwords.
...
It just "stops dead"? No error message? Nothing in the server log? No
blue screen, or even a kernel panic? I doubt it. More information --
like exactly what errors were generated -- would be very helpful in
solving your problem -- so helpful you might even solve it yourself. I
suggest you carefully check the location of the file your are requiring
in relation to the directory your CGI script runs in. The various paths
involved are probably not what you think they are. Specifying an
absolute path for the file to be require'd might help. Also, make sure
you didn't misspell it like you did above.
--
Bob Walton
------------------------------
Date: Sun, 22 Dec 2002 23:08:17 GMT
From: Zoltan Nemeti <zmeneti@chello.hu>
Subject: regexp for hostnames
Message-Id: <3E064641.3000709@chello.hu>
Greetings!
I'd like to create a pattern, which should match
every host names except of the subdomains: red|green
My hostnames consist of 3 or more parts, like
- b.a.com
Z.Y.X.W.com
I'd like to match everything except of red|green
at the 3rd pos. from right:
So Z.Y.X.red.W.com and
b.green.a.com shouldn't match
but Z.Y.X.heavy.W.com should match, and so on.
I'd like to use a general pattern, so only red|green
should be specific. For my pleasure...
Thank you for your help!
Zoltan
------------------------------
Date: Mon, 23 Dec 2002 00:06:00 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: regexp for hostnames
Message-Id: <3e0649b2.57263192@news.erols.com>
Zoltan Nemeti <zmeneti@chello.hu> wrote:
: I'd like to create a pattern, which should match
: every host names except of the subdomains: red|green
:
: My hostnames consist of 3 or more parts, like
: - b.a.com
: Z.Y.X.W.com
:
: I'd like to match everything except of red|green
: at the 3rd pos. from right:
:
: So Z.Y.X.red.W.com and
: b.green.a.com shouldn't match
:
: but Z.Y.X.heavy.W.com should match, and so on.
:
: I'd like to use a general pattern, so only red|green
: should be specific.
Does you really need it done with a single pattern match?
#!perl
use warnings;
use strict;
while(<DATA>) {
chomp;
my $test = (split /\./)[-3] !~ /^(?:green|red)$/;
printf "%s : %s\n", $_, ($test ? "pass" : "fail");
}
__DATA__
b.a.com
Z.Y.X.W.com
Z.Y.X.red.W.com
b.green.a.com
Z.Y.X.heavy.W.com
------------------------------
Date: Mon, 23 Dec 2002 00:18:38 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: regexp for hostnames
Message-Id: <slrnb0clmn.865.mgjv@verbruggen.comdyn.com.au>
On Sun, 22 Dec 2002 23:08:17 GMT,
Zoltan Nemeti <zmeneti@chello.hu> wrote:
> Greetings!
> I'd like to create a pattern, which should match
> every host names except of the subdomains: red|green
>
> My hostnames consist of 3 or more parts, like
> - b.a.com
> Z.Y.X.W.com
>
> I'd like to match everything except of red|green
> at the 3rd pos. from right:
>
> So Z.Y.X.red.W.com and
> b.green.a.com shouldn't match
>
> but Z.Y.X.heavy.W.com should match, and so on.
>
> I'd like to use a general pattern, so only red|green
> should be specific. For my pleasure...
Many things are left unspecified, but maybe you can start at this:
#!/usr/local/bin/perl -w
use strict;
while (<DATA>)
{
chomp;
if (/(^|\.)(green|red)\..+\.com$/)
{
print "NO : $_\n";
}
else
{
print "YES: $_\n";
}
}
__DATA__
grub.com
b.a.com
Z.Y.X.W.com
Z.Y.X.red.W.com
green.a.com
red.b.com
b.green.a.com
b.a.green.com
b.agreen.a.com
foo.bar.reda.c.com
Another possibility would be something like:
my $third_part = (split /\./)[-3];
# compare $third_part against the wanted or unwanted things.
# For names shorter than 3 parts, it'll be undefined.
I'd probably choose the latter one, for reasons of readability.
Note that neither of these checks for "well-formed" names, whatever
that may mean in your organisation.
Martien
--
|
Martien Verbruggen |
Trading Post Australia | In a world without fences, who needs Gates?
|
------------------------------
Date: Sun, 22 Dec 2002 18:19:34 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: regexp for hostnames
Message-Id: <slrnb0clkm.aoc.tadmc@magna.augustmail.com>
Zoltan Nemeti <zmeneti@chello.hu> wrote:
> I'd like to match everything except of red|green
> at the 3rd pos. from right:
>
> So Z.Y.X.red.W.com and
> b.green.a.com shouldn't match
>
> but Z.Y.X.heavy.W.com should match, and so on.
/(?<!green)(?<!red)\.[^.]+\.[^.]+$/
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Mon, 23 Dec 2002 04:07:37 GMT
From: "Jodyman" <Jodyman@hotmail.com>
Subject: Re: regexp for hostnames
Message-Id: <d_vN9.1879$ka5.217121@newsread1.prod.itd.earthlink.net>
"Tad McClellan" <tadmc@augustmail.com> wrote in message
news:slrnb0clkm.aoc.tadmc@magna.augustmail.com...
> Zoltan Nemeti <zmeneti@chello.hu> wrote:
>
> > I'd like to match everything except of red|green
> > at the 3rd pos. from right:
> >
> > So Z.Y.X.red.W.com and
> > b.green.a.com shouldn't match
> >
> > but Z.Y.X.heavy.W.com should match, and so on.
>
>
> /(?<!green)(?<!red)\.[^.]+\.[^.]+$/
ah, the master of look behind. :-)
------------------------------
Date: Mon, 23 Dec 2002 15:29:55 +1100
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: regexp for hostnames
Message-Id: <slrnb0d4a3.aid.mgjv@martien.heliotrope.home>
On Sun, 22 Dec 2002 18:19:34 -0600,
Tad McClellan <tadmc@augustmail.com> wrote:
> Zoltan Nemeti <zmeneti@chello.hu> wrote:
>
>> I'd like to match everything except of red|green
>> at the 3rd pos. from right:
>>
>> So Z.Y.X.red.W.com and
>> b.green.a.com shouldn't match
>>
>> but Z.Y.X.heavy.W.com should match, and so on.
>
>
> /(?<!green)(?<!red)\.[^.]+\.[^.]+$/
Of course, this also matches "a.1green.foo.com", which possibly wasn't
what the OP intended.
Martien
--
|
Martien Verbruggen | If at first you don't succeed, try again.
| Then quit; there's no use being a damn fool
| about it.
------------------------------
Date: Mon, 23 Dec 2002 10:46:29 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Tied Filehandles
Message-Id: <au5iu0$4d1c6$1@ID-79573.news.dfncis.de>
Anno Siegel wrote:
>
> Umm... What makes you think printing something to STDOUT would make
> it appear in the output to the tied filehande? "Tie" is magic, but
> not *that* magic. As you have seen, the output appears on STDOUT.
Ahhh, nothing. I expected to see my debugging contents on STDOUT, but I
don't. I stripped the garnish routine down to barebones, and just made it
print the debugging message. Given that I only see two debugging lines, not
4, I can only assume that garnish is called only twice and therefore the
non-method call method (ahem) is not working.
------------------------------
Date: Mon, 23 Dec 2002 10:53:31 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Tied Filehandles
Message-Id: <au5jb6$4lpfa$1@ID-79573.news.dfncis.de>
Anno Siegel wrote:
>
> I must have missed a bend in the thread, apparently the "print" is
> there for debugging and not for the original purpose. Apologies.
>
OK ;-) I just caught up. So you can safely ignore the last reply posted 30
seconds ago.
> I see now what the code is up to. I wasn't too amazed not to see the
> print method called because the ordinary print operator has its own
> indirect object slot (and a filehandle will fit). Reading "print
> $myhandle ..." as a method call through indirect object syntax
> probably doesn't occur to the parser, though it might.
Tassilo expects it to work so I'm keen to hear his spin.
Tassilo, have you actually run my code? If so, which version of Perl and is
it possible to also test on Perl 5.6.1? I guess I should also download the
version of IO::File I have:
1.08
Which *seems* to be latest. So much is bundled in 5.8 these days and with
recent changes to CPAN it's getting hard to use my old ways of getting to
modules. If it's bundled, you seem to end up at the Perl 5.8 bundle instead
of the individual and it won't let you sidestep easily. I need to learn new
navigation techniques ;-)
PS - is there a sweeter way (which is still fast) to get module versions
than this:
perl -MIO::File -e 'print "$IO::File::VERSION\n"'
------------------------------
Date: Mon, 23 Dec 2002 10:54:55 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Tied Filehandles
Message-Id: <au5jdp$4k7k5$1@ID-79573.news.dfncis.de>
Benjamin Goldberg wrote:
>
> If you inherit from Tie::Handle, then you only need to define a WRITE
> method.
>[SNIP]
Great - thanks for the suggestion. Consider it tested in a few hours ;-)
I'll let you know.
------------------------------
Date: Mon, 23 Dec 2002 12:38:42 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Tied Filehandles
Message-Id: <au5pgd$4eeu9$1@ID-79573.news.dfncis.de>
Benjamin Goldberg wrote:
>
> If you inherit from Tie::Handle, then you only need to define a WRITE
> method.
>
>[SNIPPED code]
Bewwwwwdiful. Thanks. So Tie::Handle maps PRINT and PRINTF to WRITE. Nice. I
was hoping for that.
I made a few changes, some gratuitous, some necessary. I won't point out the
missing semi-colon - anyone could miss that as the code comes straight off
the fingers ;-)
<test script>
#!/usr/bin/perl
use strict;
use warnings;
use TimeStampFH;
my $fh = new TimeStampFH('>junk');
print $fh "this is\na test\nof timestamped";
print $fh "\nfilehandles\n";
print $fh "which is";
print $fh " very comprehensive\nas a test (cough)\n";
</test script>
<results>
[Mon Dec 23 12:13:48 2002] this is
[Mon Dec 23 12:13:48 2002] a test
[Mon Dec 23 12:13:48 2002] of timestamped
[Mon Dec 23 12:13:48 2002] filehandles
[Mon Dec 23 12:13:48 2002] which is very comprehensive
[Mon Dec 23 12:13:48 2002] as a test (cough)
</results>
<TimeStampFH.pm changenotes embedded>
package TimeStampFH;
use IO::File; # seems more appropriate?
use Symbol qw(gensym);
use base qw(Tie::Handle);
sub new {
my $self = gensym;
tie *$self, $_[0], @_[1 .. $#_];
$self;
}
sub TIEHANDLE {
my ($class, @args) = @_;
my $fh = IO::File->new(@args) or return; # as appropriate above...
bless [ $fh, 1 ], $class;
}
sub WRITE {
my $self = shift;
my $str = substr $_[0], $_[2], $_[1];
my ($fh, $oldstate) = @$self;
my $timestamp = '[' . localtime() . '] '; # (almost) the format I
want.
# it could be useful to offer storage of a subref to a garnish block?
my $newstate = do { local $/ = "\n"; chomp $str };
$self->[1] = $newstate;
if( $oldstate ) {
$str =~ s/^/$timestamp/mg;
} else {
$str =~ s/(?<=\n)/$timestamp/g;
}
$str .= "\n" if $newstate;
print $fh $str;
}
1;
</TimeStampFH.pm>
and that makes it work and suit my eye to boot.
Thanks muchly. This will make debugging sendmail alias scripts and daemons
much easier. The only remaining trick is to tie and/or bless STDERR into
this class so that all my warns, carps, croaks and dies are timestamped into
my logfile.
tie *STDERR, 'TimeStampFH', '>junk';
does the job.
Cheers.
Oh, and
$MerryChristmas = $your->{religion}->celebration('December', -date =>
'closest', -scope => 'major');
print "$MerryChristmas and Happy New Year to all."
------------------------------
Date: Mon, 23 Dec 2002 14:57:10 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Tied Filehandles
Message-Id: <au61k2$4f9pd$1@ID-79573.news.dfncis.de>
Andrew Hamm wrote:
>
> Bewwwwwdiful. Thanks. So Tie::Handle maps PRINT and PRINTF to WRITE.
> Nice. I was hoping for that.
LOL - because if you don't laugh you cry...
It works fine in the general case, and even with:
tie *STDERR, 'TimeStampFH', '>> junk';
this will work:
print STDERR "this is a test\n";
however,
warn "this is a test\n";
does not. Output goes to original stderr, not to the target file, and it is
not garnished either way. The closest to getting delivery into the file is:
perl -MTimeStampFH -e '
open EFD, ">> junk";
tie *EFD, "TimeStampFH", ">> junk";
*STDERR = *EFD;
warn "this\nis\nit\n";
'
which delivers to junk but does not garnish. As such, it appears to be
merely a replacement of STDERR. It appears that warn is not invoking the
ties. Since warn is most likely C code, perhaps it's accessing
fileno(STDERR) and writing directly to that. Unfortunately I don't have the
knowledge of the source of Perl to go looking easily at that or to get an
understanding.
So ......
hmmmm
I don't know. I'll try going back to use of IO::Handle as Benjamin
suggested, instead of IO::File. Perhaps there is some deep magic which works
better. I just couldn't see how to open the target file with IO::Handle
since it doesn't appear to have an open method. Back to TFM.
ZZ
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 4304
***************************************