0

I treid one of my previous example to pass input and output files via command line arguments using perl?

Previous example:

[Is it possible to add the file out in the File::Find::Rule using perl?

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.

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:

I should copy the contents from one directory to another directory along with perl modulES(I.E File::Find::Rule)

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

Help me fix my issues .

2 Answers 2

1

You miss the perl comment in the first line:

#!<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 ./generate.pl -prjroot "/home/bharat/DATA" -outdir "/home/bharat/DATA1"
Sign up to request clarification or add additional context in comments.

Comments

1

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. In this case, searching for "command not found" would give you this:

%s: Command not found

(A) You've accidentally run your script through csh or another shell instead of Perl. Check the #! line, or manually feed your script into Perl yourself. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.