Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Tk Program Converted to Executable Requires 2 ENTERs to Run

by roho (Bishop)
on May 24, 2021 at 02:38 UTC ( [id://11132940]=perlquestion: print w/replies, xml ) Need Help??

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

#!/usr/bin/perl ############################# # Name: Clock-Tk.pl # Desc: Display Tk clock. ############################# use strict; use warnings; use Tk; my ($n,$mw); my $mw_width = 420; my $mw_height = 40; my $mw_left = 2570; my $mw_top = 20; $mw = MainWindow->new(); $mw->geometry("${mw_width}x${mw_height}+${mw_left}+${mw_top}"); $mw->bind('<Escape>' => sub {exit;}); $mw->optionAdd('*font','Courier 20 bold'); $mw->Label(-textvariable=>\$n,-background=>'lavender')->pack; $mw->repeat(100,sub{$n=scalar localtime time}); MainLoop();

I am running Strawberry Perl 5.24.1 on Windows 10. I copied the Tk clock one-liner that Discipulus put in Cool Uses for Perl and made it into a script, adding a "geometry" statement to place it where I want on the screen. The script runs fine. I converted the script to an executable using perl2exe Pro. The executable also runs fine.

The problem occurs when I start the executable in a cmd window with the "/b" option to free my cmd window to issue other commands. After entering "start /b Clock-Tk.exe" and pressing ENTER, I must press ENTER a second time before the program starts.

I have numerous other TK scripts that I have converted to executables using perl2exe Pro, and none of those require a second ENTER to start.

I cannot see what is unique to this program that is causing the problem. Does anyone have an idea what is causing this script to require a second ENTER in order to start?

Thanks.

"It's not how hard you work, it's how much you get done."

Replies are listed 'Best First'.
Re: Tk Program Converted to Executable Requires 2 ENTERs to Run
by pryrt (Abbot) on May 25, 2021 at 14:22 UTC
    I knew I had seen this before, but couldn't remember where or how to fix it.

    It seemed similar to stevieb's recent question, but that one was "solved" by the problem not coming back on a new VM.

    I can confirm if I copy your Tk script to tk.pk and run it in various ways (even without compiling it to an executable), I can replicate your problem under certain circumstances:

    perl tk.pl & rem ok tk.pl & rem ok start perl tk.pl & rem ok start tk.pl & rem ok start /b perl tk.pl & rem requires second ENTER, it is the start /b op +tion that's confusing things start /b tk.pl & rem requires second ENTER, same culprit

    But one of the things you're trying to do with start /b is get rid of the command window; perlwin32 #Miscellaneous-Things explains that you should use wperl.exe for that:

    start /b wperl tk.pl & rem does not require second ENTER wperl tk.pl & rem does not require start/b and doesn't requir +e a cmd.exe window to be created, even when run from Run dialog or fr +om a file association

    With that, you could use a separate extension (maybe .pltk or .tkpl) and associate that extension with wperl.exe instead of perl.exe, and that should work in all locations, without needing to compile to executable, and it will run without having a cmd.exe window opening.

    In the Anonymous Monk's shotgun of links, Discipulus had found an old MS KB article 321788, where it points out that it's STDOUT and STDIN causing the problem (which is why wperl gets around it, because those handles aren't used because there's no "console" window for wperl). But that article also shows editing the registry to add a value to HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer of InheritConsoleHandles (REG_DWORD) = 1. When I try after the registry fix:

    start /b perl tk.pl & rem no longer requires second ENTER after regist +ry fix start /b tk.pl & rem no longer requires second ENTER after regist +ry fix

    Oh, right, and pp has the --gui (aka -g) option, which allows for compiling so that it runs without the console. Maybe your compiler has a similar option.

    To sum up:

    So, if you're using an association and don't want to compile, use wperl for Tk scripts (or Win32::GUI scripts, or anything that doesn't need STDIN/STDOUT/STDERR console).

    pp --gui should allow you to make a compiled perl script .exe which runs without opening console, and other compilers may have similar options.

    If you want to use a compiled version with your compiler if it doesn't have a similar option, then you can should be able to change the registry as described above, and now start/b should launch the script or the compiled script without a cmd.exe window being created. (Sorry, I don't have your compiler, so I cannot test your exact situation; but since I could replicate without the compiler, and the registry solved the problem, I think it will solve the problem.)

Re: Tk Program Converted to Executable Requires 2 ENTERs to Run -- pp packed
by Discipulus (Canon) on May 24, 2021 at 19:00 UTC
    Hello roho,

    glad someone recognized how much cool my oneliner was... :)

    Seriously: I dont think here around there is much experience with perl2exe but I just packed your version (after resetting $mw_left and $mw_top to 0 ) using pp -o tkclock.exe tkppclock.pl

    I cannot notice any weird behaviour using start /b tkclock.exe nor by doubleclickind the executable.

    Can you please try to use PAR::Packer pp program to see if the resulting executable behaves normally? Can you reduce your original program to the smallest minimum code that reproduce the problem you are facing?

    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.
      Hello Discipulus. I agree your clock is far better. I ran pp, but the resulting executable still requires pressing ENTER twice to run. Double-clicking works fine (for that matter, double-clicking on my perl2exe executable also works fine). I am not aware of anything unusual about the Windows 10 cmd.exe on my system. This is sure a head scratcher.

      Meanwhile, I have added the executable to the registry "Run" key so the clock comes up automatically when windows is restarted. BTW, the reason I set $mw_left so high is so it will display on the right monitor (more out of the way).

      I discovered that adding the executable in the "Run" key as "cmd.exe /c start /min C:\~Zipfile\B\Bat\Clock-Tk.exe" starts the program minimized, so it's cmd window is not displayed on the desktop (much cleaner).

      As for the double ENTER, I will keep searching. The program as it stands is pretty much the smallest minimum code that reproduces the problem.

      "It's not how hard you work, it's how much you get done."

Re: Tk Program Converted to Executable Requires 2 ENTERs to Run
by roho (Bishop) on May 25, 2021 at 19:40 UTC
    I found the issue with the "start" command. I was using the "/b" option (Start application without creating a new window), which caused me to press ENTER twice to start the program. Using "/min" instead removes the "double ENTER" problem and does not display a separate "cmd" window, which is what I was after in the first place. Thanks to everyone who responded.

    "It's not how hard you work, it's how much you get done."

Re: Tk Program Converted to Executable Requires 2 ENTERs to Run
by Anonymous Monk on May 25, 2021 at 13:11 UTC
Re: Tk Program Converted to Executable Requires 2 ENTERs to Run
by Anonymous Monk on May 25, 2021 at 06:50 UTC
    What is the Out put of perl -V
      Here is the output of perl -V. What does it tell you about why ENTER must be pressed twice when "starting" the Tk program which has been converted to an executable? I would like to know for future reference. Thanks.

      perl -V Summary of my perl5 (revision 5 version 24 subversion 1) configuration +: Platform: osname=MSWin32, osvers=6.3, archname=MSWin32-x64-multi-thread uname='Win32 strawberry-perl 5.24.1.1 #1 Mon Jan 16 02:00:29 2017 +x64' config_args='undef' hint=recommended, useposix=true, d_sigaction=undef useithreads=define, usemultiplicity=define use64bitint=define, use64bitall=undef, uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='gcc', ccflags =' -s -O2 -DWIN32 -DWIN64 -DCONSERVATIVE -DPERL +_TEXTMODE_SCRIPTS -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -fwrapv + -fno-strict-aliasing -mms-bitfields', optimize='-s -O2', cppflags='-DWIN32' ccversion='', gccversion='4.9.2', gccosandvers='' intsize=4, longsize=4, ptrsize=8, doublesize=8, byteorder=12345678 +, doublekind=3 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=1 +6, longdblkind=3 ivtype='long long', ivsize=8, nvtype='double', nvsize=8, Off_t='lo +ng long', lseeksize=8 alignbytes=8, prototype=define Linker and Libraries: ld='g++', ldflags ='-s -L"C:\strawberry\perl\lib\CORE" -L"C:\straw +berry\c\lib"' libpth=C:\strawberry\c\lib C:\strawberry\c\x86_64-w64-mingw32\lib +C:\strawberry\c\lib\gcc\x86_64-w64-mingw32\4.9.2 libs=-lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 +-ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -l +mpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32 perllibs=-lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdl +g32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_3 +2 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32 libc=, so=dll, useshrplib=true, libperl=libperl524.a gnulibc_version='' Dynamic Linking: dlsrc=dl_win32.xs, dlext=xs.dll, d_dlsymun=undef, ccdlflags=' ' cccdlflags=' ', lddlflags='-mdll -s -L"C:\strawberry\perl\lib\CORE +" -L"C:\strawberry\c\lib"' Characteristics of this binary (from libperl): Compile-time options: HAS_TIMES HAVE_INTERP_INTERN MULTIPLICITY PERLIO_LAYERS PERL_COPY_ON_WRITE PERL_DONT_CREATE_GVSV PERL_HASH_FUNC_ONE_AT_A_TIME_HARD PERL_IMPLICIT_CONTEXT PERL_IMPLICIT_SYS PERL_MALLOC_WRAP PERL_PRESERVE_IVUV USE_64_BIT +_INT USE_ITHREADS USE_LARGE_FILES USE_LOCALE USE_LOCALE_COLLATE USE_LOCALE_CTYPE USE_LOCALE_NUMERIC USE_LOCALE_TIME USE_PERLIO USE_PERL_ATOF Built under MSWin32 Compiled at Jan 16 2017 02:12:13 @INC: C:/Strawberry/perl/site/lib/MSWin32-x64-multi-thread C:/Strawberry/perl/site/lib C:/Strawberry/perl/vendor/lib C:/Strawberry/perl/lib .

      "It's not how hard you work, it's how much you get done."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11132940]
Approved by GrandFather
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found