tags:

views:

93

answers:

4

In my main script, I am doing some archive manipulation. Once I have completed that, I want to run a separate script to upload my archives to and FTP server.

Separately, these scripts work well. I want to add the FTP script to the end of my archive script so I only need to worry about scheduling one script to run and I want to guarantee that the first script completes it work before the FTP script is called.

After looking at all the different methods to call my FTP script, I settled on 'do', however, when my do statement is at the end of the script, it never runs. When I place it in my main foreach loop, it runs fine, but it runs multiple times which I want to avoid since the FTP script can handle having multiple archives to upload.

Is there something I am missing? Why does it not run?

Here is the relivant code:

chdir $input_dir;
@folder_list = <*>;
foreach $file (@folder_list)
{
    if($file =~ m/.*zip/)
    {
    print "found $file\n";
    print "Processing Files...\n";
        mkdir 'BuildDir';
        $new_archive = Archive::Zip->new();
        $archive_name = $file;
        $zip = Archive::Zip->new($file);
        $zip->extractTree('', $build_dir);
        &Process_Files;
    }
}
do 'ArchiveToFTPServer.pl';
print "sending files to FTP server";

Thanks

I ended up copying and pasting the FTP code into the main file as a sub. It works fine when I call it at the end of the foreach loop.

A: 

What's the call to the new script? If using a shell, did you check your environment variables?

Tim Green
I updated my comment with the relivant code.
Jeremy Petzold
A: 

Why are you using "do" instead of native Perl FTP module (Net::FTP or something else)?

DVK
In the perl script I am calling I use Net::FTP. I want to avoid copy/paste of the code.
Jeremy Petzold
Isn't that what modules are for? Write your own module with the shared code and have both scripts `use` that module.
Dave Sherohman
Great idea. I'll get on that as soon as I teach myself how to make one.
Jeremy Petzold
Jeremy - useful enough long term that this is a great excuse to do so :)
DVK
I will probably do it so I can come back and refactor this code. I hate having ugly code.
Jeremy Petzold
@Jeremy, see http://stackoverflow.com/questions/1712016/how-do-i-include-functions-from-another-file-in-my-perl-script/1712165#1712165 for discussion on how to write a module.
daotoad
+1  A: 

Add these lines to your scripts:

use strict;
use warnings;

You will now get more diagnostic information, which should lead you to the solution. My current bet is that you are not specifying the correct path to the other script, or that it is missing a shebang line.

Ether
the script is local to the main script. The shebang line is probably correct. let me try that and get back to you.
Jeremy Petzold
One issue with that solution though, it does not explain why the do statment is executed if I place it before the foreach block.
Jeremy Petzold
I just looked. the shebang is there.
Jeremy Petzold
@Jeremy: something in the for loop is aborting your script, but you should get a better idea of what is happening with strict-checking and warnings enabled.
Ether
All strict did was complain that I was not namespacing my variables.
Jeremy Petzold
+4  A: 

Check out the docs for the do 'function'.

In there, you'll find a code sample:

unless ($return = do $file) {
  warn "couldn't parse $file: $@" if $@;
  warn "couldn't do $file: $!" unless defined $return;
  warn "couldn't run $file" unless $return;
}

I suggest putting this code in to find out what's happening with your do call. In addition, try adding warnings and strict to your code to weed out any subtle bugs.

Dancrumb