in reply to file::spec and catfile?

Sorry, I don't mean to be obstinate, but I don't understand the difficulty you're having.

I take it you're trying to combine $path and $filename into $x like this: $x = File::Spec->catfile($path, $filename);and the result is not what you want. Can you describe what you're expecting and what you're getting?

To answer your direct question: yes, it can be done. ;-)

Replies are listed 'Best First'.
Re: Re: file::spec and catfile?
by KStowe (Novice) on Jun 29, 2001 at 20:03 UTC
    HAHA, I see what you mean, my fault...let me just come clean.

    I am trying to write a script to delete a list of files from a single file specified by the user and a path specified by the user also. The thing is, I'm clueless on how to join the $path from the STDIN and the array given from the original file. This is what I've got:
    use File::Spec; print "File Name?\n"; $ToBeDeleted = <STDIN>; open(IN, $ToBeDeleted) or die " The file $TheFile .....Missing In Acti +on.\n"; @arr = <IN>; print "Directory where files are located?\n"; $Path = <STDIN>; $x=File::Spec->catfile("$path","@arr"); unlink $x;
    unlinking should be easy once I get the elements of the array and the path finally combined I think. Any ideas? Thanks...sorry that first post was so unclear, I'm running on 4 hours of sleep!!
      You're forgetting about the newlines that you get with the <STDIN> operators. Your example would be better written as:

      use strict; use File::Spec; print "File Name?\n"; my $ToBeDeleted = <STDIN>; chomp $ToBeDeleted; open(IN, $ToBeDeleted) or die " The file $TheFileToBeDeleted .....Miss +ing In Action.\n"; my @arr = <IN>; chomp @arr; print "Directory where files are located?\n"; my $Path = <STDIN>; chomp $Path; foreach my $file (@arr) { my $x=File::Spec->catfile($Path, $file); unlink $x; }
      You have a few problems other than the newlines: for one thing, you put quotes around your variables, interpolating them into strings (why do people do this?). This will make your @arr into a space-separated string, which is not what you want. And you're not processing each of your files separately through catfile.
      OK, the problem is that you can't stick an array as an argument to catfile to do what you're trying to do. (Not that you can't use an array, it just won't do what you want.) Unless you've changed some of Perl's variables, the "@arr" is flattening the list of filenames into a single string, separating the file names with blanks. (Also, you don't have to quote $path, the first argument. Looks like you're used to shell scripting. You may want to look at Perl traps for the unwary.)

      Here's one way to code your loop:

      foreach my $filename (@arr) { $x = File::Spec->catfile($path, $filename); unlink $x; }
      This is a rather verbose way, but you can see the correspondence to your code. Here's a one-line way to do it: unlink map {File::Spec->catfile($path, $_)} @arr;Warning: this will probably a touch off a round of golf ;-)

      HTH