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

Howdy, Could you guys please tell me how to run a Perl program from the Windows CMD? I am on Windows 7 and I searched the Net for answers, but none of 'em worked. I tried opening the Perl program in question that says 'Hello World' but when I open it with the CMD, it just opens up the Perl code in my default text editor (Sublime Text) and I want it to run in the CMD like the bad-ass Perl programmers do it!

This command just opens it - C:\xampp\htdocs>test.pl

Please help, thank you!

  • Comment on How to run a perl program from the Windows CMD?

Replies are listed 'Best First'.
Re: How to run a perl program from the Windows CMD?
by marto (Cardinal) on Mar 06, 2014 at 10:31 UTC

    Open a command prompt, type the following:

    perl -le "print $^X"

    Copy the output from above (e.g. the output for me was D:\perl\bin\perl.exe), run the following from the command prompt (remember to change my example path for the output of the first command):

    assoc .pl=PerlScript ftype PerlScript=D:\perl\bin\perl.exe "%1" %*

    test.pl will now run the script rather than open it in your text editor.

Re: How to run a perl program from the Windows CMD?
by Discipulus (Canon) on Mar 06, 2014 at 10:16 UTC
    Hello and welcome to the monastery.
    It seems you are at the very beginning of your learning path... some tip for you:

      1)Perl is normally not installed in ms Os. that's incredible but is true. you'll need a distro of Perl for your os: consider as better choice (in my opinion) Strawberry Perl or even activeperl
      2)then you have to verify the default association of program in your system: if you have a 'HelloWorld.pl' and when you open it pop-up a text editor it means that the default program id that text editor.
      3)if you have Perl installed you must to be able to launch the command 'perl -V' at bottom of the output you'll find wher your Perl is..
      4)if you have Perl installed but not associated as default for '.pl' files you are able to run 'perl c:\path\to\yourhelloworld.pl'

    consider also to read something like this or that

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: How to run a perl program from the Windows CMD?
by tobyink (Canon) on Mar 06, 2014 at 10:11 UTC

    You could try running:

    perl test.pl
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      Thank you, but it says it's not a recognizable file :(

        That perl isn't a recognizable file? Or test.pl isn't?

        If the former, are you sure that you've installed Perl correctly?

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: How to run a perl program from the Windows CMD?
by Eily (Monsignor) on Mar 06, 2014 at 10:07 UTC

    I might be mistaken on that, but I think you can only have Windows open files from the terminal with the default application. That is, the same one that will open by double clicking your file. So if you change it on one context, the other will follow.

    If you don't want the default behaviour to be "run perl" for every .pl file, you could always give your test file another extension, like .ple. Or there's the solution of creating a batch file to start your script.

      Thank you, I saved it at test.sh and now a pop up appears asking me to choose the correct program to open the file.
Re: How to run a perl program from the Windows CMD?
by Anonymous Monk on Mar 06, 2014 at 10:19 UTC
      Thank you for the awesome articles!
Re: How to run a perl program from the Windows CMD?
by Stefany (Acolyte) on Mar 07, 2014 at 09:28 UTC
    Thank you for the replies everyone, I do have Perl installed on my system because I wrote lots of programs with it, for example this - https://github.com/Stefany93/perl-print-r/blob/master/perl.pl

    I have no problems running Perl programs in the browser, but I want to learn how to run them in the CMD because that way I could write desktop programs and that will be cool.

    Every time I run the commands with perl in the CMD I get the - "'perl' is not recognized as an internal or externalcommand,operable program or batch file."

    Although like I said, I did install Perl long time ago and wrote programs with it, I installed it with the xampp package. Otherwise I think Perl is an awesome language that gets very much under-estimated and people think that Python replaced Perl and that Perl is useless, this is not true.

      For clarity, are you still unable to run perl from the command prompt? If so ensure that the path to perl is in your PATH variable:

      • Right click 'My computer'
      • Select 'Properties' from the drop down list
      • Select 'Advanced'/'Advanced system settings' (depending on OS version)
      • Click 'Environment Variables
      • Add the path to perl.exe in 'System Variables'->'Path'. If you don't know what this path should be search for perl.exe on your system.
      • Open a new command prompt, type perl -v
        Thank you, I did what you told me, I added Perl to the ENV variables like this:
        C:\Program Files (x86)\PC Connectivity Solution\;C:\Program Files (x8 +6)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin;C:\xampp\php +\zend_framework\bin;C:\Program Files (x86)\Lua\5.1;C:\Program Files ( +x86)\Lua\5.1\clibs;C:\xampp\php;C:\ProgramData\Composer\bin;C:\Progra +m Files\TortoiseSVN\bin;c:\Program Files (x86)\Microsoft SQL Server\1 +10\Tools\Binn\;c:\Program Files\Microsoft SQL Server\110\Tools\Binn\; +c:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\xampp\perl\bin +\perl.exe
        And when I run  perl -v in the CMD like this:
        C:\Users\Stefany>perl -v
        I get this error -
        'perl' is not recognized as an internal or external command, operable program or batch file.
        Please help!
Re: How to run a perl program from the Windows CMD?
by Anonymous Monk on May 07, 2017 at 13:13 UTC
    1. Right click 'My computer'
    2. Select 'Properties' from the drop down list.
    3. Select 'Advanced'/'Advanced system settings' (depending on OS version)
    4. Click 'Environment Variables.
    5. Add the path of perl.exe in 'System Variables'->'Path'.
    6. open cmd promt
    7. cd <your_path_where_perl_path_is_saved>
    8. In case your perl file is saved in D drive then, the type D: in cmd promt
    9. run the command perl <perl_file_name>
Re: How to run a perl program from the Windows CMD?
by locked_user sundialsvc4 (Abbot) on Mar 06, 2014 at 16:50 UTC

    Okay, step one ... do you know that Perl is installed on your system and working?   If so, which one (Strawberry, ActiveState, etc.) is it?   How do you know?

    If you open CMD, and type the word perl -V, with a capital-V, exactly what does or does not happen?

    In (I think ...) the System control-panel, is there presently a file-association for .pl?   Right now it would seem that there is not one, and Windows is guessing that it is just text to be edited, but it might also be that .pl is right now associated with Sublime Edit.   If you right-click, are you given a choice for “Open With ...” ??

Re: How to run a perl program from the Windows CMD?
by Anonymous Monk on Aug 19, 2017 at 04:19 UTC
    #!/usr/bin/perl use strict; use warnings; use Path::Tiny; use autodie; # die if problem reading or writing a file my $dir = path("/tmp"); # /tmp my $file = $dir->child("file.txt"); # /tmp/file.txt # Get a file_handle (IO::File object) you can write to # with a UTF-8 encoding layer my $file_handle = $file->openw_utf8(); my @list = ('a', 'list', 'of', 'lines'); foreach my $line ( @list ) { # Add the line to the file $file_handle->print($line . "\n"); }