复制文件时未定义的子程序调用

时间:2014-10-22 10:48:36

标签: perl

我正在尝试使用perl将文件从一个文件夹复制到另一个文件夹。这些文件夹将作为用户的输入传递。

示例:

First folder : \\filesource\source 
Target folder: \\filesource\destination

但在复制文件时,我收到以下提到的错误:

Undefined subroutine &main::Copy called

代码:

use strict;
use File::Copy;
my $errExitCode_const = 1;
unless( @ARGV == 2){
    print "$0 - requires 2 input parameters and only " .
        @ARGV ." are being passed. \n$!\n";
    exit( $errExitCode_const );
}

## Get parameters
my $source_dir  = $ARGV[0];
my $dest_dir  = $ARGV[1];

## Initiazise Variables
my @files;
my $file_name;
my $filename;
my $dt_stamp;

## Change the directory path.
chdir($source_dir);

# Reads all files from the input directory.
@files = <*>;

foreach $filename(@files) {   
   $file_name =  substr($filename,0,19);
   print "\nfile_name:" . $file_name;
   if($file_name =~ "PROVIDER_KEY_IN_IGF") {
        Copy($file_name,$dest_dir.$file_name)
            or die "Failed to copy $file_name :$!\n";
    }
  #Delete input file #unlink($filename);
} ## End foreach

1 个答案:

答案 0 :(得分:2)

来自File::Copy

的文档
  

The File::Copy module provides two basic functions, copy and move , which are useful for getting the contents of a file from one place to another.

您需要使用模块提供的功能,而不是按名称调用模块。

这一行:

File::Copy($file_name,$dest_dir.\$file_name)
    or die "Failed to copy $file_name :$!\n";
因此

copy($file_name, $dest_dir.$file_name) or die ...;

如果要在文件名前添加斜杠,则需要将$dest_dir$file_name放在引号中或在斜杠周围使用引号,即:

copy($file_name, $dest_dir."\".$file_name)