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

Changing the title of the command window on which the perl exe is run

by Anonymous Monk
on Apr 30, 2011 at 03:10 UTC ( [id://902114]=perlquestion: print w/replies, xml ) Need Help??

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

Am trying to change the title of the cmd window on which my perl exe is run,title just changes and in blink of a second it goes back,any idea how to permanently change the title of the window on which the exe is run?

system ("title Myperlscript.exe");
  • Comment on Changing the title of the command window on which the perl exe is run

Replies are listed 'Best First'.
Re: Changing the title of the command window on which the perl exe is run
by ww (Archbishop) on Apr 30, 2011 at 03:51 UTC
    We can infer (it would have been nice to tell us) that you're on a Windows box. And we can infer either that you know very little about Perl and very little about using your CLI -- or that you are sufficiently expert to convert a script to an executable ... in which case you may stop reading here, since the rest of this reply won't address your problems at that kind of expert level.

    But expertise seems unlikely.

    So let's look further: the line of code you show is neither properly formatted for this site (use <code>...</code> tags around all code and data), nor terribly informative about what your full script may look like. (Knowing that might be helpful.)

    However, guessing that you're doing something like:
    C:\>perl -e "system("title Myperlscript.exe");"
    Unfortunately, that produces this message:
    syntax error at -e line 1, at EOF

    So, you might wish to consider fixing your quotes to keep the windows shell happy:
    >perl -e "system(\"title Myperlscript.exe\");" which would cause something to flash in the title bar... very briefly.

    However, that's not an answer to your (presumed) question (and it's wrong, anyway!), so moving away from guessing, the next bit of code would do what you ask (not quite so briefly):
    perl -e "system(\"title foo\");sleep 5;"
    which would change the title bar caption to "foo" ... for 5 seconds, which is a little short of "permanently" (and one hopes that is merely a careless phrase, since changing Mr. Gates' handiwork - permanently - is beyond the scope of this forum).

    Another approach would be to use this:
    perl -e "`title \"my script\"`;print 'enter: ';$x=<>;" which would print enter: on your screen... and (ta da!) change the title bar caption from "Command Prompt" to "my script."

    Woot!

    Regrettably, as soon as you enter something the caption changes back to "Command Prompt."

    However, if you're really interested in learning something from all this, see exec or (at your command prompt) try perldoc -q backticks and perldoc -f exec for relevant discussions.

    And, oh yes, please read On asking for help, How do I post a question effectively?, I know what I mean. Why don't you? and Markup in the Monastery to learn what's expected of a SOPW.

    Update: fixed typo in link to exec docs; thanks, AnomalousMonk!

Re: Changing the title of the command window on which the perl exe is run
by wind (Priest) on Apr 30, 2011 at 03:24 UTC
    Set the command prompt title before you start your perl script.
    title "foobar" perl yourscript.pl

      No,I want to do it via perlscript.Whenever my exe is run,that command window should have a title

Re: Changing the title of the command window on which the perl exe is run
by furry_marmot (Pilgrim) on Apr 30, 2011 at 20:29 UTC

    Wow. The snarkiness is flying today! And yet no one has the answer! Kuh-ching!

    First, an explanation of why your approach doesn't work. When you run system from Perl, you are running a new shell, which is to say a new copy of CMD.exe, which has its own environment. You type title <something> and it works, but as soon as the new shell exits, you're back in the parent shell, which never had its title changed. You have to do it from inside, via the Win32 API.

    use Win32::Console; my $CONSOLE=Win32::Console->new; $CONSOLE->Title('This is a new title - Huzzah!');

    That will change the title for the duration of your script. When the script ends, the title will revert to what it was before you ran the script. You can change the title as much as you want.

    Cheers!

    --marmot
      using system 'title', 'the title;' will work form within your program same as using Win32::Console, for the duration of the program
Re: Changing the title of the command window on which the perl exe is run
by Anonymous Monk on Apr 30, 2011 at 12:32 UTC
    A child cannot affect the parent.

    Start a cmd.exe then run this

    perl -e " exec qq[cmd.exe /k \x22title \x22quoted title\x22\x22] "
    then type "exit".

    Then type "exit" again.

    First "exit" exits the cmd.exe with the title "quoted title" started from perl with exec, the second "exit" exists the original cmd.exe.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-25 14:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found