in reply to Re: Undefined subroutine &main::copy called
in thread Undefined subroutine &main::copy called

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

Replies are listed 'Best First'.
Re^3: Undefined subroutine &main::copy called
by chrestomanci (Priest) on Dec 14, 2010 at 13:07 UTC

    If you want to copy a directory and all it's contents, then you need rcopy or dircopy from File::Copy::Recursive

    marto recommend that you enable strict an warnings, and sort out the issues raised. Have you done that?

    In your first post you used <code> tags correctly, so you clearly know how to use them, but this time you did not. Could you edit that last post to put the code tags back in.

      The code is given below.
      #! /usr/bin/perl -W use File::Copy::Recursive qw (dircopy); use warnings; 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"; dircopy("$Source","$Destination") or die "File cannot be copied. +"; } else { print " The File doesnot Exist \n"; }

        Nihad Nizar:

        A couple random comments:

        • When you're using a variable as a string, there's no need to quote it. You typically only do so when you're trying to combine it with other elements in the resulting string.
        • If you add $! to your failure message, you may get some helpful clues as to the reason that your files aren't copying.
        • A little whitespace in a program can enhance the readability. In the slightly modified version of your code below, I put a blank line around the section to read the source location and the destination location. That way, if you have a problem with a section, your eye can easily see hich lines are involved in that section.
        #! /usr/bin/perl -W use File::Copy::Recursive qw (dircopy); use warnings; 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"; dircopy($Source, $Destination) or die "File cannot be copied: $! +"; } else { print " The File does not Exist \n"; }

        I personally don't like having code after the opening brace ({) of a code block, but I didn't change that as it's only a preference, and doesn't affect readability/maintainability/etc.

        I'd also advise you to error-check things as soon as possible, rather than later. It simplifies code flow in most cases, and keeps related things together. Consider this version of your program:

        #! /usr/bin/perl -W use File::Copy::Recursive qw (dircopy); use warnings; 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); if ( -e $Source) { die " The File does not Exist \n"; } print "Enter the full path of the Destination \n"; $Destination = <STDIN>; chomp($Destination); print "copying $Source to $Destination \n"; dircopy("$Source","$Destination") or die "File cannot be copied.";

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        You still don't have use strict;, you don't need both #!/usr/bin/perl -w and use warnings;, you don't display $! in the event of a copy failure. Re read the module documentation.

        #!/usr/bin/perl use strict; use warnings; use File::Copy::Recursive qw (rcopy); print "Please enter source file or directory: "; my $source = <STDIN>; chomp($source); if ( -e $source ){ print "Please enter target: "; my $target = <STDIN>; chomp($target); print "Copying $source to $target\n"; rcopy($source, $target) or die "Copy failed: $!"; }else{ print "Source $source does not exist\n"; }