Re: tail -f does not exit
Rob == Rob Dixon [EMAIL PROTECTED] writes: I got your code running nicely, although I had to make a small change due to an older Perl (5.004) I am using: [...] Rob You need to close and reopen the file if you want to check for a rename. Rob Something like the program below. Which actually emulates tail f- nicely, since it notices a filename change and exits, on contrary of tail -f which has to be killed. Rob use strict; Rob use warnings; Rob use IO::Handle; Rob autoflush STDOUT; Rob use Fcntl qw(:seek); Removed line, as seek is not yet a tag in earlier Perl. Rob my $file = 'junk.txt'; Rob my $pos; Rob while (1) { Rob open LOG, $file or die Cannot open $file, $!; Rob seek LOG, $pos, SEEK_SET if defined $pos; seek LOG, $pos, 0 if defined $pos; Rob print while LOG; Rob $pos = tell LOG; Rob close LOG; Rob sleep 1; Rob } Tx a lot, Rob! -- Claude -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: tail -f does not exit
Claude [EMAIL PROTECTED] wrote: I am reading continuously a file like this: open LOG, junk.txt or die Cannot open $file, $!\n; while ( my $line = LOG ) { print $line; } While appending lines to the file from a shell command line: $ echo this is a new line junk.txt Everything ok, except that I would like to find out from the Perl code above when junk.txt has been deleted, or renamed. Unfortunately, tail -f does not exit... Any idea how to do that? Hi Claude, You need to close and reopen the file if you want to check for a rename. Something like the program below. HTH, Rob use strict; use warnings; use IO::Handle; autoflush STDOUT; use Fcntl qw(:seek); my $file = 'junk.txt'; my $pos; while (1) { open LOG, $file or die Cannot open $file, $!; seek LOG, $pos, SEEK_SET if defined $pos; print while LOG; $pos = tell LOG; close LOG; sleep 1; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: tail -f does not exit
Rob == Rob Dixon [EMAIL PROTECTED] writes: [...] Rob You need to close and reopen the file if you want to check for a Rob rename. Something like the program below. [...] Tx, Rob, I'll give feedback soon here! -- Claude -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: tail + count
if (!$ARGV[0]){ die(you've forgotten to enter the file name\n); } if (!$ARGV[1]) { $n = 9; # output 10 rows by default } else { $n = $ARGV[1]-1; } what if the user enters, script.pl 8 ??? This wil try to open a file 8 and dump last 9 lines of it. if( $#ARGV != 2){ print usage.;exit 1;} unless ( -f $ARGV[0] ){ print bad file name ;exit 1;} $n = $ARGV[1]-1 || 9; - Original Message - From: Mrtlc [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, January 10, 2003 11:52 PM Subject: tail + count I wrote the following script as a windows tail + count function, but it's too slow if the input is huge, how can it be improved? if (!$ARGV[0]){ die(you've forgotten to enter the file name\n); } if (!$ARGV[1]) { $n = 9; # output 10 rows by default } else { $n = $ARGV[1]-1; } open IN, $ARGV[0] or die; @_ = IN; foreach my $i(($#_-$n)..$#_){ print $_[$i]; } close IN; $i = $#_+1; printf ---\nTotal # of rows: $i\n; Thanks, Stan L -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: tail + count
Mrtlc wrote: I wrote the following script as a windows tail + count function, but it's too slow if the input is huge, how can it be improved? if (!$ARGV[0]){ die(you've forgotten to enter the file name\n); } if (!$ARGV[1]) { $n = 9; # output 10 rows by default } else { $n = $ARGV[1]-1; } open IN, $ARGV[0] or die; @_ = IN; foreach my $i(($#_-$n)..$#_){ print $_[$i]; } close IN; $i = $#_+1; printf ---\nTotal # of rows: $i\n; A more Perlish way to do it: use warnings; use strict; @ARGV == 2 and my $n = pop || 10; $n--; @ARGV or die you've forgotten to enter the file name\n; my @tail; while ( ) { push @tail, $_; shift @tail if @tail $n; } print @tail; print ---\nTotal # of rows: $.\n; __END__ John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: tail + count
Hi John John W. Krahn [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... @ARGV == 2 and my $n = pop || 10; $n will be undefined if @ARGV != 2. Need something like: $n = @ARGV == 2 ? pop : 10; Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: tail -f with cgi
On Fri, 12 Jul 2002 08:24:55 -0400, [EMAIL PROTECTED] (Fliptop) wrote: Max Clark wrote: I am trying to write a cgi program to tail -f a log file. I have a perl script that will open and print the log file, however it closes as soon as it reads whatever is in the file at that particular time. How do I mimic tail -f functionality? try the qx// operator: my $tail = qx/tail -f filename.ext/; print $tail; #!/usr/bin/perl $filename='my.log'; open( IN, $filename ) or die Couldn't open $filename: $!; my $last; while ( IN ) { $last = $_; } print Last line is:\n$last\n; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: tail -f with cgi
- Original Message - From: Max Clark [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, July 11, 2002 6:43 PM Subject: tail -f with cgi Hi, Hello Max, I am trying to write a cgi program to tail -f a log file. I have a perl script that will open and print the log file, however it closes as soon as it reads whatever is in the file at that particular time. How do I mimic tail -f functionality? This is a quick snippet of code I threw together to view file info on a win32 platform from the command line, that works on *nix as well. It should be easy enough to convert to use from within a program. use strict; print \n\n; my $size; open(F,$ARGV[0]) or die Can't open $ARGV[0]: $!\n; while(F) { $size++; } close(F); my ($s1,$s2)=((stat($ARGV[0]))[7],''); for(;;){ do { select(undef,undef,undef,0.25); $s2=(stat($ARGV[0]))[7]; } until($s1!=$s2); $s1=$s2; open(F,$ARGV[0]) or die Can't open $ARGV[0]: $!\n; my $row; while(F) { $row++; print $row: $_ if($size $row); } $size=$row; close(F); } Shawn Thanks, Max -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: tail -f with cgi
-Original Message- From: Max Clark [mailto:[EMAIL PROTECTED]] Sent: Thursday, July 11, 2002 7:44 PM To: '[EMAIL PROTECTED]' Subject: tail -f with cgi Hi, I am trying to write a cgi program to tail -f a log file. I have a perl script that will open and print the log file, however it closes as soon as it reads whatever is in the file at that particular time. How do I mimic tail -f functionality? CPAN has a File::Tail module. But a CGI script isn't designed to be long-running like this. The web server will eventually time out the request and kill your script. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: tail -f with cgi
Alright, what's a tail -f? -Original Message- From: Bob Showalter [mailto:[EMAIL PROTECTED]] Sent: Friday, July 12, 2002 11:09 AM To: 'Max Clark'; '[EMAIL PROTECTED]' Subject: RE: tail -f with cgi -Original Message- From: Max Clark [mailto:[EMAIL PROTECTED]] Sent: Thursday, July 11, 2002 7:44 PM To: '[EMAIL PROTECTED]' Subject: tail -f with cgi Hi, I am trying to write a cgi program to tail -f a log file. I have a perl script that will open and print the log file, however it closes as soon as it reads whatever is in the file at that particular time. How do I mimic tail -f functionality? CPAN has a File::Tail module. But a CGI script isn't designed to be long-running like this. The web server will eventually time out the request and kill your script. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: tail -f with cgi
- Original Message - From: Bob Showalter [EMAIL PROTECTED] To: 'Max Clark' [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Friday, July 12, 2002 11:08 AM Subject: RE: tail -f with cgi -Original Message- From: Max Clark [mailto:[EMAIL PROTECTED]] Sent: Thursday, July 11, 2002 7:44 PM To: '[EMAIL PROTECTED]' Subject: tail -f with cgi Hi, I am trying to write a cgi program to tail -f a log file. I have a perl script that will open and print the log file, however it closes as soon as it reads whatever is in the file at that particular time. How do I mimic tail -f functionality? CPAN has a File::Tail module. But a CGI script isn't designed to be long-running like this. The web server will eventually time out the request and kill your script. I don't think he was running from the web server, but will the shell time out in the same fashion? Shawn -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: tail -f with cgi
-Original Message- From: Shawn [mailto:[EMAIL PROTECTED]] Sent: Friday, July 12, 2002 12:29 PM To: Bob Showalter; 'Max Clark'; [EMAIL PROTECTED] Subject: Re: tail -f with cgi - Original Message - From: Bob Showalter [EMAIL PROTECTED] To: 'Max Clark' [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Friday, July 12, 2002 11:08 AM Subject: RE: tail -f with cgi -Original Message- From: Max Clark [mailto:[EMAIL PROTECTED]] Sent: Thursday, July 11, 2002 7:44 PM To: '[EMAIL PROTECTED]' Subject: tail -f with cgi Hi, I am trying to write a cgi program to tail -f a log file. I have a perl script that will open and print the log file, however it closes as soon as it reads whatever is in the file at that particular time. How do I mimic tail -f functionality? CPAN has a File::Tail module. But a CGI script isn't designed to be long-running like this. The web server will eventually time out the request and kill your script. I don't think he was running from the web server, I'm just going off the phrase I am trying to write a cgi program to tail -f a log file. but will the shell time out in the same fashion? No. It continues to tail the file until interrupted (^C or whatever). -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: tail -f with cgi
I found the file::tail module on cpan... #!/usr/bin/perl -w use File::Tail; my $log = /usr/local/apache2/logs/access_log; $file=File::Tail-new ( name=$log, interval=2, maxinterval=10 ); while (defined($line=$file-read)) { print $line; } It does exactly what I need. I can't seem to get this to work correctly through cgi. Any ideas? Thanks, Max -Original Message- From: Max Clark [mailto:[EMAIL PROTECTED]] Sent: Thursday, July 11, 2002 4:44 PM To: '[EMAIL PROTECTED]' Subject: tail -f with cgi Hi, I am trying to write a cgi program to tail -f a log file. I have a perl script that will open and print the log file, however it closes as soon as it reads whatever is in the file at that particular time. How do I mimic tail -f functionality? Thanks, Max -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Tail call optimization
From: Tagore Smith [EMAIL PROTECTED] Thanks :). That's great. I don't use goto much, except for a couple of very specific situations, so I hadn't read the docs for goto. It seems I missed a very interesting beast in goto NAME. In fact , I sent a friend of mine some code recently that would have been improved in one place by its use. But I still have a question :). As I understand it when tail-call optimization is done automatically there are two advantages. One is that the stack doesn't grow out of control, so that you don't have to worry about blowing it up if you recurse very deeply. The other is that the overhead of successive calls is eliminated. In theory (but, I think, often not in practice) the optimized routine should be as efficient as the equivalent iterative routine. Using goto name has the first advantage, but does it have the second? That is, does goto NAME have the same overhead as a normal call? No it does not. On the other hand most usualy it will still be slower that rewriting the code to iterative. That is ... if you assign to @_ before the goto NAME and later extract the values from it to lexical variables. Besides ... Benchmark.pm is your friend. Try it out :-) Does anyone write this kind of recursive function regularly? I mean, is it idiomatic in the more rarefied Perl circles? Or is it better (from a style point of view) to use iterative constructs even in places where recursion is more natural, but tail-call optimization would be required? I think next to noone uses this kind of recursive functions. I think I am crazy and I do have a functional language background, but still I would not use it. I know about it, but it would not come to my mind when coding normaly. In any case ... if you ever use this, please comment it heavily. Jenda === [EMAIL PROTECTED] == http://Jenda.Krynicky.cz == There is a reason for living. There must be. I've seen it somewhere. It's just that in the mess on my table ... and in my brain I can't find it. --- me -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Tail call optimization
Jenda Krynicky wrote: I believe they meant goto NAME. illuminating example snipped This way perl doesn't create a new record in the call stack every time you call the _fib(). As you can see if you comment out the return in fib_() and remove the comment from croak ... and use Carp;. (die() with stack print). See perldoc -f goto Thanks :). That's great. I don't use goto much, except for a couple of very specific situations, so I hadn't read the docs for goto. It seems I missed a very interesting beast in goto NAME. In fact , I sent a friend of mine some code recently that would have been improved in one place by its use. But I still have a question :). As I understand it when tail-call optimization is done automatically there are two advantages. One is that the stack doesn't grow out of control, so that you don't have to worry about blowing it up if you recurse very deeply. The other is that the overhead of successive calls is eliminated. In theory (but, I think, often not in practice) the optimized routine should be as efficient as the equivalent iterative routine. Using goto name has the first advantage, but does it have the second? That is, does goto NAME have the same overhead as a normal call? Does anyone write this kind of recursive function regularly? I mean, is it idiomatic in the more rarefied Perl circles? Or is it better (from a style point of view) to use iterative constructs even in places where recursion is more natural, but tail-call optimization would be required? Thanks Tagore Smith -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Tail call optimization
From: Tagore Smith [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject:Tail call optimization Date sent: Tue, 28 May 2002 23:42:30 -0400 I came across this statement on the web: Perl ... supports the tail-call optimization (although you have to do it by hand). I was wondering if someone could give me an example of this. I know what tail-call optimization means, and how to write tail-recursive functions, in Lisp- just not sure how you would do this in Perl. Thanks Tagore Smith I believe they meant goto NAME. #use Carp; sub fib { die fib() defined only on positive numbers if ($_[0] 0); return $_[0] if ($_[0] 3); fib_(int($_[0]-2), 2, 1); } sub fib_ { print @_\n; my ($rest, $n, $n_1) = @_; if ($rest = 1) { # croak $n + $n_1 return $n + $n_1 } else { # fib_( $rest - 1, $n + $n_1, $n); # is replaced by @_ = ( $rest - 1, $n + $n_1, $n); goto fib_; } } This way perl doesn't create a new record in the call stack every time you call the _fib(). As you can see if you comment out the return in fib_() and remove the comment from croak ... and use Carp;. (die() with stack print). See perldoc -f goto Jenda === [EMAIL PROTECTED] == http://Jenda.Krynicky.cz == There is a reason for living. There must be. I've seen it somewhere. It's just that in the mess on my table ... and in my brain I can't find it. --- me -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: TAIL
grep in perl doesn't work exactly same way as grep in *nix. It functions differently in perl, and has better uses in perl that the *nix's grep can't do. perldoc -f grep -Original Message- From: James Taylor [mailto:[EMAIL PROTECTED]] Sent: Wednesday, March 13, 2002 3:06 PM To: [EMAIL PROTECTED] Subject: TAIL is there a perl function equivalent to the *nix command 'tail'? I don't mean like, a workaround through loops that will produce the same sort of result, just a function. Also, what are the benefits of using the function grep? Doing a system call to grep seems to run faster than the perl function! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: TAIL
Is there a perl function equivalent to the *nix command 'tail'? Here is a basic Perl implementation of tail: #!/usr/bin/perl @a=;print@a[-10,-1]; IIRC there is a shorter way to do it, but that'd mean going back over the FWP mailing list archives. I don't mean like, a workaround through loops that will produce the same sort of result, just a function. sub tail (\@) { my $array = shift; return @$array[-10,-1]; } Also, what are the benefits of using the function grep? Lots, cross platform, NFA engine (not DFA) hence backtracking and lookahead/lookbehinds etc. Doing a system call to grep seems to run faster than the perl function! It probably will, since the system grep is built for absolute speed in the small task it does. Perl is a little more general, and the grep patterns are just as powerful as any other regex in Perl. Platform independence springs to mind too... and also if you already have data in memory (in Perl) writing it out to disk is a pain that none of us bother with. Jonathan Paton __ Do You Yahoo!? Everything you'll ever need on one web page from News and Sport to Email and Music Charts http://uk.my.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: TAIL
I suggest: File::Tail if you are wanting to something like tail -f, though. Works like a champ. - Jim At 06:09 03.14.2002 +, Jonathan E. Paton wrote: Is there a perl function equivalent to the *nix command 'tail'? Here is a basic Perl implementation of tail: #!/usr/bin/perl @a=;print@a[-10,-1]; IIRC there is a shorter way to do it, but that'd mean going back over the FWP mailing list archives. I don't mean like, a workaround through loops that will produce the same sort of result, just a function. sub tail (\@) { my $array = shift; return @$array[-10,-1]; } Also, what are the benefits of using the function grep? Lots, cross platform, NFA engine (not DFA) hence backtracking and lookahead/lookbehinds etc. Doing a system call to grep seems to run faster than the perl function! It probably will, since the system grep is built for absolute speed in the small task it does. Perl is a little more general, and the grep patterns are just as powerful as any other regex in Perl. Platform independence springs to mind too... and also if you already have data in memory (in Perl) writing it out to disk is a pain that none of us bother with. Jonathan Paton __ Do You Yahoo!? Everything you'll ever need on one web page from News and Sport to Email and Music Charts http://uk.my.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -BEGIN PGP PUBLIC KEY BLOCK- Version: GnuPG v1.0.6 (MingW32) Comment: For info see http://www.gnupg.org - Jim Philosophy is for those who have nothing better to do than wonder why philosophy is for those who have nothing better to do than... mQGiBDxAonQRBACx+sz63XIeo5uTzc5n3Elf7Y13VVZGIM8Pilp3LpBu70/nGQPu anKYDB3aa1U5cfl+cTK5lOtUxN7Fu0a2Uv0ApIlC1qA8CjDZqlu7PDETFTVrpfGZ 007BHO+y2Y0bVsaMPXdnhbi0LAFSIkNYRhyzNWbAkeMsgA+i2k9hcnhvVwCgor7P nflXu7xWN9aWt3RJBzqdUR0EAK/1obJFUKQSK39cKTMPQ4u2UPflbS5dJ871naG5 xBAlQAjHAXT+f/fXE2ezrSyoQnlOD4kVbPN3gB5UT5mWoylPuf5W7WmupthVzUUN IsPDbmAT0YOwgALCfJVS+PrPCC8opmZhTjQBwgxCSY9MWULlzN3X2EEDqWIxluYb o5W/BACgHA+aFOO5F03QZBBScWn9YBS1ZH3sSlkQEK5RiwGXLmHJacOjn660SbOE MEKPDLDDJu/vt1fb3VRLc/fPB3aB7fi4XagfobaHbID9rx55slLhD94Q+5JuJSfg DyJ+vVSA1k+9/SynflPl0QY5zt0xSM+0CBg9mBg2bPyuGsDwXLQ5SmltIENvbm5l ciAoTmV3IEdQRyBLZXkgZm9yIFNuYWZ1WCkgPGpjb25uZXJAZW50ZXJpdC5jb20+ iFcEExECABcFAjxAonQFCwcKAwQDFQMCAxYCAQIXgAAKCRDmnFh04+r7ZdFiAKCh t8Vq7ZT6qvh9Dzn0lzZXRM4gywCfSLU/H5UHX7ZoxapfDs9pLxEEZeO5Ag0EPECj chAIAIsdwiPqW8IsumvpXu59qkfsi4H2nofxvbhMDiapEhgloydehNQOEiHwC/O1 a06PjUmNRLRdK88kjy99R84ILbWUJZUclQB2LcjlttnrIG/FzCMxoLTKOeOCJk8N ONswBdJdcf/XqbWJBTs/MXeNf4rmShYi6WJ5+jc1IE5PXGf4SR/9bz2r+/GESlrX tAoNtWl5a/NUxb6b0hR6zU9Y6oO1vpDDJNbcV9mafdYhsvoFYdD2c6JF+JoN+FHR tEP3k6leYwQ5P0kuUQNgWdWNWZfBq1tQDBfhg1/AV0JBzamyJfd0prFmtUEemKx4 haDsOoT4gLSPNTqSsyDt6TNLtGMAAwUIAINeot1FVpree5bvhy3xL+Pr1UGb++DM b8Qeer6ERkVQNx7YoU8hfpqOwvEQMyfb9s6HPfSWRUfQRF+g+9ohPgYkH+1nqH3V PtGSw1kgLOqxZQTVPEcAMhSflt9LSJETIQQByKKh1e5RvOuApwBFmQq3syRhzqv/ j2b6t3IqAB9WR5TnoYkdUtTWM9MGubiFl5B9uH5EHWAlFF8h760U7Xp9m1J3qTyH EJqjfGj2SP2DK5cisuWOWdPy5aSqT7ZKrcKeSTDUyiHclI1ygFHue8oO0HXqrs+k KjFdRqIKnzfY9gW/b/6gLHhBDV6BoA9w6+1Y9egOByRcVonE8zY/xMeIRgQYEQIA BgUCPECjcgAKCRDmnFh04+r7ZcyDAJ4ogYX7W4u8g+QJsksyL4Ld+dObCwCfU7hB 7I3ZgTsYwP6mr5RPjkH5PG8= =QOu8 -END PGP PUBLIC KEY BLOCK- __END__ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]