intrance has asked for the wisdom of the Perl Monks concerning the following question:

Hi! If there is not much trouble I would like to ask something myself. I searched but found nothing on this. I use the code below to get something from FTP but I can't abort(). I want to be able to read from <STDIN> while downloading and if the command is abort then stop the transfer. Can you see anything wrong in my code? Thanks
sub getfile(){ my $file = $_[0]; my $pid; if (!defined($pid = fork())) { die "cannot fork: $!"; } elsif ($pid) { while (1){ my $command = <STDIN>; chomp($command); if($command =~ m/^\s*abort\s*/){ $ftp->abort(); last; } } } else { $ftp->get($file); } }

Replies are listed 'Best First'.
Re: Aborting ftp
by ww (Archbishop) on Dec 29, 2008 at 16:29 UTC
    Which module are you using?

    Your sub appears to work properly -- that is, to download and interactively-abort -- under w2k, when use Net::FTP or use Net::FTP::File and appropriate code is added.

    And, not to your key question, but re the regex: why allow spaces? Seems to me (YMMV) that m/^abort/i would be slightly less subject to user error or mischance.

    Afterthought: Please don't put the narrative part of your question inside code tags; use <p>...</p> instead. (See Markup in the Monastery.)

Re: Aborting ftp
by Marshall (Canon) on Dec 29, 2008 at 16:59 UTC
    I don't know why you are trying to fork another process here. Why doesn't just this work?:
    $ftp->get($file); ##CTRL-C bails - no need for "Abort"
Re: Aborting ftp
by intrance (Initiate) on Dec 29, 2008 at 20:52 UTC

    Hello. Thanks for the replies. Yes I have used NET::FTP. Everything works fine but when a file is being downloaded there is no reply on the shell so that's why I try forking I don't use ctrl+c because I want my program to continue. Is there any way for get() to become silent or something? Sorry but I'm totally newbie. And sorry for the formatting

      I am a "newbie" too. But please confirm this:
      1) You don't need multiple processes downloading multiple files at the same time - correct? If you don't need to download multiple files at once, this is a huge simplification!
      2) It sounds like you run a program in a window and it becomes "stuck"? - e.g. you see no return prompt and it looks like the program hangs?
      Is that what is going on?
      3) If the above is correct, can you back up one step and tell me more about what you are trying to accomplish?

        Your assumptions are correct. I try to make an ftp client with NET::FTP. It's not that I have no prompt, The execution stucks on $ftp->get(). I just want to do other staff. I don't want to waste your time, I just want to know if I did something wrong or if it's just the case with get(). I thought that forking would allow me to read from STDIN. Thanks for your time so far.