how to remove or add code based on file test

M

mastermagrath

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
 
T

Tad McClellan

mastermagrath said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top