Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have just finished a converter with perl. But it can only convert one file in one go. Now I want to write a small wrapper programm which can call the converter programm and define the called file, e.g. if I write in command:

 perl wrapper.pl .a

The converter will convert all the files with extension .a. And if I write:

 perl wrapper.pl xxx.a

Then it will only convert the xxx file. Could anyone give me an idea how to implement the wrapper, since I don't have any experiences on it... Thanks a lot!

Replies are listed 'Best First'.
Re: A small wrapper
by toolic (Bishop) on Oct 17, 2013 at 13:57 UTC
    glob
    use warnings; use strict; my $input = shift; my @files; if ($input =~ /^\./) { @files = glob "*$input"; } else { @files = $input; } for (@files) { print "$_\n"; }
Re: A small wrapper
by wjw (Priest) on Oct 17, 2013 at 14:02 UTC
    Personally, I would instead modify my existing code to include the functionality you say you want.

    You might want to collect the desired file specs by accepting arguments that point to a text file that lists all the files that the user wants modified(because they are scattered about the file system) and/or a combination of path/wildcarded filenames from the command line etc... .

    That way you only have to worry about code in one place to maintain.

    Take a look at Getopt::Long for some ideas about what you might want to do regardless of whether you do the wrapper or incorporate the code in the main script.

    Hope that is helpful...


    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results.
Re: A small wrapper
by Laurent_R (Canon) on Oct 17, 2013 at 16:51 UTC

    You could also use a Perl one-liner, something like this (untested):

    perl -e 'map {system "convert.pl $_"} glob $ARGV[0];' "*.a"
      Why not let the shell glob?
      perl -e 'system "convert.pl", $_ for (@ARGV)' *.a

        Right, this is also what I would usually do most of the time because I work mostly on various Unix systems, but I can't do that under VMS, under which I am also doing some of my work, and this also does not work under Windows. I was trying to give a more portable solution (although both under VWS and Windows, single quote would have to be changed into double quotes and vice-versa).