drock has asked for the wisdom of the Perl Monks concerning the following question:

I am running a system command to a file passing the data to a filehandle, but the if clause does not work. My goal is to run a system app command and if it creates a file greater than 0 bytes then use MIME:Lite to email the data otherwise just "touch" a file.Any ideas? thank you!
## Set pragmas use strict; my $foreigntapes="/usr/local/log/foreign_tapes.log"; delete $ENV{'IFS'}; local $ENV{'PATH'}= "/usr/epoch/bin:/usr/epoch/EB/bin:/usr/bin +:/usr/sbin:/bin:/sbin"; open (OUT, ">$foreigntapes") || die "could not open file:$!"; my @ftapes = grep s/^barcode=//, `evmvol -w label_state=1`; my $svsel = select; select OUT; $| = 1; print OUT "@ftapes"; if ( -s OUT ) { print "file is greater than 0 bytes \n"; }

Replies are listed 'Best First'.
Re: if -s clause on a FILEHANDLE
by Fletch (Bishop) on Jun 08, 2004 at 14:06 UTC

    Erm, why not just if( @ftapes ) { ... }?

    Update: Also note that since you call select OUT and never re-select the old handle that your print will go to the OUT handle. You may want to use the common select( (select(OUT),$|=1)[0] ) idiom instead.

      what exactly is
      select( (select(OUT),$|=1)[0] )
      saying? I still want to state
      if ( -s "@ftapes" ) { ..... }
      right? not just if ( "@ftapes" )

        The select stuff is a hackish shortcut from the dawn of time (or at lest the perl4 days) to set autoflush on a handle without changing the current default output handle afterwards. The chunk inside the inner parens is a list consisting of the previous default output handle (what you were getting with my $svsel = select OUT;) and then setting $| on the default output handle (which is then OUT since the select executes first). The list value is subscripted to get just the first element (the old saved handle) and that's passed to select to reset the default output handle.

        And no, you don't want  -s "@ftapes". -s gets the size of a file (either by name or for an already opened filehandle). Yours would attempt to find the size of a file named after the contents of @ftapes joined by $,. Using an array in a scalar context evaluates to the number of items in the array; if you found any items then @ftapes will have a non-zero number of elements and evaluate to a true boolean value.

Re: if -s clause on a FILEHANDLE
by pelagic (Priest) on Jun 08, 2004 at 13:57 UTC
    Close the file before asking it's properties.


      You don't have to close it, but you do need to make sure that any writes that you want considered have been flushed (either by setting $|, calling $fh->flush, or closing it if you really are done).</pedant>

Re: if -s clause on a FILEHANDLE
by BrowserUk (Patriarch) on Jun 08, 2004 at 14:45 UTC

    If you want to know how much was written prior to closing the file, use tell to tell you :)

    print OUT "@ftapes"; if( tell( OUT ) ) { ... }

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail