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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NET::FTP 1

Status
Not open for further replies.

plotzer

Programmer
Aug 28, 2001
35
US
I am trying to assign a listing of files within an ftp directory to an array. When I try this, all get for the array is the following:

C:\twc\perl>perl ftp_t.pl
ARRAY(0x1c5a4d8)

My code is as follows:

use Net::FTP;
$ftp = Net::FTP->new(&quot;rousest1&quot;, Debug => 0)|| die &quot;Could not open<1>: $!\n&quot;;
$ftp->login('anonymous','qa@qa.com')|| die &quot;Could not open <2>: $!\n&quot;;
@list1 = $ftp->ls || die &quot;LS Failed: $!\n&quot;;
print $list1[0];
$ftp->get(&quot;MyImage.jpg&quot;)|| die &quot;GET Failed: $!\n&quot;;
$ftp->quit;




The file transfer works fine. According tothe documantion DIR or LS is supposed to return a listing of files.

What am I doing wrong.


Thanks

Plotzer
 
Hi Plotzer,

If all you are trying to do is get the list of files in a dir into an array, you can use:

Code:
opendir (FOLDER, $folder) or die &quot;No path available.&quot;;
@fnames = readdir(FOLDER); 
closedir (FOLDER);

This will grab a complete listing of the files in that dir and store them in @fnames

Jim
 
will that work if the directory is on a remote system? I am trying to get files onto my desktop PC from a remote unix server.
 
My goodness you've been patient...... I figured it out. It has nothing to do with using NET::FTP. Rather, it has to do with your use of the 'or' operator, or should I say, your mis-use of the 'or' operator.

Given this statement:
$ftp = Net::FTP->new(&quot;rousest1&quot;, Debug => 0) ||
die &quot;Could not open<1>: $!\n&quot;;

What you are intending to do is watch the 'new' method and if it fails, then die. In fact, what you are doing when the 'new' method fails, you are setting $ftp to the return value of the 'die' function.

You want to do this,
($ftp = Net::FTP->new()) or die;

But, you are doing this,
$ftp = (this || that); # if 'this' fails, then, $ftp is set to 'that'.

So, in general, unless you are in a conditional clause of a statement, don't use '||'. Use 'or', instead. The two versions of the 'or' operator have different behaviors. I remember reading it, but, I don't remember the specifics past what is here.

Given a real ftp server, this works and the only difference is swapping out the '||'s for 'or's.
Code:
use Net::FTP; 
$ftp = Net::FTP->new(&quot;ftp.server.com&quot;, Debug => 0) or 
                  die &quot;Could not open<1>: $!\n&quot;; 
$ftp->login('anonymous','qa@qa.com') or 
                  die &quot;Could not open <2>:$!\n&quot;; 
@list1 = $ftp->ls or die &quot;LS Failed: $!\n&quot;; 
print &quot;$list1[0]&quot;; 
$ftp->get(&quot;MyImage.jpg&quot;) or  die &quot;GET Failed: $!\n&quot;; 
$ftp->quit;
HTH Please use descriptive titles and check the FAQs
and beware the evil typo.
TMTOWTDI ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top