[mh] Troubles with Perl hashes passed to sub
Stephen Switzer <[email protected]>
| Newsgroups | gmane.comp.misc.misterhouse.user |
|---|---|
| Message-ID | <CALSXUc2g_V4Z-ZXSEfdjNFQkpyf=9WK68q3X3eiwA9fKgh-84g@mail.gmail.com> |
Hi everyone! Although I'm trying to modify code in my MH code directory
unsuccessfully, this could apply anywhere. I've googled and tried to come
up with ways to get this done, but it's time that I seek help.
My implementation includes sending messages to cell phones of my family and
I with AutoRemote, which ties in to Tasker. My Tasker recipe then takes the
data and creates a custom reminder in the notifications of my phone (or my
wife's, etc). Here's a call:
$adevices->notify_send('sswitzer', 1,
'Test Reminder',
'This is a reminder for you to do something',
(
'I did' => 'notify:idid',
'Remind Me Later' => 'notify:remind_me_later',
'Open URL' => 'url:http://maps.google.com/maps?q=test',
),
#('ttl'=>300),
);
As you can see, I'm sending a hash as parameter 5, and parameter 6 is a
commented out hash declaration. The first hash is used to build a "message'
variable in the URL and the second would ideally add an additional URL
parameter for the CURL call in another sub. When I tried to use this, the
receiving sub had everything in the %actions hash:
sub notify_send {
my ($self, $userid, $deviceid, $title, $text, %actions, %urlvars) = @_;
Then it dawned on me that Perl hashes tend to take all the remaining
parameters and slurp them up, so I couldn't have them separated. I tried to
send them by reference, just to see if that would help, but no.
So, I tried to combine the parameters into 1, making a hash of hashes, then
pass it along, like this:
%thehash = (
'urlvars' => {
'ttl' => '300'
},
'actions' => {
'I did' => 'notify:idid'
}
);
The result seemed to be OK, EXCEPT when I used Dumper on the hash from
inside the sub... I got this sort of thing:
%thehash = (
'300' => 'actions',
'I did' => 'notify:idid',
'urlvars' => 'ttl'
);
The hash gets all rearranged. So, I build it more manually:
my %combined;
$combined{'actions'}{'I did'} = 'notify:idid';
$combined{'actions'}{'Remind Me Later'} = 'notify:remind_me_later';
$combined{'actions'}{'Open URL'} = 'url:http://maps.google.com/maps?q=test';
$combined{'urlvars'}{'ttl'} = '300';
...and get:
%actions = (
'HASH(0x18b5158)' => undef,
'HASH(0x18b5080)' => 'urlvars'
);
While I know that's a reference to the hash memory location, this shows
it's being built backward once passed to a sub!
I went from a simple task of trying to pass another parameter to not even
ae to build a hash properly. My code is regressing to the point that even
the simple things that I thought I had a handle on are not working.
Frustrated...
So, I thought about using JSON to pass the object, but at this point I feel
I'm crawling too far down the rabbit hole trying to find it and the rabbit
has already exited at the other end.
Anyway, what ideas do others have? This is the file I created to test this
scenario outside of MH:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper::Simple;
#use JSON qw(encode_json decode_json);
sub notify_send {
my ($self, $userid, $deviceid, $title, $text, %actions) = @_;
print("(notify_send) actions: ".Dumper(%actions));
#print("(notify_send) urlvars: ".Dumper(%urlvars));
}
my %combined;
$combined{'actions'}{'I did'} = 'notify:idid';
$combined{'actions'}{'Remind Me Later'} = 'notify:remind_me_later';
$combined{'actions'}{'Open URL'} = 'url:http://maps.google.com/maps?q=test';
$combined{'urlvars'}{'ttl'} = '300';
my %thehash = (
'urlvars' => (
'ttl' => '300'
),
'actions' => (
'I did' => 'notify:idid'
)
);
print("Before sending: ".Dumper(%combined));
notify_send('sswitzer', 1,
'Test Reminder',
'This is a reminder for you to do something',
%combined,
);
How should I approach this for best results?
--
Best regards,
Steve Switzer
---
Get world-class business I.T. services and a phone system with awesome
features that won't challenge your budget!http://www.sbsROC.com
________________________________________________________
To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users