Re: Passing hash ref [ was Re: finding head and tail in data structure

2013-01-11 Thread budi pearl
Hi Jim,

On Fri, Jan 11, 2013 at 1:54 PM, Jim Gibson jimsgib...@gmail.com wrote:

 OR

 print_path( $id, $routes-{$id} );
 ...
 sub print_path
 {
 ...
 while( my ($start, $end) = each %$edges ) {


This is what i want, thank you!
Apparently  passing $routes-{$id} is my earlier issue.


--budi


finding head and tail in data structure

2013-01-10 Thread budi perl
Hi,

I have this following hash:

#!/usr/bin/perl
#
use strict;
use Data::Dumper;

my %MYROUTES = (
ROUTE-252 = {
#  src = dest
   427 = ABEP,
   ABEP = 441,
   441 = 427,
   427 = 444,
   444 = MGWQ,
   MGWQ = CDEF
},

ROUTE-432 = {
   AAA = BBB,
   BBB = CCC,
   CCC = DDD,
   XXX = YYY,
   YYY = ZZZ
}
);

print Dumper %MYROUTES;

__END__

Expected results:

ROUTE-252: 427 - ABEP - 441 - 427 - 444 - MGWQ - CDEF
ROUTE-432: Error: can not follow the route!

or if possible can be more specific on how many link founds:
Error, some path is missing:
ROUTE-432: AAA - BBB - CCC -DDD
ROUTE-432: XXX - YYY -ZZZ

I put data in order for brevity, actual data may not.

Can someone shed some light how to find head then follow the path as above?
Many thanks.

--budi


Re: finding head and tail in data structure

2013-01-10 Thread David Precious
On Thu, 10 Jan 2013 17:01:43 +0700
budi perl budipe...@gmail.com wrote:

 Hi,
 
 I have this following hash:
 
 #!/usr/bin/perl
 #
 use strict;
 use Data::Dumper;
 
 my %MYROUTES = (
 ROUTE-252 = {
 #  src = dest
427 = ABEP,
ABEP = 441,
441 = 427,
427 = 444,
444 = MGWQ,
MGWQ = CDEF
 },

You can't have the same hash key twice; you've duplicated 427 there.

Also, you don't need to quote the left side of a fat comma, so you can
just as easily say e.g. ABEP = 441.


 Expected results:
 
 ROUTE-252: 427 - ABEP - 441 - 427 - 444 - MGWQ - CDEF
 ROUTE-432: Error: can not follow the route!
[...]
 Can someone shed some light how to find head then follow the path
 as above? Many thanks.

What have you tried so far?  What have you got stuck on?

How do you know where the start of the route is?  (Rememeber, hash keys
will come back in essentially random order; so, where do you start?)

If you need to start following from the first key you defined, you'll
need to either have an arrayref of hashrefs to iterate over, or store
the starting point as well as the routes.


-- 
David Precious (bigpresh) dav...@preshweb.co.uk
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedinwww.preshweb.co.uk/facebook
www.preshweb.co.uk/cpanwww.preshweb.co.uk/github



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: finding head and tail in data structure

2013-01-10 Thread budi pearl
On Thu, Jan 10, 2013 at 5:19 PM, David Precious dav...@preshweb.co.ukwrote:

 On Thu, 10 Jan 2013 17:01:43 +0700
 budi perl budipe...@gmail.com wrote:
 You can't have the same hash key twice; you've duplicated 427 there.

 Also, you don't need to quote the left side of a fat comma, so you can
 just as easily say e.g. ABEP = 441.


  Expected results:
 
  ROUTE-252: 427 - ABEP - 441 - 427 - 444 - MGWQ - CDEF
  ROUTE-432: Error: can not follow the route!
 [...]
  Can someone shed some light how to find head then follow the path
  as above? Many thanks.

 What have you tried so far?  What have you got stuck on?

 How do you know where the start of the route is?  (Rememeber, hash keys
 will come back in essentially random order; so, where do you start?)

 If you need to start following from the first key you defined, you'll
 need to either have an arrayref of hashrefs to iterate over, or store
 the starting point as well as the routes.


Here it is:

#!/usr/bin/perl
#
use strict;
use Data::Dumper;

my %MYROUTES = (
ROUTE-252 = {
#  src = dest
   CX77 = ABEP,
   ABEP = 441,
   441 = 427,
   427 = 444,
   444 = MGWQ,
   MGWQ = CDEF
},

ROUTE-432 = {
   AAA = BBB,
   BBB = CCC,
   CCC = DDD,
   DDD = EEE,
   EEE = FFF,
   XXX = YYY,
   YYY = ZZZ,
}
);

#print Dumper %MYROUTES;

foreach my $id (keys %MYROUTES) {
print $id \n;

# finding the starting point
my %found = ();
my $node = \%{$MYROUTES{$id}};
foreach my $src (keys %{$node}) {
print \t$src : ;
$found{$src} = 0;
foreach my $key (keys %{$node}) {
print  $node-{$key} ;
$found{$src}++ if $src eq $node-{$key};
}
print \n;
}

# trace the path
my $sum = 0;
foreach(keys %found) {
print FOUND: $_ - $found{$_} ;
$sum += $found{$_};
}
print \n;

my $size = scalar keys %found;

print size: $size , sum: $sum\n;
if ($sum == ($size - 1)) {
print $id: has complete path\n;
} else {
my $total_route =  $size - $sum;
print $id: has missing path, got $total_route different routes\n;
}
}

# should do recursively to print the route?
sub get_next {
my ($s, $n) = @_;
if ($n-{$s}) {
return 1;
} else {
return 0;
}
}

[budi@diavel bin]$ ./printroute.pl
ROUTE-252
427 :  444  441  ABEP  CDEF  MGWQ  427
ABEP :  444  441  ABEP  CDEF  MGWQ  427
CX77 :  444  441  ABEP  CDEF  MGWQ  427
MGWQ :  444  441  ABEP  CDEF  MGWQ  427
444 :  444  441  ABEP  CDEF  MGWQ  427
441 :  444  441  ABEP  CDEF  MGWQ  427
FOUND: CX77 - 0 FOUND: ABEP - 1 FOUND: 427 - 1 FOUND: 444 - 1 FOUND:
MGWQ - 1 FOUND: 441 - 1
size: 6 , sum: 5
ROUTE-252: has complete path
ROUTE-432
XXX :  YYY  DDD  CCC  FFF  EEE  ZZZ  BBB
CCC :  YYY  DDD  CCC  FFF  EEE  ZZZ  BBB
BBB :  YYY  DDD  CCC  FFF  EEE  ZZZ  BBB
EEE :  YYY  DDD  CCC  FFF  EEE  ZZZ  BBB
DDD :  YYY  DDD  CCC  FFF  EEE  ZZZ  BBB
YYY :  YYY  DDD  CCC  FFF  EEE  ZZZ  BBB
AAA :  YYY  DDD  CCC  FFF  EEE  ZZZ  BBB
FOUND: XXX - 0 FOUND: BBB - 1 FOUND: CCC - 1 FOUND: DDD - 1 FOUND: EEE
- 1 FOUND: AAA - 0 FOUND: YYY - 1
size: 7 , sum: 5
ROUTE-432: has missing path, got 2 different routes

Looks ok to me, but ugly and need a way to print the full path.
Thanks!


--budi


Re: finding head and tail in data structure

2013-01-10 Thread Rob Dixon

On 10/01/2013 10:01, budi perl wrote:

Hi,

I have this following hash:

#!/usr/bin/perl
#
use strict;
use Data::Dumper;

my %MYROUTES = (
 ROUTE-252 = {
 #  src = dest
427 = ABEP,
ABEP = 441,
441 = 427,
427 = 444,
444 = MGWQ,
MGWQ = CDEF
 },

 ROUTE-432 = {
AAA = BBB,
BBB = CCC,
CCC = DDD,
XXX = YYY,
YYY = ZZZ
 }
);

print Dumper %MYROUTES;

__END__

Expected results:

ROUTE-252: 427 - ABEP - 441 - 427 - 444 - MGWQ - CDEF
ROUTE-432: Error: can not follow the route!

or if possible can be more specific on how many link founds:
Error, some path is missing:
ROUTE-432: AAA - BBB - CCC -DDD
ROUTE-432: XXX - YYY -ZZZ

I put data in order for brevity, actual data may not.

Can someone shed some light how to find head then follow the path as above?


This calls for a proper module that has been thoroughly tested. The
program below uses Graph::Directed. Beware that it does no checks for
things like cyclic links, but it does print the path to /all/ end points
starting at each source.

HTH,

Rob


use v5.10;
use warnings;

use Graph::Directed;

my %routes = (
  ROUTE-252 = { 427 = 444, 441 = 427, 444 = MGWQ, ABEP = 441, 
MGWQ = CDEF },
  ROUTE-432 = { AAA = BBB, BBB = CCC, CCC = DDD, XXX = 
YYY, YYY = ZZZ },

);

while (my ($label, $edges) = each %routes) {

  my $graph = Graph::Directed-new;

  while (my ($start, $end) = each %$edges) {
$graph-add_edge($start, $end);
  }

  my @sinks = $graph-sink_vertices;
  for my $source ($graph-source_vertices) {
for my $sink (grep $graph-is_sink_vertex($_), 
$graph-all_successors($source)) {

  say $label: , join ' - ', $graph-path_vertices($source, $sink);
}
  }
}

**output**

ROUTE-252: ABEP - 441 - 427 - 444 - MGWQ - CDEF
ROUTE-432: AAA - BBB - CCC - DDD
ROUTE-432: XXX - YYY - ZZZ


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: finding head and tail in data structure

2013-01-10 Thread budi pearl
Hi Rob,

This works and looks much more simpler. Thanks, i love it.

--budhi

On Fri, Jan 11, 2013 at 1:00 AM, Rob Dixon rob.di...@gmx.com wrote:

 On 10/01/2013 10:01, budi perl wrote:

 Hi,

 I have this following hash:

 #!/usr/bin/perl
 #
 use strict;
 use Data::Dumper;

 my %MYROUTES = (
  ROUTE-252 = {
  #  src = dest
 427 = ABEP,
 ABEP = 441,
 441 = 427,
 427 = 444,
 444 = MGWQ,
 MGWQ = CDEF
  },

  ROUTE-432 = {
 AAA = BBB,
 BBB = CCC,
 CCC = DDD,
 XXX = YYY,
 YYY = ZZZ
  }
 );

 print Dumper %MYROUTES;

 __END__

 Expected results:

 ROUTE-252: 427 - ABEP - 441 - 427 - 444 - MGWQ - CDEF
 ROUTE-432: Error: can not follow the route!

 or if possible can be more specific on how many link founds:
 Error, some path is missing:
 ROUTE-432: AAA - BBB - CCC -DDD
 ROUTE-432: XXX - YYY -ZZZ

 I put data in order for brevity, actual data may not.

 Can someone shed some light how to find head then follow the path as
 above?


 This calls for a proper module that has been thoroughly tested. The
 program below uses Graph::Directed. Beware that it does no checks for
 things like cyclic links, but it does print the path to /all/ end points
 starting at each source.

 HTH,

 Rob


 use v5.10;
 use warnings;

 use Graph::Directed;

 my %routes = (
   ROUTE-252 = { 427 = 444, 441 = 427, 444 = MGWQ, ABEP = 441,
 MGWQ = CDEF },
   ROUTE-432 = { AAA = BBB, BBB = CCC, CCC = DDD, XXX = YYY,
 YYY = ZZZ },
 );

 while (my ($label, $edges) = each %routes) {

   my $graph = Graph::Directed-new;

   while (my ($start, $end) = each %$edges) {
 $graph-add_edge($start, $end);
   }

   my @sinks = $graph-sink_vertices;
   for my $source ($graph-source_vertices) {
 for my $sink (grep $graph-is_sink_vertex($_), $graph-all_successors($
 **source)) {
   say $label: , join ' - ', $graph-path_vertices($source, $sink);
 }
   }
 }

 **output**

 ROUTE-252: ABEP - 441 - 427 - 444 - MGWQ - CDEF

 ROUTE-432: AAA - BBB - CCC - DDD
 ROUTE-432: XXX - YYY - ZZZ




Passing hash ref [ was Re: finding head and tail in data structure

2013-01-10 Thread budi pearl
Hi All,

I would like to pass hash: %{$routes{ROUTE-252}} instead of %routes but
got this error:

[budi@dev bin]$ ./print_path.pl
Type of arg 1 to each must be hash (not hash element) at
./print_path.plline 38, near }) 
Execution of ./print_path.pl aborted due to compilation errors.


#use strict;
use Graph::Directed;
use Data::Dumper;

my %routes = (
ROUTE-252 = {
#  src = dest
   CX77 = ABEP,
   ABEP = 441,
   441 = 427,
   427 = 444,
   444 = MGWQ,
   MGWQ = CDEF
},

ROUTE-432 = {
   AAA = BBB,
   BBB = CCC,
   CCC = DDD,
   DDD = EEE,
   EEE = FFF,
   XXX = YYY,
   YYY = ZZZ,
}
);

my $id = ROUTE-252;
print Dumper $routes{$id};

print_path($id, \%{$routes{$id}});

sub print_path {
my ($label, $edges) = @_;
my $graph = Graph::Directed-new;

while (my ($start, $end) = each $edges{$label}) {
#while (my ($start, $end) = each %{$routes{$label}}) {
$graph-add_edge($start, $end);
}

my @sinks = $graph-sink_vertices;
for my $source ($graph-source_vertices) {
for my $sink (grep $graph-is_sink_vertex($_),
$graph-all_successors($source)) {
print $label: , join ' - ', $graph-path_vertices($source,
$sink);
print \n;
}
}


}


Re: Passing hash ref [ was Re: finding head and tail in data structure

2013-01-10 Thread Shawn H Corey
On Fri, 11 Jan 2013 10:33:02 +0700
budi pearl budipe...@gmail.com wrote:

 my $id = ROUTE-252;
 print Dumper $routes{$id};
 
 print_path($id, \%{$routes{$id}});

I think you want:

  print_path( $id, $routes{$id} );


-- 
Don't stop where the ink does.
Shawn

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Passing hash ref [ was Re: finding head and tail in data structure

2013-01-10 Thread budi pearl
Hi Shawn,

When trying to accessed inside subroutine , i got:

Type of arg 1 to each must be hash (not hash element) at
./print_path.plline 41, near }) 
Execution of ./print_path.pl aborted due to compilation errors.


this is work:
  while (my ($start, $end) = each %{$routes{$label}}) {

but this not:
while (my ($start, $end) = each %{$edges{$label}}) {

Thanks.

--budhi



On Fri, Jan 11, 2013 at 10:47 AM, Shawn H Corey shawnhco...@gmail.comwrote:

 On Fri, 11 Jan 2013 10:33:02 +0700
 budi pearl budipe...@gmail.com wrote:

  my $id = ROUTE-252;
  print Dumper $routes{$id};
 
  print_path($id, \%{$routes{$id}});

 I think you want:

   print_path( $id, $routes{$id} );


 --
 Don't stop where the ink does.
 Shawn

 --
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/





Re: Passing hash ref [ was Re: finding head and tail in data structure

2013-01-10 Thread budi pearl
On Fri, Jan 11, 2013 at 11:05 AM, budi pearl budipe...@gmail.com wrote:

 Hi Shawn,

 When trying to accessed inside subroutine , i got:

 Type of arg 1 to each must be hash (not hash element) at ./print_path.plline 
 41, near }) 

 Execution of ./print_path.pl aborted due to compilation errors.


 this is work:
   while (my ($start, $end) = each %{$routes{$label}}) {

 but this not:
 while (my ($start, $end) = each %{$edges{$label}}) {



This will work, but this will pass %routes instead of %routes{$id}. Or it
should be that way?

my $hroute = \%routes;
print_path($id, $hroute);

...
while (my ($start, $end) = each %{$edges-{$label}}) {


Re: Passing hash ref [ was Re: finding head and tail in data structure

2013-01-10 Thread Jim Gibson

On Jan 10, 2013, at 7:33 PM, budi pearl wrote:

 Hi All,
 
 I would like to pass hash: %{$routes{ROUTE-252}} instead of %routes but
 got this error:
 
 [budi@dev bin]$ ./print_path.pl
 Type of arg 1 to each must be hash (not hash element) at
 ./print_path.plline 38, near }) 
 Execution of ./print_path.pl aborted due to compilation errors.
 
 
 #use strict;

If you had not commented out this line, Perl would have told you what the 
problem is.

 use Graph::Directed;
 use Data::Dumper;
 
 my %routes = (
ROUTE-252 = {
#  src = dest
   CX77 = ABEP,
   ABEP = 441,
   441 = 427,
   427 = 444,
   444 = MGWQ,
   MGWQ = CDEF
},
 
ROUTE-432 = {
   AAA = BBB,
   BBB = CCC,
   CCC = DDD,
   DDD = EEE,
   EEE = FFF,
   XXX = YYY,
   YYY = ZZZ,
}
 );
 
 my $id = ROUTE-252;
 print Dumper $routes{$id};
 
 print_path($id, \%{$routes{$id}});

You are passing a reference to the inner hash, but you are doing it by first 
de-referencing, then referencing the inner hash. This can be simply:

print_path( $id, $routes{$id} );

 
 sub print_path {
my ($label, $edges) = @_;

You are storing the hash reference in the scalar variable $edges. This value is 
$routes{'ROUTE-252'}.

my $graph = Graph::Directed-new;
 
while (my ($start, $end) = each $edges{$label}) {

There are two problems here:

1. $edges{$label} is a member of the hash %edges, but you have not declared or 
defined %edges in your subroutine.

2. You are trying to access the member $route{'ROUTE-252'}-{'ROUTE-252'}. In 
other words, you are indexing twice. You should either pass the top-level hash 
and index in the subroutine, or pass the indexed hash member and do not index 
in the subroutine:

print_path( $id, \%routes );
...
sub print_path
{
...
while( my ($start, $end) = each %{$edges-{$label}} ) {


OR

print_path( $id, $routes-{$id} );
...
sub print_path
{
...
while( my ($start, $end) = each %$edges ) {


 #while (my ($start, $end) = each %{$routes{$label}}) {
$graph-add_edge($start, $end);
}
 
my @sinks = $graph-sink_vertices;
for my $source ($graph-source_vertices) {
for my $sink (grep $graph-is_sink_vertex($_),
 $graph-all_successors($source)) {
print $label: , join ' - ', $graph-path_vertices($source,
 $sink);
print \n;
}
}
 
 
 }


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




More on perl like tail

2009-10-24 Thread Harry Putnam
Sorry about being tricky with what was an older thread.  But I suspect
that thread died... and no one noticed there was an unaswered question
still there.

Shawn C originally suggested I use File::Tail.  There was a short
exchange about why and then I began trying to use File::Tail but
haven't been successful with it.  The details are below:

 Shawn H Corey shawnhco...@gmail.com writes:

   open(FILE,./named-pipe) or die Can't Open ./named-pipe: $!;
   while(FILE){
 print;
 if(eof){
   sleep 2;
   seek (FILE,0,1);
 }
   } 
 
 It seems at least to survive repeated restarts of system logger.
 
 If I write my script based on this code... what I'd be adding would be
 code to get 1 or 2 rgx from the cmdline, then write the hits to
 various files.

 [...]

 In the general case, modules usually take care of special cases that you
  may not be aware of.  That makes them the preferred method of solving a
 problem.

 In this case, the above code is a kludge.  You can tell this because the
 program sleeps, rather than waiting on input.  When a program does
 something to emulate what it really should be doing, it introduces code
 that may not work in all cases.

I'm having trouble with File::Tail... or more likely the way I'm
trying to use it is wrongly setup.

But first about that sleep comment.  As I read a little of File::Tail
and its very likely I'm not really understanding what I'm reading but,
it appears to be saying that it `sleeps' at times...  the times are
a little more sophisticated... but none the less sleep.

Now the problem.

I've taken the first examples in the perldoc File::Tail output:
  (http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm)

  use File::Tail;
  $file=File::Tail-new(/some/log/file);
  while (defined($line=$file-read)) {
  print $line;
  }

And tied to make it work for my case (/var/adm/slpipe in the script is a
named-pipe that the system logger reads into):

cat fltr_sl

  #!/usr/local/bin/perl
  
  use strict;
  use warnings;
  use File::Tail;

  my ($file,$line);
  my $fname_in = /var/adm/slpipe;

  $file=File::Tail-new($fname_in);
  while (defined($line=$file-read)) {
 print $line;
  }  

When I run it, it doesn't show any errors, and appears to be waiting
on the pipe.

But no data ever comes out.  I used the same test sequence as for the
earlier script that didn't use File::Tail (posted earlier in this
thread).   The sequence is.

 1) start the script `fltr_sl' shown above
 2) To make sure if data is flowing thru the pipe start
start (in a different xterm) tail -f slpipe too.
 2) kill -HUP the system logger.
 3) Ensure some data is flowing thru the named-pipe by
running ssh r...@localhost from a user account

I see 7-8 lines output to the tail -f slpipe command, but nothing to
the perl script fltr_sl.

And as I write this message, quite a few more lines are appearing at
the `tail -f slpipe' cmd, but still nothing at my perl filter.

Have I made some serious mistake in my attempted usage of File::Tail? 


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: More on perl like tail

2009-10-24 Thread Jim Gibson

At 3:57 PM -0500 10/24/09, Harry Putnam wrote:

Sorry about being tricky with what was an older thread.  But I suspect
that thread died... and no one noticed there was an unaswered question
still there.



Or no one knew the answer.




Shawn C originally suggested I use File::Tail.  There was a short
exchange about why and then I began trying to use File::Tail but
haven't been successful with it.  The details are below:


 Shawn H Corey shawnhco...@gmail.com writes:


   open(FILE,./named-pipe) or die Can't Open ./named-pipe: $!;
   while(FILE){
 print;
 if(eof){
   sleep 2;
   seek (FILE,0,1);
 }
   }



I would modify the above a little. You are calling eof after each 
line, which is unnecessary. The input operator FILE will return 
undef whenever eof will return true, so calling eof is redundant. You 
can make this a little more efficient with something like the 
following (untested):


  while(1) {
while(FILE) {
  print;
}
sleep 1;
seek(FILE,0,1);
  }


  In this case, the above code is a kludge.  You can tell this because the

 program sleeps, rather than waiting on input.  When a program does
 something to emulate what it really should be doing, it introduces code
 that may not work in all cases.


I'm having trouble with File::Tail... or more likely the way I'm
trying to use it is wrongly setup.

But first about that sleep comment.  As I read a little of File::Tail
and its very likely I'm not really understanding what I'm reading but,
it appears to be saying that it `sleeps' at times...  the times are
a little more sophisticated... but none the less sleep.



I do not agree with that kludge comment. When waiting for slow 
events, you can either use polling or interrupts. Polling means 
continually sleeping and then checking if the event has occurred. The 
event in this case is additional data appearing at the end of the 
file. Interrupt would mean blocking until the event has occurred. 
That can be accomplished using the Unix select statement. In the 
general case, interrupts are more efficient than polling, because 
your process does not execute at all until the event occurs.


In this case, however, your process is waiting on another, slower 
process: the file writer. If you were to use a select statement, at 
the cost of increased complexity in your program, you would respond 
faster to new data. However, sleeping for 1 second and trying to read 
the file is not going to put a large load on your system. So the 
simple method using sleep will cost you less than one second every 
time you exhaust the file input in your reader process. If this is 
acceptable, then using sleep is OK.


File::Tail does have a select method to improve response, but I think 
that in most cases that is overkill. As you have pointed out, the 
normal use of File::Tail involves calling the sleep function.




Now the problem.

I've taken the first examples in the perldoc File::Tail output:
  (http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm)

  use File::Tail;
  $file=File::Tail-new(/some/log/file);
  while (defined($line=$file-read)) {
  print $line;
  }


That looks OK, and works for me on /var/log/system.log, although the 
delays are longer using File::Tail than Unix tail. Maybe you should 
try it on a normal file that you write to occasionally with another 
Perl program.



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: More on perl like tail

2009-10-24 Thread Harry Putnam
Jim Gibson jimsgib...@gmail.com writes:

 At 3:57 PM -0500 10/24/09, Harry Putnam wrote:
Sorry about being tricky with what was an older thread.  But I suspect
that thread died... and no one noticed there was an unaswered question
still there.

 Or no one knew the answer.

He he... unlikely here I think..

[...]

 I would modify the above a little. You are calling eof after each
 line, which is unnecessary. The input operator FILE will return
 undef whenever eof will return true, so calling eof is redundant. You
 can make this a little more efficient with something like the
 following (untested):

   while(1) {
 while(FILE) {
   print;
 }
 sleep 1;
 seek(FILE,0,1);
   }


That does look like it might be better... and thanks for the explanation.

[...]


   use File::Tail;
   $file=File::Tail-new(/some/log/file);
   while (defined($line=$file-read)) {
   print $line;
   }

 That looks OK, and works for me on /var/log/system.log, although the
 delays are longer using File::Tail than Unix tail. Maybe you should

That isn't actually the version I used .. its a bit lower on the page
you cited above... but I can't see anything that make it work any
different. 

cat fltr_sl

[...]

  use File::Tail;

  my ($file,$line);
  my $fname_in = /var/adm/slpipe;

  $file=File::Tail-new($fname_in);
  while (defined($line=$file-read)) {
 print $line;
  }  

 try it on a normal file that you write to occasionally with another
 Perl program.

One question, in your test you didn't actually run it against a
named-pipe did you?  Would that be likely to make a difference?

Taking your suggestion I see the script above will ouput from a normal
file.

touch t1

./filterWithFileTail.pl (edited to open ./t1)

while [[ 1 ]];do
 echo Now you've done it  t1
sleep 1
cat ~/.bash_history  t1
sleep 1
done

The File::Tail filter does eventually output the data coming in.  You
mentioned it's slower.. it seems a good bit slower here.

But it will NOT ouput data from the named pipe.

I ran the same while loop writing to the named pipe and still the perl
filter with File::Tail won't output a thing.

Anyway I have a working script... soon to modify with your
suggestions. .. thanks for the help.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: perl like tail -f

2009-10-22 Thread Harry Putnam
Shawn H Corey shawnhco...@gmail.com writes:

   open(FILE,./named-pipe) or die Can't Open ./named-pipe: $!;
   while(FILE){
 print;
 if(eof){
   sleep 2;
   seek (FILE,0,1);
 }
   } 
 
 It seems at least to survive repeated restarts of system logger.
 
 If I write my script based on this code... what I'd be adding would be
 code to get 1 or 2 rgx from the cmdline, then write the hits to
 various files.

[...]

 In the general case, modules usually take care of special cases that you
  may not be aware of.  That makes them the preferred method of solving a
 problem.

 In this case, the above code is a kludge.  You can tell this because the
 program sleeps, rather than waiting on input.  When a program does
 something to emulate what it really should be doing, it introduces code
 that may not work in all cases.

I'm having trouble with File::Tail... or more likely the way I'm
trying to use it is wrongly setup.

But first about that sleep comment.  As I read a little of File::Tail
and its very likely I'm not really understanding what I'm reading but,
it appears to be saying that it `sleeps' at times...  the times are
a little more sophisticated... but none the less sleep.

Now the problem.

I've taken the first examples in the perldoc File::Tail output:
   http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm

  use File::Tail;
  $file=File::Tail-new(/some/log/file);
  while (defined($line=$file-read)) {
  print $line;
  }

And tied to make it work for my case (/var/adm/slpipe in the script is a
named-pipe that the system logger reads into):

cat fltr_sl

  #!/usr/local/bin/perl
  
  use strict;
  use warnings;
  use File::Tail;

  my ($file,$line);
  my $fname_in = /var/adm/slpipe;

  $file=File::Tail-new($fname_in);
  while (defined($line=$file-read)) {
 print $line;
  }  

When I run it, it doesn't show any errors, and appears to be waiting
on the pipe.

But no data ever comes out.  I used the same sequence as for the
earlier script the didn't use File::Tail (posted earlier in this
thread).   That is.

 1) start the script `fltr_sl' shown above
 2) To make sure if data is flowing thru the pipe start
tail -f slpipe too.
 2) kill -HUP the system logger.
 3) Ensure some data is flowing thru the named-pipe by
running ssh r...@localhost from a user account

I see 7-8 lines output to the tail -f slpipe command, but nothing to
the perl script fltr_sl.

And as I write quite a few more lines are appearing at the 
`tail -f slpipe' cmd, but still nothing at my perl filter. 

Have I made some serious mistake in my attempted usage of File::Tail? 
 


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: perl like tail -f

2009-10-20 Thread Shawn H Corey
Harry Putnam wrote:
 Shawn H Corey shawnhco...@gmail.com writes:
 
 http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm
 
 Thanks that looks useful.  Is there a reason why I should use that
 module as apposed to the kind of code offered in the faq about 
 tail? (perldoc -q  tail) as suggested by another poster (Jim G).
 
 I mean, I'm not experienced enough to know if there are things the
 module handles that a simple script like the one below does not.
 
   open(FILE,./named-pipe) or die Can't Open ./named-pipe: $!;
   while(FILE){
 print;
 if(eof){
   sleep 2;
   seek (FILE,0,1);
 }
   } 
 
 It seems at least to survive repeated restarts of system logger.
 
 If I write my script based on this code... what I'd be adding would be
 code to get 1 or 2 rgx from the cmdline, then write the hits to
 various files.
 
 I'm told there is a systematic way to add my script to solaris bootup,
 and to restart it automatically if need be.  But other than those are
 there other `gotchas' or whatever I'm liable to need the module for?
 
 

In the general case, modules usually take care of special cases that you
 may not be aware of.  That makes them the preferred method of solving a
problem.

In this case, the above code is a kludge.  You can tell this because the
program sleeps, rather than waiting on input.  When a program does
something to emulate what it really should be doing, it introduces code
that may not work in all cases.


-- 
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: perl like tail -f

2009-10-20 Thread Peter Scott
On Mon, 19 Oct 2009 23:30:30 -0500, Harry Putnam wrote:
 Shawn H Corey shawnhco...@gmail.com writes:
 
 http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm
 
 Thanks that looks useful.  Is there a reason why I should use that
 module as apposed to the kind of code offered in the faq about tail?
 (perldoc -q  tail) as suggested by another poster (Jim G).

For code this brief, it's a toss-up.  Using the module results in about 
the same amount of code; the advantage is that it's a bit more readable.  
In general, for tasks like this, you can expect a module to be more 
portable and featureful, and handle more special cases.  You may never 
need any of its additional functionality, but if the day comes when, for 
instance, you want your code to be smarter about how fast it responds to 
changes in a file, the module would instantly become a better choice.

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=0137001274

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




perl like tail -f

2009-10-19 Thread Harry Putnam
With a linux background I'm used to being able to my the system logger
right to a named pipe buy means of a `|' (pipe) symbol in syslog.conf

 *.*   |/var/log/fifo

But on Opensolaris the system logger is enough different than the
sysklogd daemon on linux that the `|' symbol is ignored in
/etc/syslog.conf

On opensolaris, you can tell syslogger to write direct to the fifo.
But it has a side effect
 Lets say you create the fifo

To get data flowing into your filter or script, syslog needs a restart.

But first we use the easy example of cat.

   cat fifo

   Now restart system logger.

   Now anything that sys logger produces will be displayed from the cat
   command.

   Now restart systemlogger again and cat closes as would a common
   shell script or awk.

However the `tail -f' command will not close... it keeps on reading
through restarts.

I want to get that behavior from a perl script.  I realize I could
have my script read from `tail -f fifo|script.pl' but that introduces another
factor... a buffer filling factor where tail -f's shell buffer has to
fill up between writes.

That introduces a lag... so if you want a truly live syslog read.. you
need some kind of fancy buffer flushing or the like.

I'm not sure what it is about the tail -f command that allows it to
keep reading over restarts of system logger (on Opensolaris)... but
how can I emulate whatever it is.. in perl?


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: perl like tail -f

2009-10-19 Thread Shawn H Corey
Harry Putnam wrote:
 I'm not sure what it is about the tail -f command that allows it to
 keep reading over restarts of system logger (on Opensolaris)... but
 how can I emulate whatever it is.. in perl?

Have you looked at File::Tail
http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm ?


-- 
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: perl like tail -f

2009-10-19 Thread Jim Gibson
On 10/19/09 Mon  Oct 19, 2009  11:38 AM, Harry Putnam rea...@newsguy.com
scribbled:


 
 I'm not sure what it is about the tail -f command that allows it to
 keep reading over restarts of system logger (on Opensolaris)... but
 how can I emulate whatever it is.. in perl?
 

There is an FAQ:

perldoc -q tail

See if that helps you.

The idea is to read until the end-of-file is reached, pause for awhile, then
try reading again. However, reaching EOF sets a flag in the file handle and
subsequent reads will fail. The trick is to reset the EOF flag with a seek
command, either

seek($fh,0,1)

or

$curpos = tell($fh);
seek($fh,$curpos,0);




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: perl like tail -f

2009-10-19 Thread Harry Putnam
Jim Gibson jimsgib...@gmail.com writes:

 On 10/19/09 Mon  Oct 19, 2009  11:38 AM, Harry Putnam rea...@newsguy.com
 scribbled:


 
 I'm not sure what it is about the tail -f command that allows it to
 keep reading over restarts of system logger (on Opensolaris)... but
 how can I emulate whatever it is.. in perl?
 

 There is an FAQ:

 perldoc -q tail

 See if that helps you.

 The idea is to read until the end-of-file is reached, pause for awhile, then
 try reading again. However, reaching EOF sets a flag in the file handle and
 subsequent reads will fail. The trick is to reset the EOF flag with a seek
 command, either

 seek($fh,0,1)

 or

 $curpos = tell($fh);
 seek($fh,$curpos,0);

It all sounds good so trying a simple example like the one below. 

But being A bit unsure.. I can report that this simple example seems
to work... at least in so far as surviving repeated restarts of
system.logger.  Am I making some non-obvious mistake here?

  open(FILE,./named-pipe) or die Can't Open ./named-pipe: $!;
  while(FILE){
print;
if(eof){
  sleep 2;
  seek (FILE,0,1);
}
  } 
   



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: display tail of monster file

2009-09-27 Thread Harry Putnam
叶孤城 gucheng...@freenet.de writes:

 2009/9/27 叶孤城 gucheng...@freenet.de:
 2009/9/27 Harry Putnam rea...@newsguy.com:
 Will it make any difference in loading time or cpu resource usage to
 use the `tail' command like 'qx/tail -n30 $file/' as apposed to something
 in straight perl like this example that prints the last 30 lines of $log:


 Not absolute that which is better.
 But `tail` will launch an external process that is generally slower.
 You may benchmark them to see the difference, using the module like:


 very sorry, copy a wrong link, the correct one:

 http://search.cpan.org/~dcoppit/Benchmark-Timer-0.7102/lib/Benchmark/Timer.pm

Thanks for the link.  After looking there... it appears the module
concerns itself with timing.  I wondered if timing will also tell you
about system resource use.  


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




display tail of monster file

2009-09-26 Thread Harry Putnam
Will it make any difference in loading time or cpu resource usage to
use the `tail' command like 'qx/tail -n30 $file/' as apposed to something
in straight perl like this example that prints the last 30 lines of $log:

  #!/usr/local/bin/perl 
  
  use strict;
  use warnings;
  
  my $log = shift;
  
  print rb($log);
  
  sub rb {
use File::ReadBackwards;
my $log = shift;
my (@ar,$logline);
  
my $bw = File::ReadBackwards-new( $log ) or
 die can't read $log: $! ;  
  
my $cnt = 0;
while( defined( $logline = $bw-readline ) ) {
  $cnt++ if($logline);
  if($cnt = 30) {
push @ar,$logline;
  }else{
last;
  }
}
## reverse @ar
return reverse @ar;
  }
  




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: display tail of monster file

2009-09-26 Thread 叶孤城
2009/9/27 Harry Putnam rea...@newsguy.com:
 Will it make any difference in loading time or cpu resource usage to
 use the `tail' command like 'qx/tail -n30 $file/' as apposed to something
 in straight perl like this example that prints the last 30 lines of $log:


Not absolute that which is better.
But `tail` will launch an external process that is generally slower.
You may benchmark them to see the difference, using the module like:

http://search.cpan.org/~pangj/Net-Squid-ReverseProxy-0.02/lib/Net/Squid/ReverseProxy.pm

//ye

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: display tail of monster file

2009-09-26 Thread 叶孤城
2009/9/27 叶孤城 gucheng...@freenet.de:
 2009/9/27 Harry Putnam rea...@newsguy.com:
 Will it make any difference in loading time or cpu resource usage to
 use the `tail' command like 'qx/tail -n30 $file/' as apposed to something
 in straight perl like this example that prints the last 30 lines of $log:


 Not absolute that which is better.
 But `tail` will launch an external process that is generally slower.
 You may benchmark them to see the difference, using the module like:


very sorry, copy a wrong link, the correct one:

http://search.cpan.org/~dcoppit/Benchmark-Timer-0.7102/lib/Benchmark/Timer.pm

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




RE: How to implement tail -f using perl in both windows and linux !

2009-03-24 Thread Bob McConnell
From: Chas. Owens
 On Mon, Mar 23, 2009 at 14:16, Amit Saxena learn.tech...@gmail.com
wrote:
 Is it possible to implement this without using any external modules
from
 CPAN ?
 
 The FAQ covers this quite nicely (see below).  In general you should
 use File::Tail[1].  And on UNIX systems, of course, you can always
 cheat:
 
 There are generally five reasons for not wanting to install modules:
 
 5. the target machine is totally locked down (i.e. you login
to rbash and have to provide code to a third party for
inclusion on the box)
 
 Of the five, only the fifth is truly difficult to work around, but it
 is possible using the same techniques as the fourth with an added
 argument to the third party that they are willing to install other
 binary programs.

If I may, I would like to wrap this case in a real world context. We are
a certified credit card processor, commonly known as a PCI-DSS(*) level
1 Service Provider. There are specific rules about what we can and can't
do on the production servers, which periodically have to pass detailed
audits in order to renew our certification. In addition, there are other
audits (PA-DSS) required for credit card applications we distribute to
be installed on clients' computers.

First of all, as a developer I am not allowed any access to the
production servers, either ours or the clients'. I may not log in to
them in any form. The system admins work in another department and they
are only allowed to install tested and certified packages.

Once the servers have passed the initial audit and are certified, every
modification or addition must pass through a specific sequence of
checks. First, they must undergo a thorough code review. Second, they
must be packaged in an approved format for automatic installation,
currently the Red Hat RPM packaging. Then that package must be installed
and tested on a separate QA system. Once the QA team has approved the
update, it is placed in a repository where an administrator, either here
or at a client site, can check it out and install it.

There are a number of reasons for this Chinese Wall between development
and production, but the biggest is accountability. If I can't directly
access the production servers, there is little chance that I can fiddle
with the data on those servers without someone else at least detecting
the intent to fiddle. Since there is real money involved, the discovery
of an intent to fiddle would at least cost me my job, and could put me
in prison.

These rules are in place to protect the credit card issuers and are very
thoroughly enforced. They also provide some liability protection for my
employer, although there are additional safeguards in place for that. In
the past few years there have been some highly publicized breakdowns of
these rules, resulting in more scrutiny and tighter enforcement for
every one else in the industry.

As a result, installing a new module on the production server is very
expensive. It must go through our code review and QA testing as well as
being repackaged in the correct format for deployment. It is difficult
to justify this expense. It would require even more effort to justify
installing directly from CPAN, since none of the modules there have been
through our code review nor been examined by either our QA staff or the
PCI auditors.

So don't talk to me about working around the limitations. The auditors
will almost certainly complain about that. Tell me how to install a new
module within these rules?

Bob McConnell

(*) PCI-DSS: Payment Card Industry - Data Security Standards

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How to implement tail -f using perl in both windows and linux !

2009-03-24 Thread Chas. Owens
On Tue, Mar 24, 2009 at 10:26, Bob McConnell r...@cbord.com wrote:
snip
 As a result, installing a new module on the production server is very
 expensive. It must go through our code review and QA testing as well as
 being repackaged in the correct format for deployment. It is difficult
 to justify this expense. It would require even more effort to justify
 installing directly from CPAN, since none of the modules there have been
 through our code review nor been examined by either our QA staff or the
 PCI auditors.

 So don't talk to me about working around the limitations. The auditors
 will almost certainly complain about that. Tell me how to install a new
 module within these rules?

 Bob McConnell

 (*) PCI-DSS: Payment Card Industry - Data Security Standards


You fall into case 5, completely locked down box (which if you recall
is the one I said is difficult to work around).  You have two options:

  1. Follow the procedures your company had put in place for
 new libraries to be put on the box.
  2. Provide PAR::Packer executables to QA.

I have been in those environments before, and nobody seems to complain
when a C programmer staticly links a library to his or her program.  I
see PAR::Packer as a similar beast; QA gets a monolithic binary that
they can test.

The question you have to ask yourself is whether it is more expensive
to replicate the functionality yourself.  For some modules, like the
DBI, the answer is obviously yes.  Remember, if you need the
functionality, you still wind up writing a similar amount of code (or
you wind up not implementing all of the corner cases and have buggy
code), so the hit from QAing is going to happen anyway and you will
have wasted the companies time and money writing, often inferior,
code.

Oh, and I would never suggest using CPAN (or CPANPLUS) in a such an
environment.  They do not provide a reliable mechanism for backing out
modules.  A package management system like RPM is much more
preferable.  I have often found that modules that are already packaged
by your vendor (RedHat in this case) get a free pass in environments
like this.

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




How to implement tail -f using perl in both windows and linux !

2009-03-23 Thread Amit Saxena
Is it possible to implement this without using any external modules from
CPAN ?

Thanks  Regards,
Amit Saxena


Re: [PBML] How to implement tail -f using perl in both windows and linux !

2009-03-23 Thread Randal L. Schwartz
 Amit == Amit Saxena learn.tech...@gmail.com writes:

Amit Is it possible to implement this without using any external modules from
Amit CPAN ?

Is it possible? Yes. Reasonable? No.

Please state clearly your confusion about being able to include CPAN modules
in your workflow.  Hint: none of the reasons you come up with will have any
ultimate validity, but I'm curious about your *particular* confusion.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
mer...@stonehenge.com URL:http://www.stonehenge.com/merlyn/
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How to implement tail -f using perl in both windows and linux !

2009-03-23 Thread Chas. Owens
On Mon, Mar 23, 2009 at 14:16, Amit Saxena learn.tech...@gmail.com wrote:
 Is it possible to implement this without using any external modules from
 CPAN ?

 Thanks  Regards,
 Amit Saxena


The FAQ covers this quite nicely (see below).  In general you should
use File::Tail[1].  And on UNIX systems, of course, you can always
cheat:

#!/usr/bin/perl

use strict;
use warnings;

open my $pipe, -|, /usr/bin/tail, -f, ./file.log
or die could not start tail on file.log: $!;

print while $pipe;

There are generally five reasons for not wanting to install modules:

1. they scare you (answer, get over it)
2. they scare your sysadmins (answer, work around them by
   installing in your home directory and use the lib[2] pragma)
3. you are using a hosting service that prevents you from
   installing modules (answer, get a better service, there
   are cheap services that don't behave like morons)
4. the target machine doesn't necessarily have the needed
   module (answer, use PAR[3] or PAR::Packer[4])
5. the target machine is totally locked down (i.e. you login
   to rbash and have to provide code to a third party for
   inclusion on the box)

Of the five, only the fifth is truly difficult to work around, but it
is possible using the same techniques as the fourth with an added
argument to the third party that they are willing to install other
binary programs.

from perldoc -q tail[5]
   First try

   seek(GWFILE, 0, 1);

   The statement seek(GWFILE, 0, 1) doesn’t change the current position,
   but it does clear the end‐of‐file condition on the handle, so that the
   next GWFILE makes Perl try again to read something.

   If that doesn’t work (it relies on features of your stdio
   implementation), then you need something more like this:

   for (;;) {
 for ($curpos = tell(GWFILE); GWFILE; $curpos =
tell(GWFILE)) {
   # search for some stuff and put it into files
 }
 # sleep for a while
 seek(GWFILE, $curpos, 0);  # seek to where we had been
   }

   If this still doesn’t work, look into the POSIX module.  POSIX defines
   the clearerr() method, which can remove the end of file condition on a
   filehandle.  The method: read until end of file, clearerr(), read some
   more.  Lather, rinse, repeat.

   There’s also a File::Tail module from CPAN.

1. http://search.cpan.org/dist/File-Tail/Tail.pm
2. http://perldoc.perl.org/lib.html
3. http://search.cpan.org/dist/PAR/lib/PAR.pm
4. http://search.cpan.org/dist/PAR-Packer/lib/PAR/Packer.pm
5. http://perldoc.perl.org/perlfaq5.html#How-do-I-do-a-%27tail--f%27-in-perl%3f

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




perl head and tail command ?

2008-04-28 Thread Richard Lee

I would imagine linux's head command can be replaced w/ chop
I asked this because I have a filehandle which was,

open $source, '/tmp/server.txt'   ( and no doing head -1 /tmp/server.txt 
is not an option since I need to go through some other stuff

before i need to issue below command )

and I wanted to do

my $top = `head -1 $source`
my $bottom = `tail -1 $source`

but I realized I cannot do $source in back tick.

so I imagine i can do

my $top = chop $source;

But what about the $bottom one?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: perl head and tail command ?

2008-04-28 Thread Rob Dixon
Richard Lee wrote:

 I would imagine linux's head command can be replaced w/ chop
 I asked this because I have a filehandle which was,
 
 open $source, '/tmp/server.txt'   ( and no doing head -1 /tmp/server.txt 
 is not an option since I need to go through some other stuff
 before i need to issue below command )
 
 and I wanted to do
 
 my $top = `head -1 $source`
 my $bottom = `tail -1 $source`
 
 but I realized I cannot do $source in back tick.
 
 so I imagine i can do
 
 my $top = chop $source;
 
 But what about the $bottom one?

The chop() function simply removes the last character from a string and returns 
it. Since $source isn't a string you would get an error. Your best bet is 
likely to be the Tie::File module. For instance

  use strict;
  use warnings;

  use Tie::File;

  tie my @file, 'Tie::File', '/tmp/server.txt' or die $!;

  my ($top, $bottom) = @file[0, -1];

HTH,

Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: perl head and tail command ?

2008-04-28 Thread Richard Lee

Rob Dixon wrote:

Richard Lee wrote:
  

I would imagine linux's head command can be replaced w/ chop
I asked this because I have a filehandle which was,

open $source, '/tmp/server.txt'   ( and no doing head -1 /tmp/server.txt 
is not an option since I need to go through some other stuff

before i need to issue below command )

and I wanted to do

my $top = `head -1 $source`
my $bottom = `tail -1 $source`

but I realized I cannot do $source in back tick.

so I imagine i can do

my $top = chop $source;

But what about the $bottom one?



The chop() function simply removes the last character from a string and returns 
it. Since $source isn't a string you would get an error. Your best bet is 
likely to be the Tie::File module. For instance

  use strict;
  use warnings;

  use Tie::File;

  tie my @file, 'Tie::File', '/tmp/server.txt' or die $!;

  my ($top, $bottom) = @file[0, -1];

HTH,

Rob
  



when you are doing it to filehandle vs array, which consumes more memory?

my source file is about 100,000 lines...

open my $source, 'tmp/server.txt' or die !$;

VS
tie my @file, 'Tie::File', '/tmp/server.txt' or die $!;



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: perl head and tail command ?

2008-04-28 Thread Rob Dixon
Richard Lee wrote:
 Rob Dixon wrote:
 Richard Lee wrote:
   
 I would imagine linux's head command can be replaced w/ chop
 I asked this because I have a filehandle which was,

 open $source, '/tmp/server.txt'   ( and no doing head -1 /tmp/server.txt 
 is not an option since I need to go through some other stuff
 before i need to issue below command )

 and I wanted to do

 my $top = `head -1 $source`
 my $bottom = `tail -1 $source`

 but I realized I cannot do $source in back tick.

 so I imagine i can do

 my $top = chop $source;

 But what about the $bottom one?
 

 The chop() function simply removes the last character from a string and 
 returns it. Since $source isn't a string you would get an error. Your best 
 bet is likely to be the Tie::File module. For instance

   use strict;
   use warnings;

   use Tie::File;

   tie my @file, 'Tie::File', '/tmp/server.txt' or die $!;

   my ($top, $bottom) = @file[0, -1];

 HTH,

 Rob
   
 
 
 when you are doing it to filehandle vs array, which consumes more memory?
 
 my source file is about 100,000 lines...
 
 open my $source, 'tmp/server.txt' or die !$;
 
  VS
 tie my @file, 'Tie::File', '/tmp/server.txt' or die $!;

Neither of them use any significant memory until you start reading from the 
file. Unless you mess about with file positioning with seek() and tell(). the 
only way to retrieve the last record in a file using a standard file handle is 
to read all the way through it one line at a time. The documentation for 
Tie::File says this:

 The file is *not* loaded into memory, so this will work even for
 gigantic files.

So it is likely to be best for your needs.

However, you should not worry about speed or memory usage until you have 
written the clearest program you can and then find that it consumes too many 
resources.

HTH,

Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: perl head and tail command ?

2008-04-28 Thread Gunnar Hjalmarsson

Rob Dixon wrote:

Richard Lee wrote:
when you are doing it to filehandle vs array, which consumes more 
memory?


my source file is about 100,000 lines...

open my $source, 'tmp/server.txt' or die !$;

VS tie my @file, 'Tie::File', '/tmp/server.txt' or die $!;


Neither of them use any significant memory until you start reading 
from the file. Unless you mess about with file positioning with 
seek() and tell(). the only way to retrieve the last record in a file 
using a standard file handle is to read all the way through it one 
line at a time. The documentation for Tie::File says this:


The file is *not* loaded into memory, so this will work even for 
gigantic files.


So it is likely to be best for your needs.


Actually, Tie::File seems to be very slow.

http://www.mail-archive.com/beginners%40perl.org/msg93566.html

However, you should not worry about speed or memory usage until you 
have written the clearest program you can and then find that it 
consumes too many resources.


True.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Problem in opening the file using Tail Module

2007-08-29 Thread sivasakthi
Hi Guys,

I have used the  following code to print the log files. but nothing is
printed  even it doesn't show the warning message,  no prompt is
returned.
what is the mistake in code , could u help me to find the pbm???

#!/usr/bin/perl
use strict;
use warnings;

use File::Tail;
my $file=File::Tail-new(/some/log/file/path); 
while (defined(my $line=$file-read))
{
print $line;
}


Thanks,
Siva



Re: Problem in opening the file using Tail Module

2007-08-29 Thread Jeff Pang
I tested the codes,which run well for me.
Give a try to add '$|++' to disable IO buffer.

2007/8/29, sivasakthi [EMAIL PROTECTED]:
 Hi Guys,

 I have used the  following code to print the log files. but nothing is
 printed  even it doesn't show the warning message,  no prompt is
 returned.
 what is the mistake in code , could u help me to find the pbm???

 #!/usr/bin/perl
 use strict;
 use warnings;

 use File::Tail;
 my $file=File::Tail-new(/some/log/file/path);
 while (defined(my $line=$file-read))
 {
 print $line;
 }


 Thanks,
 Siva



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: AW: Problem in opening the file using Tail Module

2007-08-29 Thread sivasakthi
File::Tail is waiting at least 10 second before starting to work.
if it is taking at least 10 second to start then how it is faster than
using open to access the file???

On Wed, 2007-08-29 at 11:49 +0200, Angerstein wrote:

 File::Tail is waiting at least 10 second before starting to work.
 If this is not the prob, try to initalize my $line = undef; 
 
 If this don´t work, you should check your file. 



Thanks,
Siva


Re: Problem in opening the file using Tail Module

2007-08-29 Thread sivasakthi
i didn't get your point , please explain little bit more...

Thanks,
Siva

 On Wed, 2007-08-29 at 17:55 +0800, Jeff Pang wrote:

 I tested the codes,which run well for me.
 Give a try to add '$|++' to disable IO buffer.
 
 2007/8/29, sivasakthi [EMAIL PROTECTED]:
  Hi Guys,
 
  I have used the  following code to print the log files. but nothing is
  printed  even it doesn't show the warning message,  no prompt is
  returned.
  what is the mistake in code , could u help me to find the pbm???
 
  #!/usr/bin/perl
  use strict;
  use warnings;
 
  use File::Tail;
  my $file=File::Tail-new(/some/log/file/path);
  while (defined(my $line=$file-read))
  {
  print $line;
  }
 
 
  Thanks,
  Siva
 
 


Re: Problem in opening the file using Tail Module

2007-08-29 Thread Rob Dixon

sivasakthi wrote:


I have used the following code to print the log files. but nothing is
 printed even it doesn't show the warning message, no prompt is 
returned. what is the mistake in code , could u help me to find the

pbm???

#!/usr/bin/perl
use strict;
use warnings;

use File::Tail;
my $file=File::Tail-new(/some/log/file/path); 
while (defined(my $line=$file-read))

{
print $line;
}


File::Tail will read only lines that are added to the file after it has been
opened. Your program will simply sleep until something new is added.

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Problem in opening the file using Tail Module

2007-08-29 Thread Mumia W.

On 08/29/2007 04:41 AM, sivasakthi wrote:

Hi Guys,

I have used the  following code to print the log files. but nothing is
printed  even it doesn't show the warning message,  no prompt is
returned.
what is the mistake in code , could u help me to find the pbm???

#!/usr/bin/perl
use strict;
use warnings;

use File::Tail;
my $file=File::Tail-new(/some/log/file/path); 
while (defined(my $line=$file-read))

{
print $line;
}


Thanks,
Siva




It works for me.

Maybe you want to change one of the intervals or timeouts for 
File::Tail. It could be that your log file is updated infrequently.




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Problem in opening the file using Tail Module

2007-08-29 Thread sivasakthi


 
 File::Tail will read only lines that are added to the file after it has been
 opened. Your program will simply sleep until something new is added.
 
 Rob


when my log file is updated then it works correctly

thanks to all for reply



AW: Problem in opening the file using Tail Module

2007-08-29 Thread Angerstein
Buffering is a problem, when you are printing to files or pipes.
The Systems don´t write everything right away to disc. It waits until it
has some time for it or the buffer is full.
If you disable buffering, you write without wait for the cost of
performance.
(if you want to write with no buffering at all you use syswrite and only
write 1 char at a time.)

-Ursprüngliche Nachricht-
Von: sivasakthi [mailto:[EMAIL PROTECTED] 
Gesendet: Mittwoch, 29. August 2007 12:13
An: Jeff Pang
Cc: beginners perl
Betreff: Re: Problem in opening the file using Tail Module


i didn't get your point , please explain little bit more...

Thanks,
Siva

 On Wed, 2007-08-29 at 17:55 +0800, Jeff Pang wrote:

 I tested the codes,which run well for me.
 Give a try to add '$|++' to disable IO buffer.
 
 2007/8/29, sivasakthi [EMAIL PROTECTED]:
  Hi Guys,
 
  I have used the  following code to print the log files. but nothing 
  is printed  even it doesn't show the warning message,  no prompt is 
  returned. what is the mistake in code , could u help me to find the 
  pbm???
 
  #!/usr/bin/perl
  use strict;
  use warnings;
 
  use File::Tail;
  my $file=File::Tail-new(/some/log/file/path);
  while (defined(my $line=$file-read))
  {
  print $line;
  }
 
 
  Thanks,
  Siva
 
 


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




AW: Problem in opening the file using Tail Module

2007-08-29 Thread Angerstein
rtfm ^^

-Ursprüngliche Nachricht-
Von: sivasakthi [mailto:[EMAIL PROTECTED] 
Gesendet: Mittwoch, 29. August 2007 13:16
An: beginners perl
Betreff: Re: Problem in opening the file using Tail Module




 
 File::Tail will read only lines that are added to the file after it 
 has been opened. Your program will simply sleep until something new is

 added.
 
 Rob


when my log file is updated then it works correctly

thanks to all for reply



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




File::Tail + JavaScript (XmlHttpRequest)

2007-06-08 Thread Steve Finkelstein
TGIF All!

I hope my thread is relevant and not off-topic here. If it is, please
ignore it and I apologize for taking up your time.

I'm currently looking to implement a web function and an OS/X widget
utilizing JavaScript's XmlHttpRequest (aka asynchronous javascript, aka
AJAX). I thought at first it might make more sense to write the backend
part of my script in PHP, but PHP lacks
http://search.cpan.org/dist/File-Tail/Tail.pm which I'd like to use for
continuous screening of a spool file.

I was hoping someone could point me in the correct direction to have
Perl properly speak with XmlHttpRequest. My main objective is to use
AJAX functionality to speak to Perl on the backend, find me updates to
the spool/log file, and push that information back to the JavaScript
object which processes it and puts it into the browser.

Is what I'm looking for feasible? Make sense?

Thank you all, and enjoy your weekend!

- sf

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




monitoring file like tail -f in perl

2006-05-29 Thread Ken Foskey
I want to monitor a daemon's logs that updates randomly through the day
and night.  Is there a way to open the log file and read the new records
as they come in the same way as tail -f does?


Thanks
Ken


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: monitoring file like tail -f in perl

2006-05-29 Thread JupiterHost.Net



Ken Foskey wrote:

I want to monitor a daemon's logs that updates randomly through the day
and night.  Is there a way to open the log file and read the new records
as they come in the same way as tail -f does?


Sure, look on cpan for File::Tail::App and File::Tail

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: monitoring file like tail -f in perl

2006-05-29 Thread Xavier Noria

On May 29, 2006, at 18:19, Ken Foskey wrote:

I want to monitor a daemon's logs that updates randomly through the  
day
and night.  Is there a way to open the log file and read the new  
records

as they come in the same way as tail -f does?


Sure, with File::Tail for example.

-- fxn



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




the 'tail' problem

2006-01-14 Thread Jeff Pang
hi,lists,

I have a log file which is a symbol link to the real logfile,shown as following:

$ll mssvr.log
lrwxrwxrwx1 cmail root   40 Jan 14 00:00 mssvr.log - 
/home/cmail/logs/mssvr.log.2006-01-14

I have to access this file in perl script with unix 'tail -f' command.Part of 
the code is below:

open (TAIL,tail -f $log|) or die can't open pipe:$!;
while(TAIL)
{
do something...
}

This script is a daemon script which run permanently.There is no problem when 
in the same day.But when the date changed,the symbol link file will point to 
another real logfile automatically (which decided by other application 
program),such as:

lrwxrwxrwx1 cmail root   40 Jan 14 00:00 mssvr.log - 
/home/cmail/logs/mssvr.log.2006-01-15

As you see,it's '2006-01-15' now, not '2006-01-14' as before.So the 'tail -f 
$log' become no use for the new symbol link file,and I can't get any input from 
the new logfile in the script.

How can I adjust this problem?Thanks a lot.


Jeff

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: the 'tail' problem

2006-01-14 Thread Adriano Ferreira
Jeff,

Maybe all you have to do is make some adjustments to the pipe you're
opening. Besides the well known -f switch, some tail's (like gnu
tail) support -F which means a file is followed by its name and the
opening is retried from time to time. From man tail (GNU):

   -F same as --follow=name --retry

  With  --follow  (-f),  tail  defaults to following the file descriptor,
  which means that even if a tail'ed file is renamed, tail will  continue
  to  track  its  end.   This  default behavior is not desirable when you
  really want to track the actual name of the file, not the file descrip-
  tor (e.g., log rotation).  Use --follow=name in that case.  That causes
  tail to track the named file by reopening it periodically to see if  it
  has been removed and recreated by some other program.

If you found it works for symlinks, all you have to do is use

 open (TAIL,tail -F $log|) or die can't open pipe:$!;

Regards,
Adriano Ferreira.

On 1/14/06, Jeff Pang [EMAIL PROTECTED] wrote:
 I have a log file which is a symbol link to the real logfile,shown as 
 following:

 I have to access this file in perl script with unix 'tail -f' command.Part of 
 the code is below:

 open (TAIL,tail -f $log|) or die can't open pipe:$!;

 This script is a daemon script which run permanently.There is no problem when 
 in the same day.But when the date changed,the symbol link file will point to 
 another real logfile automatically (which decided by other application 
 program),such as:

[snip]

 How can I adjust this problem?Thanks a lot.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: the 'tail' problem

2006-01-14 Thread Jeff Pang
Thanks for Adriano.I have tried the way that mentioned by you,and  found it's 
no use for me.
should the '-F' option  have no effect for symlinks maybe?

-Original Message-
From: Adriano Ferreira [EMAIL PROTECTED]
Sent: Jan 14, 2006 7:23 PM
To: Jeff Pang [EMAIL PROTECTED], beginners@perl.org
Subject: Re: the 'tail' problem

Jeff,

Maybe all you have to do is make some adjustments to the pipe you're
opening. Besides the well known -f switch, some tail's (like gnu
tail) support -F which means a file is followed by its name and the
opening is retried from time to time. From man tail (GNU):

   -F same as --follow=name --retry

  With  --follow  (-f),  tail  defaults to following the file descriptor,
  which means that even if a tail'ed file is renamed, tail will  continue
  to  track  its  end.   This  default behavior is not desirable when you
  really want to track the actual name of the file, not the file descrip-
  tor (e.g., log rotation).  Use --follow=name in that case.  That causes
  tail to track the named file by reopening it periodically to see if  it
  has been removed and recreated by some other program.

If you found it works for symlinks, all you have to do is use

 open (TAIL,tail -F $log|) or die can't open pipe:$!;

Regards,
Adriano Ferreira.

On 1/14/06, Jeff Pang [EMAIL PROTECTED] wrote:
 I have a log file which is a symbol link to the real logfile,shown as 
 following:

 I have to access this file in perl script with unix 'tail -f' command.Part 
 of the code is below:

 open (TAIL,tail -f $log|) or die can't open pipe:$!;

 This script is a daemon script which run permanently.There is no problem 
 when in the same day.But when the date changed,the symbol link file will 
 point to another real logfile automatically (which decided by other 
 application program),such as:

[snip]

 How can I adjust this problem?Thanks a lot.


--
http://home.earthlink.net/~pangj/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: the 'tail' problem

2006-01-14 Thread Adriano Ferreira
On 1/14/06, Jeff Pang [EMAIL PROTECTED] wrote:
 Thanks for Adriano.I have tried the way that mentioned by you,and  found it's 
 no use for me.
 should the '-F' option  have no effect for symlinks maybe?


Well, that way would be easier if it worked. But I think with some
extra logic you can do it with help of the Perl builtin readlink,
which is able to give you the filename the symbolic link points to.
Probably something can be written that checks to see if the link
changed or not.

Surely there's a CPAN module out there to help you do this. I also
made a few hasty tests, and if you got hard links rather symbolic
ones, it looks like tail -F would do the magic without hassle.

Cheers,
Adriano.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: the 'tail' problem

2006-01-14 Thread John Doe
Jeff Pang am Samstag, 14. Januar 2006 12.52:
 Thanks for Adriano.I have tried the way that mentioned by you,and  found
 it's no use for me. should the '-F' option  have no effect for symlinks
 maybe?

 -Original Message-

 From: Adriano Ferreira [EMAIL PROTECTED]
 Sent: Jan 14, 2006 7:23 PM
 To: Jeff Pang [EMAIL PROTECTED], beginners@perl.org
 Subject: Re: the 'tail' problem
 
 Jeff,
 
 Maybe all you have to do is make some adjustments to the pipe you're
 opening. Besides the well known -f switch, some tail's (like gnu
 tail) support -F which means a file is followed by its name and the
 opening is retried from time to time. From man tail (GNU):
 
-F same as --follow=name --retry
 
   With  --follow  (-f),  tail  defaults to following the file
  descriptor, which means that even if a tail'ed file is renamed, tail will
   continue to  track  its  end.   This  default behavior is not desirable
  when you really want to track the actual name of the file, not the file
  descrip- tor (e.g., log rotation).  Use --follow=name in that case.  That
  causes tail to track the named file by reopening it periodically to see
  if  it has been removed and recreated by some other program.
 
 If you found it works for symlinks, all you have to do is use
 
  open (TAIL,tail -F $log|) or die can't open pipe:$!;
 
 Regards,
 Adriano Ferreira.
 
 On 1/14/06, Jeff Pang [EMAIL PROTECTED] wrote:
  I have a log file which is a symbol link to the real logfile,shown as
  following:
 
  I have to access this file in perl script with unix 'tail -f'
  command.Part of the code is below:
 
  open (TAIL,tail -f $log|) or die can't open pipe:$!;
 
  This script is a daemon script which run permanently.There is no problem
  when in the same day.But when the date changed,the symbol link file will
  point to another real logfile automatically (which decided by other
  application program),such as:
 
 [snip]
 
  How can I adjust this problem?Thanks a lot.

Hi

After reading man tail I tested

   tail --retry --follow=name testlog

where testlog is a symlink. While this command is running, I can do (from 
another console):

   rm testlog
   ln -s testlog1 testlog
   rm testlog
   ln -s testlog2 testlog

It does what you want.

Maybe File::Tail will also do what you want, just test it (and read the man)

hth
joe

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: the 'tail' problem

2006-01-14 Thread Shawn Corey

Jeff Pang wrote:

hi,lists,

I have a log file which is a symbol link to the real logfile,shown as following:

$ll mssvr.log
lrwxrwxrwx1 cmail root   40 Jan 14 00:00 mssvr.log - 
/home/cmail/logs/mssvr.log.2006-01-14

I have to access this file in perl script with unix 'tail -f' command.Part of 
the code is below:

open (TAIL,tail -f $log|) or die can't open pipe:$!;
while(TAIL)
{
do something...
}

This script is a daemon script which run permanently.There is no problem when 
in the same day.But when the date changed,the symbol link file will point to 
another real logfile automatically (which decided by other application 
program),such as:

lrwxrwxrwx1 cmail root   40 Jan 14 00:00 mssvr.log - 
/home/cmail/logs/mssvr.log.2006-01-15

As you see,it's '2006-01-15' now, not '2006-01-14' as before.So the 'tail -f 
$log' become no use for the new symbol link file,and I can't get any input from 
the new logfile in the script.

How can I adjust this problem?Thanks a lot.


Jeff



You could try File::Tail from CPAN.
http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/
http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm
I haven't used it myself so I can't tell if it will solve your problem. 
If it doesn't, you'll have to roll your own. Look up the following:


perldoc -f lstat
perldoc -f readlink
perldoc -f tell
perldoc -f seek

Basically, you would have to keep track of where you are in the file and 
when the date changes, open the new file.



--

Just my 0.0002 million dollars worth,
   --- Shawn

Probability is now one. Any problems that are left are your own.
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is available at http://perldoc.perl.org/

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: the 'tail' problem

2006-01-14 Thread Jeff Pang
I don't know why it can't work for me.
I have tested it as following:

$ln -s mssvr.log.2006-01-14 mylog  
$tail --follow=name --retry mylog 

Now this command output the content of mssvr.log.2006-01-14 correctly.

So I continue to excute these commands (open another terminal):

$rm -f mylog
$ln -s mssvr.err mylog

mssvr.err is a real file not symlink.

So I expect the before command of 'tail --follow=name --retry mylog' should 
change to output the content of mssvr.err.But when  I return to the first 
terminal,I found the output of 'tail' is still the content of 
mssvr.log.2006-01-14,not mssvr.err's.

So strange it is for me.



-Original Message-
From: John Doe [EMAIL PROTECTED]
Sent: Jan 14, 2006 8:34 AM
To: beginners@perl.org
Subject: Re: the 'tail' problem

Jeff Pang am Samstag, 14. Januar 2006 12.52:
 Thanks for Adriano.I have tried the way that mentioned by you,and  found
 it's no use for me. should the '-F' option  have no effect for symlinks
 maybe?

 -Original Message-

 From: Adriano Ferreira [EMAIL PROTECTED]
 Sent: Jan 14, 2006 7:23 PM
 To: Jeff Pang [EMAIL PROTECTED], beginners@perl.org
 Subject: Re: the 'tail' problem
 
 Jeff,
 
 Maybe all you have to do is make some adjustments to the pipe you're
 opening. Besides the well known -f switch, some tail's (like gnu
 tail) support -F which means a file is followed by its name and the
 opening is retried from time to time. From man tail (GNU):
 
-F same as --follow=name --retry
 
   With  --follow  (-f),  tail  defaults to following the file
  descriptor, which means that even if a tail'ed file is renamed, tail will
   continue to  track  its  end.   This  default behavior is not desirable
  when you really want to track the actual name of the file, not the file
  descrip- tor (e.g., log rotation).  Use --follow=name in that case.  That
  causes tail to track the named file by reopening it periodically to see
  if  it has been removed and recreated by some other program.
 
 If you found it works for symlinks, all you have to do is use
 
  open (TAIL,tail -F $log|) or die can't open pipe:$!;
 
 Regards,
 Adriano Ferreira.
 
 On 1/14/06, Jeff Pang [EMAIL PROTECTED] wrote:
  I have a log file which is a symbol link to the real logfile,shown as
  following:
 
  I have to access this file in perl script with unix 'tail -f'
  command.Part of the code is below:
 
  open (TAIL,tail -f $log|) or die can't open pipe:$!;
 
  This script is a daemon script which run permanently.There is no problem
  when in the same day.But when the date changed,the symbol link file will
  point to another real logfile automatically (which decided by other
  application program),such as:
 
 [snip]
 
  How can I adjust this problem?Thanks a lot.

Hi

After reading man tail I tested

   tail --retry --follow=name testlog

where testlog is a symlink. While this command is running, I can do (from 
another console):

   rm testlog
   ln -s testlog1 testlog
   rm testlog
   ln -s testlog2 testlog

It does what you want.

Maybe File::Tail will also do what you want, just test it (and read the man)

hth
joe

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




--
http://home.earthlink.net/~pangj/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: the 'tail' problem

2006-01-14 Thread JupiterHost.Net



Jeff Pang wrote:

hi,lists,


Hello,


I have a log file which is a symbol link to the real logfile,shown as following:

$ll mssvr.log
lrwxrwxrwx1 cmail root   40 Jan 14 00:00 mssvr.log - 
/home/cmail/logs/mssvr.log.2006-01-14

I have to access this file in perl script with unix 'tail -f' command.Part of 
the code is below:

open (TAIL,tail -f $log|) or die can't open pipe:$!;
while(TAIL)
{
do something...
}

This script is a daemon script which run permanently.There is no problem when 
in the same day.But when the date changed,the symbol link file will point to 
another real logfile automatically (which decided by other application 
program),such as:

lrwxrwxrwx1 cmail root   40 Jan 14 00:00 mssvr.log - 
/home/cmail/logs/mssvr.log.2006-01-15

As you see,it's '2006-01-15' now, not '2006-01-14' as before.So the 'tail -f 
$log' become no use for the new symbol link file,and I can't get any input from 
the new logfile in the script.

How can I adjust this problem?Thanks a lot.


Have you looked at File::Tail::App? It will simplify your life a lot, if 
File::Tail catches that its tailing a link and its targte changes. II 
*think* it should be transparent.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: the 'tail' problem

2006-01-14 Thread Jeff Pang
Thanks for all.I have not tried any CPAN module,since we have nearly 300 hosts 
running this script,and for installing the CPAN module,I have to su to root,but 
I have no that privileges.
As a tmp resolving way,I add a cron to my crontab,and restart this script at 
everyday's 00:01,since the symlink get changed at 00:00.

-Original Message-
From: JupiterHost.Net [EMAIL PROTECTED]
Sent: Jan 15, 2006 1:04 AM
To: beginners@perl.org
Subject: Re: the 'tail' problem



Jeff Pang wrote:
 hi,lists,

Hello,

 I have a log file which is a symbol link to the real logfile,shown as 
 following:
 
 $ll mssvr.log
 lrwxrwxrwx1 cmail root   40 Jan 14 00:00 mssvr.log - 
 /home/cmail/logs/mssvr.log.2006-01-14
 
 I have to access this file in perl script with unix 'tail -f' command.Part 
 of the code is below:
 
 open (TAIL,tail -f $log|) or die can't open pipe:$!;
 while(TAIL)
 {
 do something...
 }
 
 This script is a daemon script which run permanently.There is no problem 
 when in the same day.But when the date changed,the symbol link file will 
 point to another real logfile automatically (which decided by other 
 application program),such as:
 
 lrwxrwxrwx1 cmail root   40 Jan 14 00:00 mssvr.log - 
 /home/cmail/logs/mssvr.log.2006-01-15
 
 As you see,it's '2006-01-15' now, not '2006-01-14' as before.So the 'tail -f 
 $log' become no use for the new symbol link file,and I can't get any input 
 from the new logfile in the script.
 
 How can I adjust this problem?Thanks a lot.

Have you looked at File::Tail::App? It will simplify your life a lot, if 
File::Tail catches that its tailing a link and its targte changes. II 
*think* it should be transparent.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




--
http://home.earthlink.net/~pangj/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: Emulate tail -f on file

2005-05-20 Thread Tielman Koekemoer \(TNE\)
 
Thanks all for your input. The File::Tail package works although it
seems to stop working once I restart the application writing to the
logfile. I've played around with the settings but I'll have to take it
up with the author.

Thanks again!

-Original Message-
From: Chris Knipe [mailto:[EMAIL PROTECTED] 
Sent: 19 May 2005 09:48 AM
To: Ramprasad A Padmanabhan
Cc: beginners@perl.org
Subject: Re: Emulate tail -f on file

On Thu, May 19, 2005 at 11:54:35AM +0530, Ramprasad A Padmanabhan
wrote:
 use File::Tail;
 
 
 On Thu, 2005-05-19 at 11:37, Tielman Koekemoer (TNE) wrote:
  Hi All,
  
  If I wanted to monitor a file in a way that would emulate tail
-f
  (in the shell), how would I open the file? 
  
  open(FILE,  filename |); (?)
  
  TIA


As someone who asked the -EXACT- same question, not even a month ago,
I can really suggest the archives, and I can also really second
File::Tail - it's a BRILLIANT module.

--
Chris.


--
To unsubscribe, e-mail: [EMAIL PROTECTED] For additional
commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
http://learn.perl.org/first-response



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Emulate tail -f on file

2005-05-20 Thread Wiggins d'Anconia
Because it's up-side down.
Why is that?
It makes replies harder to read.
Why not?
Please don't top-post. - Sherm Pendley, Mac OS X list


Tielman Koekemoer (TNE) wrote:
  
 Thanks all for your input. The File::Tail package works although it
 seems to stop working once I restart the application writing to the
 logfile. I've played around with the settings but I'll have to take it
 up with the author.
 
 Thanks again!
 

I am pretty sure that command line 'tail' has the same problem, though I
suspect this is an underlying problem with the C libs if that is the
case. For File::Tail you may want to check into the 'reset_tail' switch.

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Emulate tail -f on file

2005-05-19 Thread Tielman Koekemoer \(TNE\)

Hi All,

If I wanted to monitor a file in a way that would emulate tail -f
(in the shell), how would I open the file? 

open(FILE,  filename |); (?)

TIA

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Emulate tail -f on file

2005-05-19 Thread Ramprasad A Padmanabhan
use File::Tail;


On Thu, 2005-05-19 at 11:37, Tielman Koekemoer (TNE) wrote:
 Hi All,
 
 If I wanted to monitor a file in a way that would emulate tail -f
 (in the shell), how would I open the file? 
 
 open(FILE,  filename |); (?)
 
 TIA


--
Netcore Solutions Pvt. Ltd.
Website:  http://www.netcore.co.in
Spamtraps: http://cleanmail.netcore.co.in/directory.html
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Emulate tail -f on file

2005-05-19 Thread Offer Kaye
On 5/19/05, Tielman Koekemoer (TNE) wrote:
 
 Hi All,
 
 If I wanted to monitor a file in a way that would emulate tail -f
 (in the shell), how would I open the file?
 
 open(FILE,  filename |); (?)
 
 TIA
 

http://perldoc.perl.org/perlfaq5.html#How-do-I-do-a-tail--f-in-perl- 

-- 
Offer Kaye

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: Emulate tail -f on file

2005-05-19 Thread Chris Knipe
On Thu, May 19, 2005 at 11:54:35AM +0530, Ramprasad A Padmanabhan wrote:
 use File::Tail;
 
 
 On Thu, 2005-05-19 at 11:37, Tielman Koekemoer (TNE) wrote:
  Hi All,
  
  If I wanted to monitor a file in a way that would emulate tail -f
  (in the shell), how would I open the file? 
  
  open(FILE,  filename |); (?)
  
  TIA


As someone who asked the -EXACT- same question, not even a month ago,
I can really suggest the archives, and I can also really second
File::Tail - it's a BRILLIANT module.

--
Chris.


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

2004-05-19 Thread Claude
 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




tail -f does not exit

2004-05-14 Thread Claude
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?
-- 
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

2004-05-14 Thread Rob Dixon
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

2004-05-14 Thread Claude
 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: double-log-tail

2004-01-21 Thread Joel Newkirk
Cool, thanks - both look promising.

j

On Mon, 2004-01-19 at 14:47, Wiggins d'Anconia wrote:
 Joel Newkirk wrote:
  I'm interested in tailing two logs (qmail) simultaneously, and
  interleaving the data in something approaching chronological sequence,
  as well as dealing with logfile rotation gracefully.
  
  Any suggestions?
  
 
 Check out File::Tail on CPAN, particularly the section on 'select' and 
 the mentioned example script, it should get you close.
 
 http://search.cpan.org/~mgrabnar/File-Tail-0.98/Tail.pm
 
 And I might as well throw a POE reference in here, as usual this sort of 
 thing is trivial in POE, particularly since there is already a Wheel 
 written to do it, check out the cookbook entry here (not for the faint 
 of heart):
 
 http://poe.perl.org/?POE_Cookbook/Watching_Logs
 
 HTH,
 
 http://danconia.org
-- 
Not all those who wander are lost.  - JRR Tolkien


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: double-log-tail

2004-01-19 Thread Wiggins d'Anconia
Joel Newkirk wrote:
I'm interested in tailing two logs (qmail) simultaneously, and
interleaving the data in something approaching chronological sequence,
as well as dealing with logfile rotation gracefully.
Any suggestions?

Check out File::Tail on CPAN, particularly the section on 'select' and 
the mentioned example script, it should get you close.

http://search.cpan.org/~mgrabnar/File-Tail-0.98/Tail.pm

And I might as well throw a POE reference in here, as usual this sort of 
thing is trivial in POE, particularly since there is already a Wheel 
written to do it, check out the cookbook entry here (not for the faint 
of heart):

http://poe.perl.org/?POE_Cookbook/Watching_Logs

HTH,

http://danconia.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



double-log-tail

2004-01-12 Thread Joel Newkirk
I'm interested in tailing two logs (qmail) simultaneously, and
interleaving the data in something approaching chronological sequence,
as well as dealing with logfile rotation gracefully.

Any suggestions?

j

-- 
Not all those who wander are lost.  - JRR Tolkien


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Tail -f from a cgi script

2003-07-14 Thread Eugene Geldenhuys
Hi all

My woes continue
I had a great way to view system logs in real-time, all I did is direct the 
output of the tail command to my browser - a simple tail-f worked well.
Since upgrading to RH8 and Apache 2.x this no longer works, though a tail 
-n20 still does.
Does anyone have a way to output real-time logs to a browser?
Best Regards
Eugene Geldenhuys
MCNE ECNE MCSE MCP

TFX SOLUTIONS -
PROFESSIONAL NETWORK DESIGN ,IMPLEMENTATION AND 
SUPPORT


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Unix ls -lrt | tail -1 in Perl

2003-03-21 Thread NYIMI Jose (BMB)
 -Original Message-
 From: Palmer Greg [mailto:[EMAIL PROTECTED] 
 Sent: Thursday, March 20, 2003 4:50 PM
 To: NYIMI Jose (BMB)
 Subject: RE: Unix ls -lrt | tail -1 in Perl
 
 
 $filename = `ls -ltr|tail -1 $DIRECTORY`;
 print $filename;

Sorry, i didn't mentioned that i wanted a pure perl solution, thanks anyway.

my $latest = (sort {-M $a = -M $b} $dir/*)[0];
Will be my way to go, thanks Sudarshan Raghavan

José.



 DISCLAIMER 

This e-mail and any attachment thereto may contain information which is confidential 
and/or protected by intellectual property rights and are intended for the sole use of 
the recipient(s) named above. 
Any use of the information contained herein (including, but not limited to, total or 
partial reproduction, communication or distribution in any form) by other persons than 
the designated recipient(s) is prohibited. 
If you have received this e-mail in error, please notify the sender either by 
telephone or by e-mail and delete the material from any computer.

Thank you for your cooperation.

For further information about Proximus mobile phone services please see our website at 
http://www.proximus.be or refer to any Proximus agent.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Unix ls -lrt | tail -1 in Perl

2003-03-21 Thread Sudarshan Raghavan
On Fri, 21 Mar 2003, NYIMI Jose (BMB) wrote:

  -Original Message-
  From: Palmer Greg [mailto:[EMAIL PROTECTED] 
  Sent: Thursday, March 20, 2003 4:50 PM
  To: NYIMI Jose (BMB)
  Subject: RE: Unix ls -lrt | tail -1 in Perl
  
  
  $filename = `ls -ltr|tail -1 $DIRECTORY`;
  print $filename;
 
 Sorry, i didn't mentioned that i wanted a pure perl solution, thanks anyway.
 
 my $latest = (sort {-M $a = -M $b} $dir/*)[0];
 Will be my way to go, thanks Sudarshan Raghavan
 
 José.

The above solution will work but a more time effecient way will be to use 
the Schwartzian transform as explained by Rob Hanson in his mail. Thanks 
Rob :-)

The solution using while that I posted is also more time effecient than my 
sort solution :-)

hth



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Unix ls -lrt | tail -1 in Perl

2003-03-21 Thread John W. Krahn
Sudarshan Raghavan wrote:
 
 On Fri, 21 Mar 2003, NYIMI Jose (BMB) wrote:
 
   From: Palmer Greg [mailto:[EMAIL PROTECTED]
  
   $filename = `ls -ltr|tail -1 $DIRECTORY`;
   print $filename;
 
  Sorry, i didn't mentioned that i wanted a pure perl solution, thanks anyway.
 
  my $latest = (sort {-M $a = -M $b} $dir/*)[0];
  Will be my way to go, thanks Sudarshan Raghavan
 
 The above solution will work but a more time effecient way will be to use
 the Schwartzian transform as explained by Rob Hanson in his mail. Thanks
 Rob :-)
 
 The solution using while that I posted is also more time effecient than my
 sort solution :-)

If you want efficiency then don't use sort at all.  This was about three
to four times faster on my cursory tests then then Rob's example and
about two to three times faster than using `ls -ltr|tail -1` from within
perl.

my $latest;
opendir my $dh, $dir or die Cannot open $dir: $!;
my $t = ~0;
while ( defined( my $file = readdir $dh ) ) {
  if ( -f $dir/$file and  -M _  $t ) {
$latest = $file;
$t = -M _;
}
  }
print $latest\n;



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Unix ls -lrt | tail -1 in Perl

2003-03-20 Thread NYIMI Jose (BMB)
Hello,

How can I do this unix command  in Perl ?  : ls -lrt | tail -1
Actually, i would like to get the most recent file from a given directory.

Thanks in advance for your input.

José.


 DISCLAIMER 

This e-mail and any attachment thereto may contain information which is confidential 
and/or protected by intellectual property rights and are intended for the sole use of 
the recipient(s) named above. 
Any use of the information contained herein (including, but not limited to, total or 
partial reproduction, communication or distribution in any form) by other persons than 
the designated recipient(s) is prohibited. 
If you have received this e-mail in error, please notify the sender either by 
telephone or by e-mail and delete the material from any computer.

Thank you for your cooperation.

For further information about Proximus mobile phone services please see our website at 
http://www.proximus.be or refer to any Proximus agent.



Re: Unix ls -lrt | tail -1 in Perl

2003-03-20 Thread Sudarshan Raghavan
On Thu, 20 Mar 2003, NYIMI Jose (BMB) wrote:

 Hello,
 
 How can I do this unix command  in Perl ?  : ls -lrt | tail -1
 Actually, i would like to get the most recent file from a given directory.

my $latest = (sort {-M $b = -M $a} $dir/*)[-1];

or

my $latest;
while ($dir/*) {
$latest = (defined ($latest)  (-M $_  -M $latest)) ? $latest : $_;
}

Remember to check for the definedness of $latest if $dir is an empty 
directory

 
 Thanks in advance for your input.
 
 José.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Unix ls -lrt | tail -1 in Perl

2003-03-20 Thread Hanson, Rob
If you use the first version that Sudarshan posted, you might want to use
this instead.  It uses map, and is a lot faster.

my $latest = (sort {$b-{mtime} = $a-{mtime}}
 map {{mtime = -M $_, file = $_}}
 $dir/*)[-1];

print $latest-{file}, \n;

Benchmark results with and without using map:

Benchmark: timing 100 iterations of No-MAP, With-MAP...
No-MAP: 13 wallclock secs ( 2.43 usr  6.43 sys +  4.00 cusr  1.41 csys =
0.00 CPU)
  With-MAP:  8 wallclock secs ( 1.57 usr  0.81 sys +  4.04 cusr  1.27 csys =
0.00 CPU)

Using map allows -M to be performed on each file only once (instead of once
per sort iteration).  And since this is an expensive operation doing it only
once will save a lot of CPU time.

Rob


-Original Message-
From: Sudarshan Raghavan [mailto:[EMAIL PROTECTED]
Sent: Thursday, March 20, 2003 9:17 AM
To: Perl beginners
Subject: Re: Unix ls -lrt | tail -1 in Perl


On Thu, 20 Mar 2003, NYIMI Jose (BMB) wrote:

 Hello,
 
 How can I do this unix command  in Perl ?  : ls -lrt | tail -1
 Actually, i would like to get the most recent file from a given directory.

my $latest = (sort {-M $b = -M $a} $dir/*)[-1];

or

my $latest;
while ($dir/*) {
$latest = (defined ($latest)  (-M $_  -M $latest)) ? $latest : $_;
}

Remember to check for the definedness of $latest if $dir is an empty 
directory

 
 Thanks in advance for your input.
 
 José.


-- 
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]



How do you code a tail -f if you're using Net::Rsh?

2003-01-22 Thread Richard Fernandez
There's a FAQ that deals with coding a tail -f if you're logfile is on the
same box as the perl script.
But what if you want to use rsh to tail a file remotely?

Here's what I have:
--8---8---


#!/usr/bin/perl -w
use strict;
use Net::Rsh;

my $host = shift or die usage: my_rsh hostname command;
my $cmd  = shift or die usage: my_rsh hostname command;

my $session = Net::Rsh - new();

my @output = $session-rsh($host, 'local_user', 'remote_user', $cmd) or
die;

for (@output) {print;}

88
---8

and here's my command line:

$ sudo my_rsh.pl hostname tail -f /u01/app/apache/logs/access_log

It seems that I never reach the print statement...

I've tried things like:

open(OUTPUT, $session-rsh($host, 'local_user', 'remote_user', $cmd) )
or die;
while (OUTPUT) {print;}

but that doesn't work.

Can anyone shed some light on this?

Thanks.





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: How do you code a tail -f if you're using Net::Rsh?

2003-01-22 Thread Bob Showalter
Richard Fernandez wrote:
 There's a FAQ that deals with coding a tail -f if you're logfile is
 on the same box as the perl script.
 But what if you want to use rsh to tail a file remotely?
 
 Here's what I have:
 --8--
 -8---
 
 
 #!/usr/bin/perl -w
 use strict;
 use Net::Rsh;
 
 my $host = shift or die usage: my_rsh hostname command;
 my $cmd  = shift or die usage: my_rsh hostname command;
 
 my $session = Net::Rsh - new();
 
 my @output = $session-rsh($host, 'local_user',
 'remote_user', $cmd) or
 die;
 
 for (@output) {print;}
 
 88--
 -- 
 ---8
 
 and here's my command line:
 
 $ sudo my_rsh.pl hostname tail -f /u01/app/apache/logs/access_log
 
 It seems that I never reach the print statement...
 
 I've tried things like:
 
 open(OUTPUT, $session-rsh($host, 'local_user',
 'remote_user', $cmd) )
 or die;
 while (OUTPUT) {print;}
 
 but that doesn't work.

The call to Net::Rsh::rsh is going to wait for the remote command to exit,
and then return the output as an array of lines. But tail -f is never going
to exit, so you need a different approach.

Try a pipe open like this:

   open RSH, rsh hostname tail -f /u01/app/apache/logs/access_log |
  or die $!;
   print while RSH;
   close RSH or die $!;

The print loop will run forever, so you'll have to interrupt your script.

The remote process will end when it tries to write the next line (it'll get
a SIGPIPE), or when the TCP keepalive detects that the client is gone.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: tail + count

2003-01-12 Thread Mark Goland
 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]




tail + count

2003-01-11 Thread Mrtlc
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]




Re: tail + count

2003-01-11 Thread John W. Krahn
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

2003-01-11 Thread Rob Dixon
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]




Grab last line like `tail'

2002-12-01 Thread Harry Putnam
Is there a perl equivalent to the unix `tail' command?  Where I could
grab the last line from a file without having to read the whole file?



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Grab last line like `tail'

2002-12-01 Thread Danijel Tasov
Harry Putnam wrote:
 Is there a perl equivalent to the unix `tail' command?  Where I could
 grab the last line from a file without having to read the whole file?

There is the module File::ReadBackwards;

# perl -MCPAN -e 'install File::ReadBackwards'
$ perldoc File::ReadBackwards;

bye,
Da.Ta



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Howto efficiently tail a win32 Logfile?

2002-10-14 Thread Joel Hughes

Hi,
I need to set up a perl job which will process the lastest entries on an
IIS web logfile.

E.g. every (say) 5 mins, a job will run processing new additions to the
file.

What is the best way to go about this?

Should I (when the process runs):
1) read thru the IIS logfile until I reach the desired position
2) process the new records 
3) save the line number of the last (processed) record
4) exit

Or will this 'lock' IIS out of writing to the file during process time
(this is obviously highly undesireable)

If so, should I (when the process runs):
1) take a copy of the (possibly large) logfile
2) read thru the copy until desired position reached
3) process new records
4) save last line number
5) exit

Is this a better approach?


Any thoughts appreciated.

Regards

Joel



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.401 / Virus Database: 226 - Release Date: 09/10/2002
 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




tail -f with cgi

2002-07-12 Thread Max Clark

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]




Re: tail -f with cgi

2002-07-12 Thread zentara

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

2002-07-12 Thread Shawn


- 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

2002-07-12 Thread Bob Showalter

 -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

2002-07-12 Thread Camilo Gonzalez

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

2002-07-12 Thread Shawn


- 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

2002-07-12 Thread Bob Showalter

 -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

2002-07-12 Thread Max Clark

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

2002-06-05 Thread Jenda Krynicky

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

2002-06-03 Thread Tagore Smith

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

2002-06-02 Thread Jenda Krynicky

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]




Tail call optimization

2002-05-28 Thread Tagore Smith

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




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Better tail -f

2002-04-02 Thread Bruno Figueira

Hi there,

I have got the example of trailing a file from Perl Cookbook:

---
for (;;) {
   while (FH) { print $_ }
   sleep 1;
   seek(FH, 0, 1);
}
---

However, if another program removes (all) lines from the file referenced by
FH, perl looses the trailing (for a undetermined time). How do I improve
this code to understand the file was changed?


Cheers,

Bruno


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Better tail -f

2002-04-02 Thread jbajin

How about putting a file lock on it?  

That way if someone tries to access it and write to it, it will not work. 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




  1   2   >