Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Small Problem with running Perl Scripts in Windows

by Anonymous Monk
on Jun 16, 2003 at 20:01 UTC ( #266271=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm pretty sure someone knows a simple solution to this. Anyway, here goes: I'm a Perl beginner using a Windows XP system with ActiveState's Perl port. I've associated the .pl file type with the my perl.exe file, so I can run perl scripts in a DOS-style window by clicking on them. The problem is that the program terminates and disappears as soon as it executes all of its code. A simple program like:
#! C:\Perl print "Hello, world!";
would run and flash on the screen for only a moment before disappearing. How should I fix this problem and make the program remain on screen until I close it? Thank you.

Comment on Small Problem with running Perl Scripts in Windows
Download Code
Re: Small Problem with running Perl Scripts in Windows
by Ovid (Cardinal) on Jun 16, 2003 at 20:05 UTC

    This is a common problem. You can run it from the command line or, if you want to double click on it, you can add the following two lines to the end of the program:

    print "Hit the <ENTER> key to terminate this program"; <STDIN>;

    Cheers,
    Ovid

    Thanks to grantm for catching my "any key" typo.

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

Re: Small Problem with running Perl Scripts in Windows
by cbro (Pilgrim) on Jun 16, 2003 at 20:10 UTC
    This is similar to an issue I used to deal with using Turbo C++ for DOS programs. The solution there was a getch() statement, so I suggest doing the same thing. That is, at the end of your script add:
    print "Press <Enter> To Continue..."; my $in = <STDIN>;
Re: Small Problem with running Perl Scripts in Windows
by Mr. Muskrat (Abbot) on Jun 16, 2003 at 20:11 UTC
    Take a look at tye's -Mouse. I initially scoffed at it but I've since changed my mind about it. It's quite nice.
      The -Mouse looks like a good idea, but I tried installing it based on the Win 98 post, and it didn't work. What about getting it to work for Win XP? I realize that adding a line for input at the end of each program could correct this problem, but if someone has a way to automatically implement that (i.e. -Mouse, apparently, or any other method), I would appreciate it since I won't have to type it in each time.
        You should be able to follow the directions given in the comments for -Mouse. Go to a command prompt and type:
        assoc .pl=Perl ftype Perl=perl -Mouse "%1" %*
Re: Small Problem with running Perl Scripts in Windows
by BrowserUk (Pope) on Jun 16, 2003 at 20:45 UTC

    Another solution to the problem is to tweak the command associated with your perl scripts. By default this looks (something like) this

    perl_script=e:\perl\bin\perl.exe "%1" %*

    If you modify this to

    ftype perl_script=%comspec% /k e:\perl\bin\perl.exe "%1" %*

    Then the cmd window will remain once the script terminates.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


      I am using WxPerl scripts which run in their own GUI windows. However, when I click on a WxPerl script, the DOS window opens up and remains open until the script terminates. Is there a way to create ftype association (I use .plx extension) to have the DOS window run in "minimized" mode. I using Windows 98 and I am not sure if &comspec% is defined. Thank you for any help you could provided.

        I have a funny feeling I saw a post somwhere that gave instructions for doing this (for Tk gui scripts) but I can't find it. Hopefully, someone else will notice this post and know.

        First, I've never used WxPerl, or Win98 so whether this will work for you I have no idea. This is what I have set up for Tk scripts which prevents any console window being displayed at all under AS 5.8 and NT4.

        P:\test>assoc .plb .plb=perl_background_script P:\test>ftype perl_background_script perl_background_script=wperl.exe "%1"

        In order for this to work, wperl.exe has to have a flag set in the executable to indicate that it is to run in the WINDOWS subsystem rather than as a console app. I cannot remember if it is set up this way out of the box, or whether I had to modify it to be so. The easy way to check is to use the exetype.bat program that you'll find in your perl\bin directory. If this reports the WINDOWS subsystem as below, the above associations should allow you to double-click .plb scripts in the explorer and have them run without any console window showing up.

        P:\test>exetype \bin\wperl.exe \bin\wperl.exe uses the WINDOWS subsystem.

        However, if it reports wperl.exe to be a CONSOLE app,

        P:\test>exetype \bin\perl5.8.0.exe \bin\perl5.8.0.exe uses the CONSOLE subsystem.

        then you need to modify it by adding the WINDOWS parameter on the same command

        P:\test>exetype \bin\wperl.exe WINDOWS

        I suspect that this is the diference between wperl.exe and perl.exe and you don't need to do this, but I included the info in case my memory is wrong, and I had to do this myself previously.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
        If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Small Problem with running Perl Scripts in Windows
by young_david (Friar) on Jun 16, 2003 at 22:36 UTC
    You could also add this line at the end of your script.

    # The script will sleep for 5 seconds before exiting sleep 5;
    See "perldoc -f sleep"
Re: Small Problem with running Perl Scripts in Windows
by chunlou (Curate) on Jun 17, 2003 at 00:16 UTC
    Or, instead of hacking the script, try to use some IDE, such as Perl IDE (it's free, last time I checked) or CodeGenie (not sure it's still free), which will buffer your output in an output window within the IDE. (Of course, unless your script is interactive.)
Re: Small Problem with running Perl Scripts in Windows
by jacques (Priest) on Sep 24, 2003 at 00:53 UTC
    This issue has been raised here numerous times. Everyone is giving you good advice. You might want to look at the code behind Win32::Die as well. There's also a little known module called win. It's still in beta, but it looks promising.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://266271]
Approved by benn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2014-11-26 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (171 votes), past polls