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

Good morning everyone! Can any monks help me out?

I've been looking for more info on file::spec and catfile, but I can't really find anything...it all appears to be the same information that I keep reading over and over! I've tried perlman:File::Spec, but it doesn't much elaborate on catfile. I've also tried searching everywhere for catfile, but nothing shows up there? search:catfile

I am trying to combine a path ($path) and a filename ($filename) into one single variable ($x), and thought about using catfile to do it. Can it be done, or am I sadly mistaken? Thanks for the help.

--KStowe

Replies are listed 'Best First'.
Re: file::spec and catfile?
by VSarkiss (Monsignor) on Jun 29, 2001 at 19:47 UTC
    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. ;-)

      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

Re: file::spec and catfile?
by LD2 (Curate) on Jun 29, 2001 at 19:48 UTC
    It can be done... here's an example (win32 system):
    use File::Spec; my $file; my $dir; my $wholefile; $dir = "c:\\perl\\bin\\"; $file = "perldoc.bat"; $wholefile = File::Spec->catfile($dir,$file); print "file: " .$wholefile;
    You may want to read the documents again... as well as search Google.

    Update: Eep, I must be on a roll today... anyways.. VSarkiss has answered it a bit more concisely!