LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   listing files Net::FTP (https://www.linuxquestions.org/questions/programming-9/listing-files-net-ftp-555719/)

kshkid 05-22-2007 01:02 AM

listing files Net::FTP
 
Hi All,

Am trying to use Net::Ftp

but unable to list the files available in the ftp server.

When I use,

$ftp->dir()
(or)
$ftp->ls()
it returns a value like ARRAY(0x89f1092)

how to retrieve the file listing from that /

Thanks

ghostdog74 05-22-2007 02:02 AM

Quote:

Originally Posted by kshkid
Hi All,

Am trying to use Net::Ftp

but unable to list the files available in the ftp server.

When I use,

$ftp->dir()
(or)
$ftp->ls()
it returns a value like ARRAY(0x89f1092)

how to retrieve the file listing from that /

Thanks

you just do @files=$ftp->ls(); then you can manipulate the @files array

kshkid 05-22-2007 02:11 AM

I tried that already, but didnt work !

this is the script and the output

Code:

#! /opt/third-party/bin/perl

use Net::FTP;

$server = "ftpserver";
$debuglvl = 1;
$user = "user";
$pass = "password";

$ftp = Net::FTP->new($server, Debug => $debuglvl ) || die "Unable to connect to $server $@ \n";

$ftp->login($user, $pass) || die "Unable to login. ", $ftp->message;

$ftp->ascii || die "Unable to set mode to ascii. ", $ftp->message;

$ftp->cwd("/tmp/") || die "Unable to change directory", $ftp->message;

@files = $ftp->ls() || die "Unable to ls. !!! ", $ftp->message;

foreach (@files) {
  print $_ . "\n";
}

$ftp->quit;

exit 0

Code:

Net::FTP=GLOB(0x87ca6b0)<<< 226 Closing data connection. Transferred 5280 bytes.
ARRAY(0x89154d0)
Net::FTP=GLOB(0x87ca6b0)>>> QUIT
Net::FTP=GLOB(0x87ca6b0)<<< 221 Session Ended. Downloaded 0KB, Uploaded 0KB.


chrism01 05-22-2007 02:15 AM

Try this header:

#!/opt/third-party/bin/perl -w
use strict;

and note that the string concat in perl is '.', not ',' eg;

$ftp->login($user, $pass) || die "Unable to login. ".$ftp->message;
Also, it is preferred to use 'or' here, not '||' .

kshkid 05-22-2007 02:25 AM

Quote:

Originally Posted by chrism01
Try this header:

#!/opt/third-party/bin/perl -w
use strict;

and note that the string concat in perl is '.', not ',' eg;

$ftp->login($user, $pass) || die "Unable to login. ".$ftp->message;
Also, it is preferred to use 'or' here, not '||' .

No, it didnt work!

The same output

ARRAY(oxhex value) and no files being displayed

ghostdog74 05-22-2007 03:20 AM

hmm strange, it seems the array is not derefenced. maybe you can try dereferencing and see.

kshkid 05-22-2007 04:13 AM

Quote:

Originally Posted by ghostdog74
hmm strange, it seems the array is not derefenced. maybe you can try dereferencing and see.


Excellent that worked!

Since the list of files is a long name,

I think a reference is returned and not the list of actual files.

Not sure of the reason

Thanks a lot!

ghostdog74 05-22-2007 06:31 AM

Quote:

Originally Posted by kshkid
Excellent that worked!

Since the list of files is a long name,

I think a reference is returned and not the list of actual files.

Not sure of the reason

Thanks of the lot!

no problem,just a guess , since things like ARRAY(xXXx) usually indicates a reference. :)

kshkid 05-22-2007 07:01 AM

Quote:

Also, it is preferred to use 'or' here, not '||' .

Could you please explain why it is preferred to use 'or' and not '||'. ?

I hope both have got the same meaning !

Thanks :)

0.o 05-22-2007 08:15 AM

Could you post the resulting code from this?

kshkid 05-22-2007 08:39 AM

Quote:

Originally Posted by 0.o
Could you post the resulting code from this?

Sorry! Could you please rephrase your request ?

Thanks!

ghostdog74 05-22-2007 08:40 AM

Quote:

Originally Posted by kshkid
Sorry! Could you please rephrase your request ?

Thanks!

he's saying to post your working code. :)

ghostdog74 05-22-2007 08:44 AM

Quote:

Originally Posted by kshkid
Could you please explain why it is preferred to use 'or' and not '||'. ?

I hope both have got the same meaning !

Thanks :)

here's what i think
Code:

$var = EXPR1 || EXPR2; #same as $var = ( EXPR1 || EXPR2)
and
Code:

$var = EXPR1 or EXPR2; # same as  ( $var = EXPR1) or EXPR2;
correct me if i am wrong.

kshkid 05-22-2007 12:22 PM

But what difference its going to make,

with

Code:

open(FILE, "<", "list") || die "Unable to open file <$!>\n";
Code:

open(FILE, "<", "list") or die "Unable to open file <$!>\n";

Both are definitely the same ! :)

mechdave 05-23-2007 03:50 AM

It is just easier to read this way:
Code:

open(FILE, "<", "list") or die "Unable to open file <$!>\n";


All times are GMT -5. The time now is 09:05 AM.