CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
help

 



yogi
New User

Jan 11, 2012, 12:28 PM

Post #1 of 5 (2148 views)
help Can't Post

#!/usr/bin/perl
use warnings;
use File::Path;
use File::Copy;

my $path = "/home/teddy/perl/test/";
open (TH1, 'TABLE.lst');
while (<TH1>) {
chomp;
my $file = "$_\n";
print "$file";
copy($file,$path);
}
close (TH1);

Code
 
Am getting a error "unsuccessful open on filename containing newline perl"
"unsuccessful stat on filename containing newline perl"


BillKSmith
Veteran

Jan 11, 2012, 3:50 PM

Post #2 of 5 (2141 views)
Re: [yogi] help [In reply to] Can't Post

The second argument of copy cannot be a path. Refer to the documentation for File::Copy.


Quote
The "copy" function takes two parameters: a file to copy from and a
file to copy to. Either argument may be a string, a FileHandle
reference or a FileHandle glob.

Good Luck,
Bill


yogi
New User

Jan 11, 2012, 6:45 PM

Post #3 of 5 (2139 views)
Re: [BillKSmith] help [In reply to] Can't Post

Earlier a simialr thing worked for me. Where the destination was a path. Check this.

use File::Path;
use File::Copy;

my $path = "tardir/dest1/dest2/";
my $file = "test.txt";

copy($file,$path) or die "Failed to copy $file: $!\n";

In Reply To


BillKSmith
Veteran

Jan 11, 2012, 8:25 PM

Post #4 of 5 (2133 views)
Re: [yogi] help [In reply to] Can't Post

Your syntax is documented for the move function of the module. You may have discovered an undocumented "feature" of copy. Probably not a good idea to use it, but it did work in the past.

Your error message complains about a newline. Why are you appending a newline to the filename after chomping it off? You do not do this in the example that you claim "works".
Good Luck,
Bill


histrung
Novice

Jan 12, 2012, 7:05 AM

Post #5 of 5 (2126 views)
Re: [BillKSmith] help [In reply to] Can't Post

I saw the same thing but the next paragraph talks about directories.

Quote
If the destination (second argument) already exists and is a directory, and the source (first argument) is not a filehandle, then the source file will be copied into the directory specified by the destination, using the same base name as the source file. It's a failure to have a filehandle as the source when the destination is a directory.

I agree, why the '\n' added?
If that wasn't there I think it would work, this did.

Code
#!/usr/bin/perl 
use warnings;
use File::Path;
use File::Copy;

my $path = "./dir/";
my $file = "testFile.txt";
print "$file";
copy($file,$path);

This did not.

Code
#!/usr/bin/perl 
use warnings;
use File::Path;
use File::Copy;

my $path = "./dir/";
my $file = "testFile.txt\n";
print "$file";
copy($file,$path);

Output Error:

Code
Unsuccessful stat on filename containing newline at /usr/lib/perl5/5.8.8/File/Copy.pm line 85. 
Unsuccessful stat on filename containing newline at /usr/lib/perl5/5.8.8/File/Copy.pm line 95.
Unsuccessful open on filename containing newline at /usr/lib/perl5/5.8.8/File/Copy.pm line 122.



(This post was edited by histrung on Jan 12, 2012, 7:08 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives