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

The code i have written is
#! /usr/bin/perl use File::Copy::Recursive; print "Hi this is a script to copy a file \n"; print " Enter the name of the file to be copied with the compete path\ +n"; $Source = <STDIN>; chomp($Source); print "Enter the full path of the Destination \n"; $Destination = <STDIN>; chomp($Destination); if( -e $Source) { print "copying $Source to $Destination \n"; copy("$Source","$Destination")or die "File cannot be copied."; } else { print " The File doesnot Exist \n"; }
This code throws the error "Undefined subroutine &main::copy called at ./test1.pl line 12, <STDIN> line 2." I dont understand why this error is thrown. Request someone to help me fix this.

Replies are listed 'Best First'.
Re: Undefined subroutine &main::copy called
by marto (Cardinal) on Dec 14, 2010 at 10:15 UTC
Re: Undefined subroutine &main::copy called
by chrestomanci (Priest) on Dec 14, 2010 at 10:17 UTC

    File::Copy::Recursive does not export any functions by default. You need to change line 2 of your script to specify the function(s) you need. Something like:

    use File::Copy::Recursive qw(rcopy);

    Secondly, that module does not export a copy function. If you want to copy a single file, you need to use copy from File::Copy instead. If you want to copy a whole tree, you need to change line 12 of your script to one of the recursive copy functions from File::Copy::Recursive, probably rcopy.

      If you want to copy a single file, you need to use copy from File::Copy instead.

      Not exactly true. rcopy will happily copy a single file too.

    A reply falls below the community's threshold of quality. You may see it by logging in.