Calling Perl from AHK
Embedding Perl into AHK proved simpler than I thought (or perhaps this just shows what a thoroughly good job Larry Wall and his gang are doing ).
Download this zip file, unpack it, copy the two DLLs to a location where AHK can find them and start the example script.
More details in the README which also doubles as the source code for the C module. I can supply a makefile as well, in the unlikely case anyone wants to play around with it.
Perl itself can be found at http://www.activesta...cts/ActivePerl/.
Now to learn some Perl...
To be honest, if you don't know a bit of Perl already it's probably not worthwhile.Now to learn some Perl...
Perl is not for the faint of heart and the syntax is even weirder than AHK's . OTOH one can do amazing things with just a few lines of Perl, things that would require 100s and 100s of lines of C code (literally).
If I knew how to marry the hotkeys and the GUI feature of AHK with Perl into one nice package... Well, perhaps one of these days.
AAAAH head spinning with possiblities :shock:
Why is that? What is better or different with Ruby?I'm starting to favor ruby over perl
I've never looked at it so if you can give me the lowdown in a few words... why you prefer it: that would be interesting.
This does add quite a bit of power to ahk scripts. Being able to use grouping is a big plus.
Example: p("$d=""Let's swap this and that"";") p("$d=~s/(this) and (that)/$2 and $1/") d:=ps("d") ; get d as a string MsgBox d=%d% Or this example: p("$e=""We have an IP address of 149.1.205.78. Let's grab the octets."";") p("($o1,$o2,$o3,$o4) = ($1,$2,$3,$4) if $e =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/") o1:=pi("o1") ; get octet as an int o2:=pi("o2") o3:=pi("o3") o4:=pi("o4") MsgBox Octets: o1=%o1% o2=%o2% o3=%o3% o4=%o4%
And here's another cool thing. Using backticks to get results back from a command:
#EscapeChar ? mycmd:="$results=`cmd /c dir c:\\temp`;" p(mycmd) results:=ps("results") ; get results as a string MsgBox results=%results%
I wrote a little ruby to document my main ahk script:
#produces brief documentation from appropriately commented ahk files #;; after hotkey = hotkey description #;_____ start section #;_ end section #;N a note #switch short keynames to descriptive ones def keysToDesc keys keys.gsub(/[!#^+]/) do case $& when '!' then 'Alt ' when '#' then 'Win ' when '^' then 'Ctrl ' when '+' then 'Shift ' end end end if ARGV.empty? puts "Usage: doc.rb [file1] [file2] ...." else ARGV.each do |file| indent = [] IO.foreach(File.expand_path(file)) do |line| case line when /^(.+)::.*;;(.+)$/ #hotkey description puts "" if indent.empty? #we're at outer level, put a line between entries puts "#{indent.to_s}#{keysToDesc $1} = #$2" when /^;_____([^_]+)/ #subsection start, tab in puts "\n#{indent.to_s}#{$1.sub(/(\{\{\{)|(\}\}\})/,"")}" #strip off vim fold markers indent.push("\t") when /.*;_([^_]*)/ #subsection end, tab out indent.pop when /.*;N(.+)$/ #note puts "Note: #{$1}" end end end end
I commented the regex because they're unreadable in any language but the rest of the code is really clear I think. If you're wondering how the keystodesc function works, just about everything is an expression with a return value, and functions return the value of the last expression by default.
Apparently ruby has an ARGF.each structure that's basically the same as while(<>) in perl, but I haven't gotten around to changing the script to use it.
EDIT: The indentation looks weird, but it looked fine when I pasted it. Dunno what's wrong.
Hmm, I'm not so good at explaining why I like things, but I'll give it a shot. Here's the things I like least about perl - obscure syntax (but that has it's upside in extremely concise code), scary object oriented extensions, and it seems really difficult to write a well designed and easy to read program in it. Ruby has simple, clean syntax (not quite python, but ruby is a more complex language), it's object oriented to the core - a class is an object, the number 1 is an object, classes and even instances are open - methods can be added to them at runtime, on top of that it has lots of syntax sugar that lets you write code as small as perl (though this tends to go against the "ruby way") but the language scales up extremely well (have you seen ruby on rails).
You may want to take a look at this -> http://c2.com/cgi/wiki?RubyVsPerl
The syntax can be weird, I agree. I came to Perl pretty late (ca 98 or so) so I had almost two decades of Pascal and C under my belt -- and that's why my Perl programs look almost like C. They are pretty readable I think (then again, all programmers think of their own code as readable -- until they have to understand it years later and there is not one line of comments in sight ).obscure syntax (but that has it's upside in extremely concise code), scary object oriented extensions, and it seems really difficult to write a well designed and easy to read program in it.
Yeah, if one does Perl in the way Larry Wall does it the code is concise and unreadable... but hey, nobody says we have to do things in Wall style.
What I like most about Perl is that it is syntactically close to C but infinitely more powerful; and that there is a module out there for almost everything you might care to do. I once had a simple web server hacked together in half an hour or so -- and I mostly had to read docs and write a few lines of glue code between modules.
As to regexes.. they are an eyesore in any language
Great addition, thanks! Really awesome.
However, having problems using modules; I've installed the latest ActiveState Perl, copied over the latest perl58.dll to the AutoHotKey directory, and am just trying the following:
!0::Gosub, TestPerl TestPerl: p("use UDDI::Lite;") MsgBox, success! returnHowever hitting Alt-0 just crashes AutoHotKey with no error message. UDDI::Lite is definately installed; I'm assuming its a path/config issue... any ideas?
Thanks
Ant
Well, your snippet works fine here I have 5.8.7 BTW.However hitting Alt-0 just crashes AutoHotKey with no error message
Anyway, this happened in my tests if a module that is dynamically loaded can't be found. There is not much I can do in this case as Perl itself panics with an error message (which remains invisible as there is no console window to display it on) and then dies an unceremonious death, taking AHK with it.
Double-check all your paths and also delete the Perl DLL in the AutoHotkey directory: quite possibly this is the problem. I included the Perl58.dll only for those poor souls who have no Perl on their machines. If you have a full Perl installation and the DLL can be found at all in your path there's no need to have a copy of the Perl DLL lying around in the AutoHotkey directory.
Hope that helps.
Excellent - now to start playing
Thanks thomasl,
Ant.