Perl question?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Perl question?

Neil Cherry
I have need to have an undefined variable (don't ask why). What I
want is to do something like this:

if(defined $Book_Lamp) {
   if(state_now $MacroFOff) {
     print_log(" *** Book test A 15 On\n");
     $Book_Lamp->set(ON);
   }

   if(state_now $MacroFOff) {
     print_log(" *** Book test A 15 On\n");
     $Book_Lamp->set(OFF);
   }
}

Otherwise it skips over it and MH just ignores it. Any ideas?
MH doesn't like the above and won't run with the code this way.

--
Linux Home Automation         Neil Cherry       [hidden email]
http://www.linuxha.com/                         Main site
http://linuxha.blogspot.com/                    My HA Blog
http://home.comcast.net/~ncherry/               Backup site


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
________________________________________________________
To unsubscribe from this list, go to: http://sourceforge.net/mail/?group_id=1365

Reply | Threaded
Open this post in threaded view
|

Re: Perl question?

Bruce Winter
Neil Cherry wrote:

> I have need to have an undefined variable (don't ask why). What I
> want is to do something like this:
>
> if(defined $Book_Lamp) {
>   if(state_now $MacroFOff) {
>     print_log(" *** Book test A 15 On\n");
>     $Book_Lamp->set(ON);
>   }
>
>   if(state_now $MacroFOff) {
>     print_log(" *** Book test A 15 On\n");
>     $Book_Lamp->set(OFF);
>   }
> }
>
> Otherwise it skips over it and MH just ignores it. Any ideas?
> MH doesn't like the above and won't run with the code this way.

There are 2 I can think of to test if a global var exists or not.  As
you found out, simply testing for a variable that does not exist will
give you a compile error.   So one option would be to put that test in
an eval, so errors don't cause mh to choke.

If you are testing for a global variable, like an object like $Book_Lamp
, you can leverage the %main:: array that mh creates for all global vars
(but not local vars, created with my).

So try this:

  if ($main::{Book_Lamp}) {
    ...

This is what we use to get a list of all global vars in the web page
(see vars_global in lib/http_server.pl

Bruce



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
________________________________________________________
To unsubscribe from this list, go to: http://sourceforge.net/mail/?group_id=1365