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

Is it possible to open Notepad and output results to it instead of saving it to a .txt file and having to open the file manually? Even if it has to save first, can Perl open the file such that it opens on the screen?

Replies are listed 'Best First'.
Re: Is it possible to open notepad?
by blue_cowdawg (Monsignor) on Aug 24, 2011 at 20:23 UTC
        Is it possible to open Notepad and output results to it instead of saving it to a .txt file and having to open the file manually?

    Let me interpolate what you are trying to do by what you have just said. My thinking is you could do this one of two ways: First would be some sort of funk-a-delic OLE set of calls which I am not privy to since I don't do windows.

    The other way I'd approach the problem is as follows:

    # hand waving here... # # No, I wouldn't really write a Perl script without use'ing strict. # :-D open FOUT,"> myfile.txt" or die "myfile.txt:$!"; # do writes here close FOUT; system("notepad.exe myfile.txt"); # untested code. Please do not use this for anything # that could be potentially life threatening # lest the fleas of a thousand camels infest your # armpits and drive you buggy.

    What I'm driving at is at some point you should actually write the file and then use notepad to view it. What I'm not sure about, and the windows crowd can help here, is what happens to the notepad.exe process when the script exits.

    Hope this is of some help...


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Is it possible to open notepad?
by kennethk (Abbot) on Aug 24, 2011 at 20:14 UTC
    Assuming you are working on Windows and talking about the Notepad program commonly distributed with it, you can invoke the program via the OS using system or exec, depending on whether you want to pause script execution or not.

    perl -e "exec 'notepad file.txt'"

      The difference between system() and exec() is NOT whether it pauses the script or not! system() will pause the script (unless used in one of a few special ways), exec() will start the notepad AND NEVER RETURN!

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

Re: Is it possible to open notepad?
by BrowserUk (Patriarch) on Aug 24, 2011 at 21:40 UTC

    Try this:

    #! perl -slw use strict; use Win32::GuiTest qw[ SendKeys ]; use Win32::Clipboard; my $text = "The quick brown fox jumps over the lazy dog\r\n" x 10; my $clip = Win32::Clipboard(); $clip->Set( $text ); system 'start notepad.exe'; sleep 1; SendKeys( '^v' );

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Is it possible to open notepad?
by Jim (Curate) on Aug 24, 2011 at 21:44 UTC

    Frankly, I'd discourage you from doing whatever it is you're trying to do with Perl and Microsoft Notepad. Control an inferior text editor from a superior text processing scripting language? Sounds kind of klunky.

    Tell us what your real problem is and I'll bet someone can suggest a better approach to solving it than by using Perl to control Microsoft Notepad.

    (I acknowledge I could be profoundly wrong and that the Perl/Notepad duo might be the perfect software pairing for your problem domain. But you've piqued my curiousity and I'd love to learn more—and to help if I can.)

      Control an inferior text editor from a superior text processing scripting language?

      I use this technique occasionally in CLI programs which require entering a fair amount of text: I generate a YAML or JSON file which prompts for the desired input, write it to a temporary file, then launch $ENV{EDITOR} || '/usr/bin/vi' to edit the file.

      It works well for programs where I can assume users have the appropriate technical skills.

        Fair enough. I can see a use case for popping open a text editor in the middle of CLI program.

        It works well for programs where I can assume users have the appropriate technical skills.

        In the Windows world, one can assume that users with "appropriate technical skills" will refuse to use Microsoft Notepad. ;-)

Re: Is it possible to open notepad?
by davies (Monsignor) on Aug 24, 2011 at 21:36 UTC

    sendkeys is notorious even within a single application, so combining it with Perl may well be fraught, but this may get you started. If MessWord is on the machine, there is plenty of information on automating that and you won't need the dreaded sendkeys.

    Regards,

    John Davies

    Update: there's no macro facility within Notepad, so I can't imagine how Win32::OLE would be able to automate it. Googling gets me no suggestions that don't involve sendkeys, so your choice may be sendkeys or using a more powerful application.

Re: Is it possible to open notepad?
by Logicus (Initiate) on Aug 25, 2011 at 01:28 UTC
    perl -e "system('gedit')"; Works on ubuntu.