简体   繁体   English

如何使用perl将命令行参数与perl模块一起传递?

[英]How to pass command line arguments along with perl modules using perl?


I treid one of my previous example to pass input and output files via command line arguments using perl? 我整理了上一个示例,以使用perl通过命令行参数传递输入和输出文件?

Previous example: 上例:

[ Is it possible to add the file out in the File::Find::Rule using perl? [ 是否可以使用perl将文件添加到File :: Find :: Rule中?

code which i tried to add input and output file as command line arguments:( generate.pl ).I got struck how to include command line arguments to read and copy the files along with perl modules. 我试图将输入和输出文件作为命令行参数添加的代码:( generate.pl )。我很惊讶如何包括命令行参数来读取和复制文件以及perl模块。

use strict;
use warnings 'all';
use CGI qw(:all);
#generate the user file arguments
use Getopt::Long 'GetOptions';
use File::Path       qw( make_path );
use File::Copy  qw( copy move);
use File::Find::Rule qw( );
GetOptions(
    'prjroot=s' => \my $input_dir,
    'outdir=s'  => \my $output_dir,
); 
my %created;
for my $in (
   File::Find::Rule
   ->file()
   ->name(qr/^[^.].*\.yfg$/)
   ->in($input_dir)
) {
   my $match_file = substr($in, length($input_dir) + 1); 
   my ($match_dir) = $match_file =~ m{^(.*)/} ? $1 : '.';
   my $out_dir = $output_dir . '/' . $match_dir;
   my $out     = $output_dir . '/' . $match_file;
   make_path($out_dir) if !$created{$out_dir}++;
   copy($in, $out);
}

Error occured: 发生了错误:

./generate.pl: line 1: use: command not found
./generate.pl: line 2: use: command not found
./generate.pl: line 3: syntax error near unexpected token `('
./generate.pl: line 3: `use CGI qw(:all);'

perl execution should be as follows: perl执行应如下所示:

I should copy the contents from one directory to another directory along with perl modulES(IE File::Find::Rule) 我应该将内容与perl modulES(IE File :: Find :: Rule)一起从一个目录复制到另一个目录

./generate.pl -prjroot "/home/bharat/DATA" -outdir "/home/bharat/DATA1"

Help me fix my issues . 帮助我解决问题。

You miss the perl comment in the first line: 您会错过第一行中的perl注释:

#!<path_to_perl>/perl

use strict;
use warnings 'all';
use CGI qw(:all);
#generate the user file arguments
.....

Your program will also work if you call it with the perl Interpreter in front of your command: 如果在命令前使用perl解释器调用程序,则该程序也将起作用:

perl ./generate.pl -prjroot "/home/bharat/DATA" -outdir "/home/bharat/DATA1"

You've already seen what the problem is. 您已经了解了问题所在。 But I'll just add that perldoc perldiag gives pretty good explanations for any error message you get from Perl. 但是我只是添加一下, perldoc perldiag对从Perl得到的任何错误消息给出了很好的解释。 In this case, searching for "command not found" would give you this: 在这种情况下,搜索“找不到命令”将为您提供:

%s: Command not found %s:找不到命令

(A) You've accidentally run your script through csh or another shell instead of Perl. (A)您不小心通过csh或另一个Shell(而不是Perl)运行脚本。 Check the #! 检查 #! line, or manually feed your script into Perl yourself. 行,或者自己手动将脚本输入Perl。 The #! #! line at the top of your file could look like 文件顶部的一行可能看起来像

 #!/usr/bin/perl -w 

And that's particularly impressive, given that this error isn't actually generated by Perl - but by your shell. 考虑到此错误实际上不是由Perl产生的,而是由您的shell产生的,这尤其令人印象深刻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM