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.
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.
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
Do you know how to pop up a wxPerl GUI program?
ReplyDeleteUsing 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.
ReplyDeleteIt 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.
Thanks for your comment kreetrapper. This is exactly what I had in mind when I started writing this blog:
ReplyDelete- to learn from the readership's advice as much as from the Camel book :)
John,
ReplyDeleteyou can check out http://damienlearnsperl.blogspot.com/2009/02/how-to-run-wxperl-scripts-from-notepad.html
Hey Damien,
ReplyDeleteHa. 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.
Michael,
ReplyDeletethanks for telling me about EPIC. It looks really feature-complete.
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.
ReplyDeleteLee,
ReplyDeleteyou 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)"
This comment has been removed by the author.
ReplyDeleteHello Damien ,
ReplyDeleteThanks 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 .
Hi Somil,
ReplyDeletewhen 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";
Damien,
ReplyDeleteMy 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.
DAmien,
ReplyDeleteMy problem is resolved.
I put the following single line into the execute window:
perl $(FULL_CURRENT_PATH)
Regards, thescoop.
Sorry I am New User. (and Eng is so bad)
ReplyDeleteI 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 ?
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
ReplyDeleteInstall it using the Plugin Manager.
ReplyDeleteIt's under Plugins -> Plugin Manager -> Show Plugin Manager
Thanks, John (Jericho, VT)
ReplyDeletei am done thankyou dude
ReplyDeleteThank 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.
ReplyDeleteNext 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.
holy ..... so nice !
ReplyDeleteI looked for an Perl IDE and found THIS !
Perl in my favorite Editor !
Thanks !
excellent site, cheers big ears
ReplyDeleteThanks. Great tip. Worked for me.
ReplyDelete('but I didn't had the nppexec in the plugin manager at the beggining, so I reinstall notepad++ and it worked. Thx