"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 <[EMAIL PROTECTED]> wrote:
> 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

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 =
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 >= 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 = "print something in the file.\n";

    my ($length, $limit);
    {
        use bytes;
        $length = length($msg);
        no bytes;
    }
    $limit = 5000 - $length;

    print STDERR $msg until -s STDERR >= $limit;

HTH,

--jay

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


Reply via email to