Friday, January 23, 2009

Launch your Perl script from Notepad++

Today I am going to explain how you can be using Notepad++ to run Perl scripts.
It can be convenient to launch your program directly from the editor's environment to quickly check that it compiles.
Let's try and set up Notepad++ to launch the script from Wednesday's post.

In the Plugins menu, you will find the NppExec plugin.
Select Plugins>NppExec>Execute...
You can also use the convenient F6 shortcut.

Notepad++ interface to the execute command
Once the Execute... window is open, type in the following command:
perl "$(FULL_CURRENT_PATH)"$(FULL_CURRENT_PATH) is an internal Notepad++ variable that contains the full path of the file currently open in the editor.
There is a list of 10 such global variables here. We might come back to them later.
So, hitting F6 for our example is equivalent to typing the following line in a DOS shell:
perl C:\Perl\usr\skeleton2.pl

You can save this command line and give it a meaningful name ('perl' for example).
Of course, you can enter any command (or sequence of commands) that you would type at a DOS prompt:
perl -v
dir
...


The output result is visible on the Console window.Perl script execution on Notepad++ output console
You can get rid of the "Process..." lines by selecting Plugins>NppExec>No Internal Message
Also, if you want to repeat the previous command, you can just type Ctrl+F6.

If you know more Notepad++ tips, I'd love to hear from you!

A few words about the new skeleton:
The -w added at the end of the shebang line is the equivalent of use warnings;

$PROG_NAME, $VERSION and $PROG_DATE are called scalars. They are composed of a $ followed by a variable name. Scalars are used to represent a single element (in this case, they are all strings, because initialised as characters between double quotes).
The my function has to do with the scope of the variables. It is optional but I was told it is good practice to use in conjonction with the use strict command.
We'll see if we can figure out why later. If you have a clue, please drop a comment.

