AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Calling Perl from AHK

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
thomasl



Joined: 16 Jun 2005
Posts: 92

PostPosted: Fri Jul 22, 2005 11:27 am    Post subject: Calling Perl from AHK Reply with quote

UPDATE: There is now a newer version of this package at the topic Embedding Perl.

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 Smile ).

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.activestate.com/Products/ActivePerl/.
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10480

PostPosted: Fri Jul 22, 2005 1:12 pm    Post subject: Reply with quote

I read the Readme and the Script included with the zip file. It's a great-looking package. Thanks for sharing your work.

Now to learn some Perl... Smile
Back to top
View user's profile Send private message Send e-mail
thomasl



Joined: 16 Jun 2005
Posts: 92

PostPosted: Fri Jul 22, 2005 3:41 pm    Post subject: Reply with quote

Chris wrote:
Now to learn some Perl... Smile

To be honest, if you don't know a bit of Perl already it's probably not worthwhile.

Perl is not for the faint of heart and the syntax is even weirder than AHK's Smile . 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.
Back to top
View user's profile Send private message
savage



Joined: 02 Jul 2004
Posts: 206

PostPosted: Fri Jul 22, 2005 10:00 pm    Post subject: Reply with quote

Okay that's just badass. I'm starting to favor ruby over perl, but the ability to basically bind perl code to global hotkeys without the hassle of using temp files is just really cool. Maybe I'll have a look at the ruby interface and come up with a ahk interface for it.

AAAAH head spinning with possiblities Shocked
_________________
<enormous animated gif>
Back to top
View user's profile Send private message AIM Address
thomasl



Joined: 16 Jun 2005
Posts: 92

PostPosted: Sat Jul 23, 2005 2:37 pm    Post subject: Reply with quote

Quote:
I'm starting to favor ruby over perl

Why is that? What is better or different with Ruby?

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.
Back to top
View user's profile Send private message
Atomhrt



Joined: 02 Sep 2004
Posts: 128
Location: Sunnyvale

PostPosted: Sat Jul 23, 2005 2:57 pm    Post subject: Reply with quote

This is really nice! Thanks for doing this.

This does add quite a bit of power to ahk scripts. Being able to use grouping is a big plus.
Code:

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:
Code:

#EscapeChar ?
mycmd:="$results=`cmd /c dir c:\\temp`;"
p(mycmd)
results:=ps("results")               ; get results as a string
MsgBox results=%results%

_________________
I am he of whom he speaks!
Back to top
View user's profile Send private message
savage



Joined: 02 Jul 2004
Posts: 206

PostPosted: Sun Jul 24, 2005 7:39 pm    Post subject: Reply with quote

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). There's also a lot of advanced programming trickery that's easy in ruby and probably not even possible in many other languages. I still like perl and will probably continue to use it for things that are basically just shell scripts, but I've started using ruby for everything bigger than that. http://www.poignantguide.net/ruby/ - this is the weirdest programming book/tutorial I've ever read, but check out the chapter on dwemthy's array to see what I mean about advanced programming stuff.

I wrote a little ruby to document my main ahk script:

Code:

#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.
_________________
<enormous animated gif>
Back to top
View user's profile Send private message AIM Address
Guest






PostPosted: Sun Jul 24, 2005 11:39 pm    Post subject: Reply with quote

savage wrote:
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
Back to top
thomasl



Joined: 16 Jun 2005
Posts: 92

PostPosted: Mon Jul 25, 2005 8:56 am    Post subject: Reply with quote

savage wrote:
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.

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 Smile ).

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 Smile
Back to top
View user's profile Send private message
antonyb



Joined: 26 May 2004
Posts: 61

PostPosted: Wed Jul 27, 2005 10:46 am    Post subject: Reply with quote

Hey thomasl,

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:
Code:
!0::Gosub, TestPerl

TestPerl:
  p("use UDDI::Lite;")
  MsgBox, success!
  return

However 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
Back to top
View user's profile Send private message
thomasl



Joined: 16 Jun 2005
Posts: 92

PostPosted: Wed Jul 27, 2005 11:12 am    Post subject: Reply with quote

antonyb wrote:
However hitting Alt-0 just crashes AutoHotKey with no error message

Well, your snippet works fine here Smile I have 5.8.7 BTW.

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.
Back to top
View user's profile Send private message
antonyb



Joined: 26 May 2004
Posts: 61

PostPosted: Wed Jul 27, 2005 12:27 pm    Post subject: Reply with quote

Sure does - works fine now!

Excellent - now to start playing Smile

Thanks thomasl,

Ant.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group