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

Hello, This is probably a dumb question but I'm still pretty new to Perl... I want to create a perl script that will open and read a file, and input that file's text into the CMD line and run that command.... Basically the text file will have any number of commands written in it, and I can pull out a line from the file and run it in the command prompt. Any idea where to start? I haven't been able to find anything helpful yet... only how to read FROM the command line ( i.e. @ARGV, etc. ). My thanks!

##### EDIT #####

So I should probably explain what I'm really trying to achieve ( At the suggestion of Kennethk calling me out on my XY Problem description ).

I plan to have many different text files in a folder... and I want to be able to run each one as a command whenever I want, just by typing in the name of the file... I suppose I could do the files as batch files instead, now that I think about it.. Although I'm still curious as to the process for it. Had some good comments thus far, thanks guys.

  • Comment on Code to Open A File and Run Text From CMD Link

Replies are listed 'Best First'.
Re: Code to Open A File and Run Text From CMD Link
by kennethk (Abbot) on Aug 18, 2014 at 18:50 UTC
    Relevant links:

    Backticks are probably what you actually want. GotToBTru's point is that what you suggest is potentially very insecure. Why a file as opposed to coding it in the script in question? This is a little XY Problem-y.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Code to Open A File and Run Text From CMD Link
by toolic (Bishop) on Aug 18, 2014 at 18:02 UTC
    To run a command from Perl, see system
Re: Code to Open A File and Run Text From CMD Link
by Laurent_R (Canon) on Aug 18, 2014 at 19:03 UTC
    If the file has only CMD-prompt commands, then maybe you want to rename that file with the .bat extension and run it as a batch file from the prompt. Otherwise, maybe, well quite probably, Perl can do most of what you want to do through Perl's internal commands, without having to run a system call to execute a CMD-prompt command.
Re: Code to Open A File and Run Text From CMD Link
by GotToBTru (Prior) on Aug 18, 2014 at 18:24 UTC

    ... and recognize you have opened up Pandora's Box.

    1 Peter 4:10
Re: Code to Open A File and Run Text From CMD Link
by Anonymous Monk on Aug 19, 2014 at 08:09 UTC

    I plan to have many different text files in a folder... and I want to be able to run each one as a command whenever I want, just by typing in the name of the file... I suppose I could do the files as batch files instead, now that I think about it.. Although I'm still curious as to the process for it. Had some good comments thus far, thanks guys.

    You cannot execute text files without changing the text file association ... perl will execute text files (or any files, perl doesn't care about file extension

    The batch processor generally will refuse to process text files, it cares about file extension