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

Hi I just want to ask if there is a way I can get list of contents of an ftp path using a globbed string?

I have tried:

@con = $ftp->ls("glob*.???"); #and @con2 = $ftp->dir("glob*.???");

but they dont produce the output that I'm looking for.

The 'dir' supports globbing but it outputs additional datas.

It seems that LS and DIR commands of FTP(as well as some of its commands) have their own option such as -l, -w, etc. but the documentation of this is hard to find in the net.

I know it's possible to do this using regex but I just want to know if their is a very simple way in perl to do this so I can add it to my reference and use it also in my current project.

I need your help monks... I hope you have what I'm looking for.

Thanks in advance.

Replies are listed 'Best First'.
Re: Get contents in an FTP path using glob
by aquarium (Curate) on Feb 01, 2010 at 22:36 UTC
    glob works directly on file system so doesn't somehow extend ftp protocol beyond it's RFC implementation. In other words, you can only execute ftp commands (and parse their output), which includes a very basic filename matching similar to DOS in the limited FTP command set.
    the hardest line to type correctly is: stty erase ^H
      As a followup to aquarium's post, here's an example that uses a simple print and Dumper:
      #!/usr/bin/perl use Net::FTP; use Data::Dumper::Concise; use constant HOST => 'ftp.cpan.org'; use constant DIR1 => '/pub/CPAN/authors'; my $ftp = Net::FTP->new( HOST, Debug => 1, Passive => 1, Timeout => 1 ); $ftp->login('anonymous'); $ftp->cwd(DIR1); print Dumper $ftp->dir(<DIR1>); $ftp->quit;
      Note: The list of authors isn't current because its not maintained anymore. I only use it for examples. Update: Or you could use a glob and ls:
      #!/usr/bin/perl use Net::FTP; use Data::Dumper::Concise; use constant HOST => 'ftp.cpan.org'; use constant DIR1 => '/pub/CPAN/authors'; use constant GLOB => '*.???'; my $ftp = Net::FTP->new( HOST, Debug => 1, Passive => 1, Timeout => 1 ); $ftp->login('anonymous'); $ftp->cwd(DIR1); print Dumper $ftp->ls(<GLOB>); $ftp->quit;
Re: Get contents in an FTP path using glob
by molecules (Monk) on Feb 01, 2010 at 19:32 UTC
    Am I correct in assuming that you are using the Net::FTP module?

      Yes, I'm using Net::FTP module.

      I want to search a directory content of a path in ftp based on glob. I want it just what the DOS command 'dir /b' does but 'ls' command of ftp has weird behavior when using glob chars.

        I think you want to grep the list returned by $ftp->ls. For example, I case-insensitively get a file like this:
        ... my $guess = shift; my $file = (grep { /$guess/i } $ftp->ls )[0] or return "Couldn't find $guess.\n"; ...