how to remove or add code based on file test

Discussion in 'Perl Misc' started by mastermagrath, Feb 16, 2006.

  1. Hi all,

    Can onyone advise me on the following. I have a script that uses 2 gif
    images if they are present on the users machine. If they are not then i
    want to effectively remove the code that would use them on execution of
    the script, here is a snippet of the script:
    The first part tests for the existence, if they do exist then all is
    well.....

    if ((-e ".\\gif1.gif") && (-e ".\\gif2.gif")) {

    my $photo1 = $f1-> Photo('imggif', -file => ".\\gif1.gif");
    my $photo2 = $f1-> Photo('imggif2', -file => ".\\gif2.gif");
    my $progressEntry = $f1-> Label('-image' => 'imggif')->pack(-side =>
    'right');

    }

    However if they don't exist then the following code later in the script
    causes a problem:

    if ($flash == 0) {
    $progressEntry->configure('-image' => 'imggif2');
    $flash = 1;}
    else {
    $progressEntry->configure('-image' => 'imggif');
    $flash = 0;}
    }
    else {$progressEntry->configure('-image' => 'imggif');}

    So how do i effectively remove this second batch of code from being
    compiled if the file test returns false?

    Thanks in advance
    mastermagrath, Feb 16, 2006
    #1
    1. Advertising

  2. Thanks.....
    mastermagrath, Feb 17, 2006
    #2
    1. Advertising

  3. mastermagrath <> wrote:

    > Can onyone advise me on the following.



    Questions should end with a question mark.


    > I have a script that uses 2 gif
    > images if they are present on the users machine. If they are not then i
    > want to effectively remove the code that would use them on execution of
    > the script,



    Why do you want that?

    Why not just have conditional execution?


    > if ((-e ".\\gif1.gif") && (-e ".\\gif2.gif")) {



    If you use single quotes instead of double quotes, then you
    won't have to mentally filter out extra backslashes when
    reading the code:

    if ((-e '.\gif1.gif') && (-e '.\gif2.gif')) {

    If you use sensible slashes instead of silly slashes, then your
    code will be more portable:

    if ((-e './gif1.gif') && (-e './gif2.gif')) {

    If you use the lower precedence "and" instead of "&&", then reading
    and understanding your code gets easier still:

    if ( -e './gif1.gif' and -e './gif2.gif' ) {


    > my $photo1 = $f1-> Photo('imggif', -file => ".\\gif1.gif");
    > my $photo2 = $f1-> Photo('imggif2', -file => ".\\gif2.gif");
    > my $progressEntry = $f1-> Label('-image' => 'imggif')->pack(-side =>
    > 'right');
    >
    > }



    None of those variables will be accessable after the if-block exits,
    so what is the point of setting them?


    > However if they don't exist then the following code later in the script
    > causes a problem:
    >
    > if ($flash == 0) {
    > $progressEntry->configure('-image' => 'imggif2');
    > $flash = 1;}
    > else {
    > $progressEntry->configure('-image' => 'imggif');
    > $flash = 0;}
    > }

    ^
    ^

    Where is the opening curly brace that goes with that closing one?

    If you adopted a sensible indenting scheme, then such things
    would be easier to see.


    > else {$progressEntry->configure('-image' => 'imggif');}



    You can only have one else clause.

    Post real code if you want a real answer.

    Have you seen the Posting Guidelines that are posted here frequently?


    > So how do i effectively remove this second batch of code from being
    > compiled if the file test returns false?



    Why do you want to avoid compiling it?

    Why not simply avoid executing it instead?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Feb 17, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BLF
    Replies:
    4
    Views:
    1,267
    Mike Treseler
    Aug 7, 2004
  2. Simon-Pierre  Jarry
    Replies:
    2
    Views:
    2,348
    Henrik
    Aug 10, 2005
  3. tshad
    Replies:
    6
    Views:
    21,437
    tshad
    Aug 8, 2006
  4. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    680
    Skybuck Flying
    Oct 4, 2009
  5. chris
    Replies:
    4
    Views:
    105
    chris
    Jun 30, 2003
Loading...

Share This Page