Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

file copy undefined?

by grashoper (Monk)
on Apr 14, 2009 at 15:54 UTC ( #757422=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to copy files from one directory to another and getting the following error. undefined subroutine &main::Copy called at copy.pl line 11. using strict gives me this..for each variable I have.. global symbol $_ requires explicit package name @ line number..
use File::Copy; use strict; use warnings; $dirpath="C:\\inetpub\\performancetesting\\output\\new\\mlx\\aar"; $dirpath2="c:\\temp"; opendir(IN,"$dirpath") or die "opening directory failed:$!"; while (defined ($file = readdir(IN)) ) { @files=split / /,$file; foreach $filename (@files) { Copy (INPUT, "$dirpath".$filename, "$dirpath2".$filename) or warn "Can +'t open the file yet $filename\n:$!"; until (eof(INPUT)) { chomp($line = <INPUT>); # print contents of the file or do anything print "$line"; } } } closedir(IN);

Replies are listed 'Best First'.
Re: file copy undefined?
by ikegami (Patriarch) on Apr 14, 2009 at 16:02 UTC

    undefined subroutine &main::Copy

    File::Copy exports a sub named copy, not Copy.

    $ perl -le'use File::Copy qw( Copy ); print "ok"' "Copy" is not exported by the File::Copy module Can't continue after import errors at -e line 1 BEGIN failed--compilation aborted at -e line 1. $ perl -le'use File::Copy qw( copy ); print "ok"' ok

    global symbol $_ requires explicit package name

    Well, you won't get that error for $_ specifically — it's special — but it's correctly claiming that you didn't declare any of your other variables.

    ... my $dirpath=... ... while (defined (my $file = ...) ) { ... foreach my $filename (...) { ... } ...
Re: file copy undefined?
by kennethk (Abbot) on Apr 14, 2009 at 15:58 UTC
    Perl is case sensitive, so Copy and copy are different. Your issue is on line 13.

    Update: Missed the second part of the question. When you use strict;, you must explicitly declare each variable with my. You also appear not to be using copy correctly. Working code:

    use File::Copy; use strict; use warnings; my $dirpath="C:\\inetpub\\performancetesting\\output\\new\\mlx\\aar"; my $dirpath2="c:\\temp"; opendir(IN,"$dirpath") or die "opening directory failed:$!"; while (defined (my $file = readdir(IN)) ) { my @files = split / /,$file; foreach my $filename (@files) { copy ("$dirpath".$filename, "$dirpath2".$filename) or warn "Ca +n't open the file yet $filename\n:$!"; open INPUT, "<", $dirpath.$filename or die "Open failure: $!"; until (eof(INPUT)) { chomp(my $line = <INPUT>); # print contents of the file or do anything print "$line"; } close INPUT; } } closedir(IN);
      I still get the following 2 errors not sure why. can't open the file yet . :no such file or directory at copy.pl line 13 open failure permission denied at copy.pl line 14.
        The answer is in your errors: The script does not see the file in question on your system. I would suggest adding print statements on your file paths and then checking if they correspond to the files you think you are opening.
        Do you have a separator between your directories and your filename? You didn't in your original post. ie shouldn't $dirpath end with a pair of backslashes?


        Unless I state otherwise, all my code runs with strict and warnings
Re: file copy undefined?
by ikegami (Patriarch) on Apr 14, 2009 at 16:15 UTC
    Problems you didn't get around to noticing yet:
    • @files=split / /,$file;
      makes no sense. Why would you divide the file name into bits, and treat the bits as file names. You want
      push @files, $file;

    • copy copies a file, it doesn't open it for reading. If you wish to copy the file, use copy. If you wish to read from the file, use open. If you wish to do both, use both. copy does not take three arguments.

    • while ($line = <INPUT>) if used to iterate through the lines of a file.

    use strict; use warnings; my $dir_qfn = "C:\\inetpub\\performancetesting\\output\\new\\mlx\\aar" +; opendir(my $dh, $dir_qfn) or die("Can't read dir $dir_qfn: $!\n"); while (defined(my $fn = readdir($dh)) ) { my $qfn = "$dir_qfn/$fn"; next if !-f $qfn; open(my $fh, '<', $qfn) or do { warn("Can't open file $qfn: $!\n"); next; }; while (<$fh>) { chomp; printf("%03d: %s\n", $., $_); } }
Re: file copy undefined?
by FunkyMonk (Chancellor) on Apr 14, 2009 at 16:12 UTC
    In addtion to what others have told you, you should read the documentation for File::Copy, which says:
    The copy function takes two parameters: a file to copy from and a file to copy to.
    and a little later
    An optional third parameter can be used to specify the buffer size used for copying.

    You've got 3 parameters in your call: a filehandle and the names of two files (there's no need to quote variables, btw).

Re: file copy undefined?
by linuxer (Curate) on Apr 14, 2009 at 17:52 UTC

    Apart from the already mentioned stuff and as it hasn't been mentioned yet: You should consider to use File::Spec and/or File::Spec::Functions to combine your directory and file paths.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://757422]
Approved by kennethk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2022-01-18 16:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (53 votes). Check out past polls.

    Notices?