in reply to Re^3: Get contents in an FTP path using glob
in thread Get contents in an FTP path using glob

It is something like this:

!#/usr/bin/perl -w use strict; use Net::FTP; my $hostname = "sample-host"; my $ftp = Net::FTP->new($hostname); $ftp->cwd("mypath"); my @cat1 $ftp->ls("h?_*"); my @cat2 $ftp->ls("*-zips"); print "category 1:\n", join "\n",@cat1; print "category 2:\n", join "\n",@cat2; <>;

I think I must prefer globbing than regex since not all user that will use my program has knowledge in regex.

I have tried converting all * and ? into .* and .? respectively and use regex but before I finish coding it I thought about the "+" character which is a valid char for naming file or directory. It will be a problem when a file/folder contains a "+" char. since it is a special char in regex. And there are many other special char in regex which i don't want to make a code just to escape them all.

It seems that it will become complicated if i use regex. What should I do..?

Replies are listed 'Best First'.
Re^5: Get contents in an FTP path using glob
by ikegami (Patriarch) on Feb 01, 2010 at 21:03 UTC

    which i don't want to make a code just to escape them all.

    Why not? It's just a question of using quotemeta($1) instead of $1.

    my $re_pat = ''; for ($glob_pat) { /\G ([^?*\\]+) /xsgc && $re_pat .= quotemeta($1); /\G \? /xsgc && do { $re_pat .= '.'; redo }; /\G \* /xsgc && do { $re_pat .= '.*'; redo }; /\G \\(.) /xsgc && do { $re_pat .= quotemeta($1); redo }; /\G \Z /xsgc or redo; }
Re^5: Get contents in an FTP path using glob
by hbm (Hermit) on Feb 01, 2010 at 20:44 UTC

    I suggest getting all the files once, and then grep'ing that list twice for your @cat1 and @cat2. I'm not completely sure what you're trying to match, but something like this?

    my @files = $ftp->ls(); my @cat1 = grep { /h+_/ } @files; my @cat2 = grep { /-zips/ } @files;