Finally, the last line makes use of the variables declared at the top of the script:
print "This is $PROG_NAME $VERSION from $PROG_DATE.\n";
Strings between double quotes (") are interpolated. This means that when the Perl interpreter sees $PROG_NAME between quotes, it will replace it by its current value.
If you replace the " by a single quote ('), then the output will become:
This is $PROG_NAME $VERSION from $PROG_DATE.\n

Come on, try it! All it takes are 2 key strokes and Ctrl+F6!
Finally, "\n" is understood as the next line character.

Today's French expression would not get approval from Larry Wall:
"La paresse est source de tous les vices": An idle person is the devil's cushion.
"Paresse" is the French word for lazyness. This is a quality for a developper according to Larry, because it pushes him/her to create scripts in order to automate his work and avoid repetitive tasks.

New posts:
  • Our first Perl program - Part II: parsing the Perl command line
  • More about CPAN
  • Our first Perl program - Part III: Add a GUI interface
  • How to install Google Analytics on your Blogger blog
  • First version of FileInfo script
  • Perl help resources

22 comments :

  1. Do you know how to pop up a wxPerl GUI program?

    ReplyDelete
  2. Using use strict and my has the immediate benefit of easily spotting spelling mistakes, because every use of an uninitialised value now results in a compile error. If strict is not used Perl assumes you mean two different variables with $variable and $varaible and quietly creates the second one.

    It also limits a variable's scope to the current block as seen here

    As is often said: You should always use strict.

    PS: Love your blog. I am just returning to Perl after some years of using other languages (mainly C#) and it is interesting to watch your progress here.

    ReplyDelete
  3. Thanks for your comment kreetrapper. This is exactly what I had in mind when I started writing this blog:
    - to learn from the readership's advice as much as from the Camel book :)

    ReplyDelete
  4. John,

    you can check out http://damienlearnsperl.blogspot.com/2009/02/how-to-run-wxperl-scripts-from-notepad.html

    ReplyDelete
  5. Hey Damien,
    Ha. I google Notepad++ & Perl and look who pops up. I usually use EPIC for my major Perl projects but need something to perform quick edits without all the extras. I am unfamiliar with Notepad++ so this is helpful. thanks.

    ReplyDelete
  6. Michael,
    thanks for telling me about EPIC. It looks really feature-complete.

    ReplyDelete
  7. How do you teach it to use the directory of the script as the current working directory? It seems to use the notepad++ directory as the current working directory.

    ReplyDelete
  8. Lee,
    you can do so by preceding the wperl line of the "Execute.." window (type F6) with this line:

    cd "$(CURRENT_DIRECTORY)"

    This will make the script's location become the cwd. Then run the perl interpreter:

    wperl "$(FULL_CURRENT_PATH)"

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Hello Damien ,
    Thanks for such a wonderful article . I am new to Perl and was trying to execute the following program using the NPPexec plugin .
    #! /usr/bin/perl -w

    print "What is your shoe size? ";
    $size = ;
    chomp $size;
    print "your shoe size is $size";

    I could execute the above program using dos window ,but could not do the same using the steps mentioned above . If I try to close the blank console window(ctrl + F6), I get the warning :console process is still running . Any pointers to resolve would be helpful . Thanks in advance .

    ReplyDelete
  11. Hi Somil,
    when you hit CTRL+F6, your script is running. Enter a value and it will terminate properly.
    However, you will notice that you are asked for an input value before the first print message.
    You can force a flush of the standard output by setting the $| variable to a value different from zero(see $OUTPUT_AUTOFLUSH in perldoc).
    The following version of your program will execute correctly with NPPExec:

    #! /usr/bin/perl -w

    print "What is your shoe size? \n";
    $| = 1; #Flush standard output buffer
    $size = <>;
    chomp $size;
    print "your shoe size is $size";

    ReplyDelete
  12. Damien,

    My skeleton.pl file is saved in C:\Users\thescoop\perl

    I have added these lines to the execute window:

    cd "$(CURRENT_DIRECTORY)"
    perl "$ (FULL_CURRENT_PATH)"

    Yet, the output I get is:

    CD: C:\Users\thescoop\perl
    Current directory: C:\Users\thescoop\perl
    perl "$ (FULL_CURRENT_PATH)"
    Process started >>>
    Can't open perl script "$ (FULL_CURRENT_PATH)": No such file or directory
    <<< Process finished.
    ================ READY ================

    Can you advise me on this problem?
    Many thanks for your blog. Yours seems to be the only one I can find that takes complete novices through the initial setup stages.
    Regards, thescoop.

    ReplyDelete
  13. DAmien,
    My problem is resolved.
    I put the following single line into the execute window:

    perl $(FULL_CURRENT_PATH)

    Regards, thescoop.

    ReplyDelete
  14. Sorry I am New User. (and Eng is so bad)
    I have Problem run perl
    I Use Win32:GUI
    I make new window
    but cannot show window
    show only Console window
    How to see new window I build ?

    ReplyDelete
  15. I am unable to find "Plugins>NppExec>Execute..." . Please let know me know if I need to do any setting. I have installed Perl and unable to run the perl program

    ReplyDelete
  16. Install it using the Plugin Manager.

    It's under Plugins -> Plugin Manager -> Show Plugin Manager

    ReplyDelete
  17. Thanks, John (Jericho, VT)

    ReplyDelete
  18. i am done thankyou dude

    ReplyDelete
  19. Thank you SOOOOO much for this. I wasted many hours trying to get my script to run. My solution was similar to yours except I had to put the FULL PATH to Perl in the NPPExec pop-up window.

    Next I could not get it to do I/O, as another user found ... and AGAIN you came to the rescue with the flush buffer tip.

    I can't thank you enough. Now I'm off to read the rest of your site as you obviously will save me MUCH more time.

    ReplyDelete
  20. holy ..... so nice !
    I looked for an Perl IDE and found THIS !
    Perl in my favorite Editor !
    Thanks !

    ReplyDelete
  21. excellent site, cheers big ears

    ReplyDelete
  22. Thanks. Great tip. Worked for me.
    ('but I didn't had the nppexec in the plugin manager at the beggining, so I reinstall notepad++ and it worked. Thx

    ReplyDelete