in reply to Unlink files but not in pwd

hi ,

well you should define the path to the file you want to delete :)

something like this:

opendir (DIRHANDLE, "./dir") or die "Cannot open directory $!\n"; my @l = grep (/hh.txt/i, readdir DIRHANDLE); unlink("./dir/$l[0]"); closedir DIRHANDLE;
the point is that when you call readdir on a dir you get an array of names of the files in that dir and not the full path to the file in dir

Replies are listed 'Best First'.
Re^2: Unlink files but not in pwd
by athanasia (Pilgrim) on Mar 27, 2009 at 16:23 UTC
    Hi baxy77bax,

    Very true :-). However, this kind of implies that I cannot delete the whole list @l at once and using a foreach loop is inevitable. Anyway, don't know why I insist so much on a one-liner, I am usually quite verbose when coding.

    Thanks,
    Athanasia
      You can avoid the foreach if you use map:
      opendir(my $dh, './dir') || die "opendir: $!"; unlink map { "./dir/$_" } grep { /$mypattern/i } readdir $dh; closedir $dh;