Professional Web Applications Themes

if -s clause - PERL Beginners

was hoping for some add'l assistance. I deleted the bulk of the code out b/c I cannot get it to send me the results of the if clause. Any ideas? I did verify the file is being appended to and is greater that 0 bytes. The if -s on the array is running b/c my else clause is creating the run file with a current timestamp??? -rw-r--r-- 1 root eb 7577 Jun 7 17:53 foreign_tapes.log -rw-r--r-- 1 root other 0 Jun 7 17:53 ftapes_runfile use strict; use warnings; ## Set and edit variables my $foreigntapes="/usr/local/log/foreign_tapes.log"; delete $ENV{'IFS'}; local $ENV{'PATH'} = ...

  1. #1

    Default if -s clause

    was hoping for some add'l assistance. I deleted the bulk of the code out
    b/c I cannot get it to send me the results of the if clause. Any ideas?
    I did verify the file is being appended to and is greater that 0 bytes.
    The if -s on the array is running b/c my else clause is creating the run
    file with a current timestamp???

    -rw-r--r-- 1 root eb 7577 Jun 7 17:53 foreign_tapes.log
    -rw-r--r-- 1 root other 0 Jun 7 17:53 ftapes_runfile


    use strict;
    use warnings;


    ## Set and edit variables

    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";
    #print $ENV{'PATH'},"\n";

    ## Traverse through array and play with data

    open (OUT, ">$foreigntapes") || die "could not open file:$!";
    my ftapes = grep s/^barcode=//, `evmvol -w label_state=1`;
    print OUT "ftapes";
    if ( -s ftapes ) {
    print "file is greater than 0 bytes \n";
    `ls -la /usr/local/log/foreign_tapes.log`;
    foreach (ftapes) {
    print $_;
    #`evmlabel -l st_9840_acs_0 -t 9840S -b$_`
    }
    close (OUT);
    } else {
    my $foo="/tmp/ftapes_runfile";
    open (RUNFILE, ">$foo") || die "could not open runfile:
    $!;"
    }
    close (RUNFILE);

    thank you,

    Derek B. Smith
    OhioHealth IT
    UNIX / TSM / EDM Teams


    DBSMITH@OhioHealth.com Guest

  2. #2

    Default Re: if -s clause



    It is "running", yes, but it is probably attempting to test for a file
    that is named the same as the number of elements of the array. That is
    my best guess at what Perl is doing, however '-s' takes a
    filename/filehandle *NOT* a list.

    perldoc -f -s
     

    This is a backtick call in void context which is worthless,
    non-portable, error prone, slow, etc. If you want information about a
    file use the 'stat' built-in.

    perldoc -f stat
     

    This foreach needs to be moved up outside of the file test. Then test
    each element of the array.
     

    A little surprising Perl isn't complaining about the location of this
    close, but it truly is an amazing language/interpreter. Since you don't
    use OUT after the previous print close it there, which will make the
    code much more readable.
     

    You open this file and then do nothing with it??
     

    HTH,

    http://danconia.org

    Wiggins Guest

  3. #3

    Default Re: if -s clause

    Please bottom post.

    com wrote: 

    Well theoretically that might work since the def in the docs indicates a
    filehandle is possible. I am wondering if maybe it is because your
    output does not contain a new line and is buffered. One of the gurus
    with internals knowledge would be better suited to answer that. You
    might try turning on autoflush for the handle before printing it, or
    alternatively as I already suggested close the filehandle, which will
    force the flush to occur (and conveniently force you to test the file
    rather than the handle which is my next suggestion). This is probably
    the copout approach, but to me the easiest least worrisome approach. No
    guesses needed.
     

    close OUT or warn "Can't close write handle: $!";
     

    if (-s $foreigntapes) {
     

    Like I said before, what are you trying to accomplish with the above?
     

    <snip old posts>

    http://danconia.org
    Wiggins Guest

  4. #4

    Default Re: if -s clause

    I have tried all three methods.... if -s OUT, if -f $filename and if -s
    arrayname with no evail.????
    I did make a test.pl file so that I am just testing whether the file is
    greater than 0 bytes. Here is the code.
    autoflush is turned on. Is this the correct spot for this?

    Is it this tedious to test if a file is greater than 0 bytes????
    I thought perl is supposed to be this great language for UNIX and C
    people???

    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`;
    $| = 1;
    print OUT "ftapes";
    if ( -s OUT ) {
    print "file is greater than 0 bytes \n";
    }






    "Wiggins d'Anconia" <org>
    06/07/2004 10:36 PM


    To: com
    cc: org
    Subject: Re: if -s clause


    Please bottom post.

    com wrote: 
    not 

    Well theoretically that might work since the def in the docs indicates a
    filehandle is possible. I am wondering if maybe it is because your
    output does not contain a new line and is buffered. One of the gurus
    with internals knowledge would be better suited to answer that. You
    might try turning on autoflush for the handle before printing it, or
    alternatively as I already suggested close the filehandle, which will
    force the flush to occur (and conveniently force you to test the file
    rather than the handle which is my next suggestion). This is probably
    the copout approach, but to me the easiest least worrisome approach. No
    guesses needed.
     
    a mail 
    touch a file. 
    verify 
    data 

    close OUT or warn "Can't close write handle: $!";
     

    if (-s $foreigntapes) {
     

    Like I said before, what are you trying to accomplish with the above?
     

    <snip old posts>

    http://danconia.org



    DBSMITH@OhioHealth.com Guest

  5. #5

    Default RE: if -s clause

    com wrote: 

    1. Autoflush is not turned on for the handle OUT. To do that, you would need
    to use:

    my $svsel = select; select OUT; $| = 1;

    or, better yet:

    use IO::Handle;
    OUT->autoflush(1);


    2. Because of buffering, even though you have printed to OUT, the data has
    not actually been written to disk yet (still in the stdio buffers), so

    3. -s OUT returns 0 (which is true)

    The proper test for your program is

    if (ftapes)

    This evaluates the array in boolean (scalar) context, which gives the number
    of elements in the array. If the array is not empty, the test is true,
    regardless of what's going on with the file.
     

    Nope. You are testing, and the file is 0 bytes.
     

    Your algorithim is the problem, not Perl.
    Bob Guest

  6. #6

    Default RE: if -s clause

    ok I can take some blame! : ) I made some changes and I am still not
    getting the print message.


    ## 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";
    }





    Bob Showalter <com>
    06/08/2004 09:45 AM


    To: "'com'" <com>, org
    cc:
    Subject: RE: if -s clause


    com wrote: 

    1. Autoflush is not turned on for the handle OUT. To do that, you would
    need
    to use:

    my $svsel = select; select OUT; $| = 1;

    or, better yet:

    use IO::Handle;
    OUT->autoflush(1);


    2. Because of buffering, even though you have printed to OUT, the data has
    not actually been written to disk yet (still in the stdio buffers), so

    3. -s OUT returns 0 (which is true)

    The proper test for your program is

    if (ftapes)

    This evaluates the array in boolean (scalar) context, which gives the
    number
    of elements in the array. If the array is not empty, the test is true,
    regardless of what's going on with the file.
     

    Nope. You are testing, and the file is 0 bytes.
     

    Your algorithim is the problem, not Perl.



    DBSMITH@OhioHealth.com Guest

  7. #7

    Default Re: if -s clause

    Please bottom post....
     

    See Bob's e-mail too, but why did the -s all of a sudden become a -f in
    the above statement? If this is not a typo, the -f will be true on the
    file if it exists and is normal, but doesn't tell you whether it has size.

    <snip>

    http://danconia.org
    Wiggins Guest

  8. #8

    Default RE: if -s clause

    Please bottom post....
     

    You made *some* changes, but you still haven't listened to what Bob and
    I have said, and made the changes we have pointed out specifically. That
    is becoming tedious.

    I would suggest going back through the e-mails and re-reading every
    single statement made and see why your script still does not match what
    we have suggested.

    http://www.catb.org/~esr/faqs/smart-questions.html

    http://danconia.org
     

    Wiggins Guest

  9. #9

    Default Re: if -s clause

    yes -f is a typo! I want to use -s

    Derek B. Smith
    OhioHealth IT
    UNIX / TSM / EDM Teams
    614-566-4145





    "Wiggins d Anconia" <org>
    06/08/2004 11:02 AM


    To: com, org
    cc:
    Subject: Re: if -s clause


    Please bottom post....
     

    See Bob's e-mail too, but why did the -s all of a sudden become a -f in
    the above statement? If this is not a typo, the -f will be true on the
    file if it exists and is normal, but doesn't tell you whether it has size.

    <snip>

    http://danconia.org



    DBSMITH@OhioHealth.com Guest

  10. #10

    Default RE: if -s clause

    ok so here is what I did and it is now working.

    one thing I do not fully understand is the diff between
    my $svsel = select; select OUT ; $|=1;
    and
    select (select(OUT), $| = 1 ) [0] );

    the second line works while the first does not and
    from my understanding I am selecting OUT and never re-selecting the
    original filehandle?

    thanks!

    Here is the working code.

    ## Set pragmas

    use strict;
    use MIME::Lite;

    ## Set and edit variables

    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";
    #print $ENV{'PATH'},"\n";

    ## Traverse through array and play with data

    open (OUT, ">$foreigntapes") || die "could not open file:$!";
    my ftapes = grep s/^barcode=//, `evmvol -w label_state=3`;
    select( (select(OUT), $|=1 ) [0] );
    print OUT "ftapes";
    if ( -s OUT ) {
    my $msg = MIME::Lite->new(
    From => 'EDM01 <ohnet>',
    To => 'Derek Smith <com>',
    Subject => "Foreign Tapes Found, now attmepting to
    label",
    Data => "ftapes" );
    $msg->send;

    foreach (ftapes) {
    print $_;
    #`evmlabel -l st_9840_acs_0 -t 9840S -b$_`
    }
    } else {
    my $foo="/tmp/ftapes_runfile";
    open (RUNFILE, ">$foo") || die "could not open runfile:
    $!;"
    }
    close (RUNFILE);


    Derek B. Smith
    OhioHealth IT
    UNIX / TSM / EDM Teams
    614-566-4145





    "Wiggins d Anconia" <org>
    06/08/2004 11:06 AM


    To: com, org
    cc:
    Subject: RE: if -s clause


    Please bottom post....
     

    You made *some* changes, but you still haven't listened to what Bob and
    I have said, and made the changes we have pointed out specifically. That
    is becoming tedious.

    I would suggest going back through the e-mails and re-reading every
    single statement made and see why your script still does not match what
    we have suggested.

    http://www.catb.org/~esr/faqs/smart-questions.html

    http://danconia.org
     




    DBSMITH@OhioHealth.com Guest

  11. #11

    Default RE: if -s clause

    com wrote: 

    My bad. I should have added select($svsel) to the first line in order to
    reselect the previous handle (i.e. STDOUT).

    But as I said before, this is NOT the right approach to your problem. Just
    test your ftapes array and be done with it.
    Bob Guest

  12. #12

    Default RE: if -s clause

    that's ok! thanks!
    my goal is to run the system app command only if the file handle OUT is
    greater than 0 bytes else create a runfile. You say test ftapes but you
    never said how?
    is there a better way to just create a file rather than opening it and
    doing nothing.
    currently I am stating

    my $foo="/tmp/ftapes_runfile";
    open (RUNFILE, ">$foo") || die "could not open runfile:
    $!;"



    open (OUT, ">$foreigntapes") || die "could not open file:$!";
    my ftapes = grep s/^barcode=//, `evmvol -w label_state=3`;
    select( (select(OUT), $|=1 ) [0] );
    print OUT "ftapes";
    if ( -s OUT ) {
    .....
    }

    Derek B. Smith
    OhioHealth IT
    UNIX / TSM / EDM Teams
    614-566-4145





    Bob Showalter <com>
    06/08/2004 11:40 AM


    To: "'com'" <com>
    cc: org
    Subject: RE: if -s clause


    com wrote: 

    My bad. I should have added select($svsel) to the first line in order to
    reselect the previous handle (i.e. STDOUT).

    But as I said before, this is NOT the right approach to your problem. Just
    test your ftapes array and be done with it.



    DBSMITH@OhioHealth.com Guest

Similar Threads

  1. Using IN in a WHERE clause
    By drmaves in forum Coldfusion Database Access
    Replies: 15
    Last Post: September 5th, 05:05 PM
  2. CF MX 6 WHERE Clause
    By Jeremy5431 in forum Macromedia ColdFusion
    Replies: 13
    Last Post: May 2nd, 02:26 PM
  3. with clause in sql procedure
    By Leo in forum IBM DB2
    Replies: 5
    Last Post: September 12th, 06:28 PM
  4. Ignoring 'Having' clause
    By London in forum Microsoft SQL / MS SQL Server
    Replies: 3
    Last Post: July 17th, 07:50 AM
  5. Where to put a WHERE clause
    By Dale in forum Microsoft SQL / MS SQL Server
    Replies: 2
    Last Post: July 14th, 03:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139