dude77 has asked for the wisdom of the Perl Monks concerning the following question:

HI,

I am struggleing to add a perl variable into a command that sould run in bash.

perl code:

listen
{
        foreach my $event ( @_ )
        {
                #print( Data::Dumper( $_ )."\n" );
                if ( $event->house_code() eq ZM_X10_HOUSE_CODE )
                {
                        my $unit_code = $event->unit_code();
                        my $device = $device_hash{$unit_code};
                        if ( !$device )
                        {
                                $device = $device_hash{$unit_code} = { appliance=>$x10->Appliance( unit_code=>$unit_code ), status=>'unknown' };
                        }
                        next if ( $event->func() !~ /(?:ON|OFF)/ );
                        $device->{status} = $event->func();
                        my $task_list = $device->{$event->func()."_list"};
                        if ( $task_list )
                        {
                                foreach my $task ( @$task_list )
                                {
                                        processTask( $task );
                                }
                        }
                }
                Info( "Got event - ".$event->as_string()."\n" );
                Info( " got - ".$event->house_code().$event->unit_code().$event->func()."\n");
                my $x10_device = .$event->house_code().$event->unit_code().$event->func().;
                system("/bin/echo $x10_device");
}


code not working:
                my $x10_device = .$event->house_code().$event->unit_code().$event->func().;
                system("/bin/echo $x10_device");

$x10_device has the value of A1ON

thus on bash should look like

dvr@dvr:~$ echo A1ON
A1ON


currently in bash it outputs :

.X10::Event=HASH(0x96a9f58)->house_code().X10::Event=HASH(0x96a9f58)->unit_code().X10::Event=HASH(0x96a9f58)->func().

Thanks

Dude

Replies are listed 'Best First'.
Re: backticks with system()
by almut (Canon) on Dec 19, 2008 at 02:35 UTC
    my $x10_device = .$event->house_code().$event->unit_code().$event->func().;

    I don't think this is your real code. As it is, it produces nothing but a syntax error.  Do you maybe have double quotes around the right hand side of the assignment, i.e.

    ".$event->house_code().$event->unit_code().$event->func()."

    That would produce the string you're seeing, because the $event object would be interpolated/stringified instead of having its methods called...

    This has nothing to do with system() or bash.  Just get rid of the superfluous dots:

    my $x10_device = $event->house_code().$event->unit_code().$event->func +();
Re: backticks with system()
by Bloodnok (Vicar) on Dec 19, 2008 at 13:34 UTC
    Use (pun definitely intended) strictures e.g. use warnings; use strict; !! That would point out that there's a missing right brace '}' completing the foreach block.

    Is there a specific reason for your use of system with /bin/echo ... instead of print ??

    A user level that continues to overstate my experience :-))