Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nagging Subroutine

Status
Not open for further replies.

Ramnarayan

Programmer
Jan 15, 2003
56
0
0
US
Hi,

I just made a simple subroutine to check whether the zip file has jpg files. However the script is working great. But whenever it tests a zip file that does not have a jpg file, it should do the other part of the loop. This is not working... Can someone help me!

my $has_jpg = "FALSE";
open (JPG, "unzip -t fff.zip | grep jpg |") or die "Can't get JPG files from zipfile\n";
while(<JPG>)
{
if (m/OK\s*$/)
{
$has_jpg = &quot;TRUE&quot;;
last;
}
}
close (JPG);
if ($has_jpg = &quot;TRUE&quot;)
{
&withjpg;
}
elsif ($has_jpg = &quot;FALSE&quot;)
{
&nojpg;
}
sub withjpg
{
!system(&quot;unzip -qq $fff.zip *.txt *.jpg *.rpt&quot;) or die (&quot;can't unzip zip file: $!&quot;);
}
sub no jpg;
{
!system(&quot;unzip -qq fff.zip *.txt *.rpt&quot;) or die(&quot;can't unzip zip file: $!&quot;);
}
 
Why are you using;

elsif ($has_jpg = &quot;FALSE&quot;)

Think about it.... $has_jpg won't be set to FALSE....you need to just use an 'else', rather than 'elsif'

That should do it :)

Cheers

Andy
 
Andy,

That does not work either. I replaced elsif (....) with else and that too did not work. Please help me out!
 

From your example above -

sub no jpg;
{
!system(&quot;unzip -qq fff.zip *.txt *.rpt&quot;) or die(&quot;can't unzip zip file: $!&quot;);
}


sub no jpg; - remove the ';' should do it.




Agree with changing the elseif too, good programming practice.


Carl.
 
Also, the nojpg has a space in it ;)

sub no jpg;


Try the following code;

Code:
my $has_jpg = &quot;FALSE&quot;;
open (JPG, &quot;unzip -t fff.zip | grep jpg |&quot;) or die &quot;Can't get JPG files from zipfile\n&quot;;
while(<JPG>)
{
    if (m/OK\s*$/)
    {
        $has_jpg = &quot;TRUE&quot;;
        last;
    }
}
close (JPG);
if ($has_jpg = &quot;TRUE&quot;)
{
    &withjpg;
}
else ($has_jpg = &quot;FALSE&quot;)
{
    &nojpg;
}

sub withjpg
{
    !system(&quot;unzip -qq $fff.zip *.txt *.jpg *.rpt&quot;) or die (&quot;can't unzip zip file: $!&quot;);
}

sub nojpg;
{
    !system(&quot;unzip -qq fff.zip *.txt *.rpt&quot;) or die(&quot;can't unzip zip file: $!&quot;);
}
 
[tt]if ($has_jpg = &quot;TRUE&quot;)[/tt]

is assigning the string &quot;TRUE&quot; to [tt]$has_jpg[/tt], and returning it. This is always true.

You want:
[tt]if ($has_jpg eq &quot;TRUE&quot;)[/tt]
 
Good point Carl, you will need to remove the ; off the end of the following line that I gave you;

sub nojpg;

I managed to miss that one :p

Cheers

Andy
 
This is my refined code after correcting of all the mistakes mentioned above. Thanks to you all for pointing it out. However, still the script does not work for zip files without jpg files.

my $has_jpg = &quot;FALSE&quot;;
open (JPG, &quot;unzip -t fff.zip | grep jpg |&quot;) or die &quot;Can't get JPG files from zipfile\n&quot;;
while(<JPG>)
{
if (m/OK\s*$/)
{
$has_jpg = &quot;TRUE&quot;;
last;
}
}
close (JPG);
if ($has_jpg = &quot;TRUE&quot;)
{
&withjpg;
}
else
{
&nojpg;
}
sub withjpg
{
!system(&quot;unzip -qq $fff.zip *.txt *.jpg *.rpt&quot;) or die (&quot;can't unzip zip file: $!&quot;);
}
sub nojpg
{
!system(&quot;unzip -qq fff.zip *.txt *.rpt&quot;) or die(&quot;can't unzip zip file: $!&quot;);
}
 
Hey all,

Thanks for the wonderful tips. I got what Rosenk said. It is the problem due to the &quot;=&quot; assigner operation. I changed it to &quot;eq&quot; and the script worked wonders.

Thanks to all for helping me out.
 
What's with all the exclamation marks in front of your system calls??
 
Toolkit,

This was told to me during a training session. If there is no reason for me to put the exclamation marks, Please enlighten me. Thanks for pointing it out to me!
 
Actually you are quite right.
System returns 0 for success, and non zero for failure, so you can use either:
Code:
1) system(xxx) and die &quot;yyy&quot;;
OR:
2) !system(xxx) or die &quot;yyy&quot;;
The 'and' and 'or' here are short circuit logical operators, so in both 1) and 2) the die clause will only get called if system returns non zero (fails). However, there is no value in doing:
Code:
!system(xxx)
Sorry if I confused you :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top