How to call a Perl file in .BAT file without number of input limitation?

Posted on 2012-09-04
Last Modified: 2012-09-05
I use a bat file to call a Perl file. The reason is to eliminate the need for the .pl extension. This is the code that I use in the .bat file:

@perl //path/to/a/directory/check %1 %2 %3 %4 %5 %6 %7 %8 %9

Open in new window

However, this code has a limitation of maximum 9 inputs. Since my Perl code may have more than 9 inputs, i would like to eliminate this limitation.

Can you please let me know how I can do it?

Question by:Tolgar
    LVL 28

    Expert Comment

    Your reason for using a batch file wrapper is a little odd and unnecessary.

    It is my understanding that batch files are limited to accepting a max of 9 parameters.  If your perl script needs more than that, then don't use a batch file as a wrapper.

    If you want to execute a perl script without having to type its .pl extension, then simply add the .pl extension to the PATHEXT environment variable.
    LVL 82

    Accepted Solution

    Just use "%*" instead of enumerating single variables; "%*" will contain the complete argument list:
    @perl //path/to/a/directory/check %*

    Open in new window


    Author Comment

    @FishMonger: But I am writing this tool for others and due to business requirements, I cannot ask them to add the .pl extension to the PATHEXT environment variable.

    @oBdA: I will try it. I hope it is that simple. Did you try it on a simple example?

    Author Comment

    I found these pages on the web. They seem to be recommending the same method. And the last one is similar to oBdA's approach.

    Can you please help me how to implement it to my example?

    LVL 28

    Expert Comment

    It looks like I was partially wrong.  I am correct in that using batch syntax, you can only access the %1 thru %9 args, however, I did not know that you could use the shift command to walk back through the arg names to access the next set of 9 args.

    It should be noted that if perl was installed correctly, the .pl filetype ext will be configured with "%1" %* to allow for an unlimited number of args.  The solution that oBdA suggests duplicates that capability for the batch script without needing to use the shift command.

    However, IMO, it is an odd if not an awkward and dumb approach to take simply because you don't want to type the .pl extension.

    If this tool is going to be distributed to your customers, a better argument can be made to use perl2exe or similar app to package your tool up as a standalone .exe and not need to worry about writing batch script wrappers simply to eliminate the need to type the ext.
    LVL 28

    Assisted Solution

    There is the most obvious solution that we missed.  Simply leave of the .pl extension when naming the script.  If you do that, you will need to call the perl interpreter directly and pass it the script, which is exactly what you're doing in the batch file.

    D:\perl>type hello
    use strict;
    use warnings;
    use Data::Dumper;
    print "Hello World\n";
    print Dumper \@ARGV;

    Open in new window

    See, no batch file and no .pl extension.
    D:\perl>perl hello bill cathy joe sue mary ed jose scott sally dave john rich

    Hello World
    $VAR1 = [

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Promote certifications in your email signature

    Has your company recently won an award or achieved a certification? They'll no doubt want to show it off. Email signature images used to promote certifications & awards can instantly establish credibility with a recipient and provide you with numerous benefits.

    The use of stolen credentials is a hot commodity this year allowing threat actors to move laterally within the network in order to avoid breach detection.
    Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now