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

I am new to perl, and so to practice writing to another program, I decided to automate something I do often: FTPing into my email server to retrieve the contents of my inbox. (Since I can't use telnet at work, stupid firewalls.) Here is what I have:
#!/usr/bin/perl open (FTP, "|ftp") || (die "Couldn't fork process"); print FTP "open my.email.server\n"; print FTP "my_userid\n"; print FTP "my_password\n"; print FTP "lcd /windows/desktop\n"; print FTP "get /var/mail/userid\n"; #location of my inbox print FTP "bye\n"; close FTP;
The odd thing is this: it works fin until the point at which it *should* put in the password for me. Then it hangs. If at this point I type the password and press enter, it says "Invalid command", and then continues as normal, putting the inbox file on the desktop as it should. What is going on here? Is there any way around this? Thanks.

Replies are listed 'Best First'.
Re: Writing to a program
by davorg (Chancellor) on Aug 02, 2000 at 00:59 UTC

    Handling things like command line interfaces is always trickier than it seems it will be. You'll always trip up on some little problem like this.

    That's why people write things like Net::FTP which go straight for the underlying protocol. It's easier to use and more efficient. There's no good reason not to use it.

    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>
      Thanks for the help. I'll check it out. :)
Re: Writing to a program
by maverick (Curate) on Aug 02, 2000 at 01:25 UTC
    At the risk of having a thousand flame throwers turned on me, you could also look at a language called expect. It was written with 'pretending to be a human at the keyboard' in mind. For short little tasks like this, it does pretty well.

    /\/\averick

      <pedant>Actually Expect isn't a language at all, but an extension to Tcl (so best not go there!) But there's an Expect.pm on the CPAN which allows you to do all the same things from Perl.</pedant>

      --
      <http://www.dave.org.uk>

      European Perl Conference - Sept 22/24 2000, ICA, London
      <http://www.yapc.org/Europe/>
        ya, you're right. I complete forgot about that. Just goes to show how often I use it :)

        /\/\averick

RE: Writing to a program
by Corion (Patriarch) on Aug 02, 2000 at 12:25 UTC

    Just another small point to add. If you are basically automating a task without interaction, there are already programs for it, namely a port of wget (a web grabber that also does ftp:// links and ncftp a really good free console ftp client. Both can transfer your file in one go.

    Obviously, this has nothing to do with Perl, but I think it is as important to know what other tools are already available out there :-)