Pass hook values to subroutines

All resolved question will be found here. It is recommended that you browse / search through this section first and see if your question has been answered before

Moderators: Moderators, Documentation Writers

Message
Author
necrotek
Noob
Noob
Posts: 2
Joined: 02 Feb 2015, 05:41
Noob?: No

Pass hook values to subroutines

#1 Post by necrotek »

Hello.
I'm sorry if this question was already asked but quick check of topics didn't show me similar issues (unless I overlooked it).
I'm trying to write an automacro which reacts on some events using hooks. For some unclear reason, I can't find a way to pass assigned values from hook to subroutine, openkore returns an error no matter what I try.

Some example:

Code: Select all

automacro PM {
	hook packet_privMsg
	save Msg
	call {
		$msg = $.hooksave0
		$len = len($msg)
		log He said $msg and it has $len characters
	}
}

sub len {
	my ($str) = @_;
	return length($str)
}
When I try to send a private message, openkore returns:
(From: Tester) : Test private message
[macro] automacro PM triggered.
[macro] PM.call error: error in 1: len($msg) failed
But if you try to replace $.hooksave0 in $msg with "Test private message", it will work (so, there is no problem with the code itself).
I'm completely lost at this point, I don't know how to make it possible to pass events values to subroutines.

necrotek
Noob
Noob
Posts: 2
Joined: 02 Feb 2015, 05:41
Noob?: No

Re: Pass hook values to subroutines

#2 Post by necrotek »

Oh, I think I managed to solve it at last (but need to test it to be sure). In order to pass hook value to subroutine, you need to pass it enclosed in double quotes.

Code: Select all

      $msg = "$.hooksave0"
      $len = len($msg)
Though I still didn't find a way to pass arrays from hooks.

ezza
Developers
Developers
Posts: 109
Joined: 04 Apr 2008, 09:50

Re: Pass hook values to subroutines

#3 Post by ezza »

try..

$len = len("$msg")


i'm not sure if this is okay coz i forgot everything about ro n all this stuff..sry guys

Locked