STAT function; limit a log file.
------=_Part_3190_12575999.1128434120467
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Hello List,
#!/usr/bin/perl
close(STDERR);
open(STDERR,">/tmp/test.log") or die " Can't do it!!";
for($i=3D0; $i <=3D30; $i++) {
print STDERR "print something in the file.\n";
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime, $ctime,$blksize=
,$blocks)
=3D stat(STDERR);
if ( $size eq '500' ) {
exit(0);
}
}
I want this file not to grow more than 50KB.
How can I go about it. I been stuck here.. this above script will not do th=
e
needful
Thanks in advance,
Cheers,
Umesh
------=_Part_3190_12575999.1128434120467--
Re: STAT function; limit a log file.
Chances are that the file size will never equal exactly 500 bytes. You could
use >= instead of eq, and it should work. Also, you don't have to close
STDERR before reopening it, and in cases where you want to do numeric
comparisons, use == instead of eq. Hope that helps a little. Also, it might
be better to use an until loop, unless you wanted to do no more than 30
iterations. Indentation is good too.
On Tuesday 04 October 2005 09:55, Umesh T G wrote:
> Hello List,
>
> #!/usr/bin/perl
>
> close(STDERR);
>
> open(STDERR,">/tmp/test.log") or die " Can't do it!!";
>
> for($i=0; $i <=30; $i++) {
> print STDERR "print something in the file.\n";
> ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime, $ctime,$blksize
>,$blocks) = stat(STDERR);
> if ( $size eq '500' ) {
> exit(0);
> }
> }
>
> I want this file not to grow more than 50KB.
> How can I go about it. I been stuck here.. this above script will not do
> the needful
>
> Thanks in advance,
>
> Cheers,
> Umesh
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Re: STAT function; limit a log file.
"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 Perl list
On 10/4/05, Stephen Kratzer <kratzers [at] pa.net> wrote:
> Chances are that the file size will never equal exactly 500 bytes. You co=
uld
> use >=3D instead of eq, and it should work. Also, you don't have to close
> STDERR before reopening it, and in cases where you want to do numeric
> comparisons, use =3D=3D instead of eq. Hope that helps a little. Also, it=
might
> be better to use an until loop, unless you wanted to do no more than 30
> iterations. Indentation is good too.
>
> On Tuesday 04 October 2005 09:55, Umesh T G wrote:
> > Hello List,
> >
> > #!/usr/bin/perl
> >
> > close(STDERR);
> >
> > open(STDERR,">/tmp/test.log") or die " Can't do it!!";
> >
> > for($i=3D0; $i <=3D30; $i++) {
> > print STDERR "print something in the file.\n";
> > ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime, $ctime,$blk=
size
> >,$blocks) =3D stat(STDERR);
> > if ( $size eq '500' ) {
> > exit(0);
> > }
> > }
> >
> > I want this file not to grow more than 50KB.
> > How can I go about it. I been stuck here.. this above script will not d=
o
> > the needful
> >
> > Thanks in advance,
> >
> > Cheers,
> > Umesh
As stephen noted, the file size will probably never be 500 bytes. When
you're using stat(), take a slice. There's no point populating all
those variables every time through if you just want one: `$size =3D
stat(STDERR)[7]`. The '-s' test on the filehandle will work, too.
Also, stat returns the size in bytes, not kilobytes, so you're looking
for $size >=3D 5000. and finally, if you really don't want the file to
grow beyond a certain point, you need to stop it early. Use length()
to find out how long your message is, and then subtract. There's a
gotcha there, though, because length returns characters, not bytes, by
default, so you need 'use bytes'. And always use warnings and strict;
they'll save you a lot of trouble in the long run:
#!/usr/bin/perl
use warnings;
use strict;
open(STDERR,">/tmp/test.log") or die " Can't do it!!";
my $msg =3D "print something in the file.\n";
my ($length, $limit);
{
use bytes;
$length =3D length($msg);
no bytes;
}
$limit =3D 5000 - $length;
print STDERR $msg until -s STDERR >=3D $limit;
HTH,
--jay
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Re: STAT function; limit a log file.
------=_Part_9897_5203170.1128500222931
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
On 10/4/05, Jay Savage <daggerquill [at] gmail.com> wrote:
>
> > >
> > > I want this file not to grow more than 50KB.
> > > How can I go about it. I been stuck here.. this above script will not
> do
>
> <snip>
Thanks a lot for all that gyan. :)
Cheers,
Umesh
------=_Part_9897_5203170.1128500222931--