in reply to scripting i3 with Perl

The error message is pretty descriptive. You are calling ->recv from within a handler. Don't do that.

The following should work:

sub handle { my($data)=@_; if($data->{change} eq "empty") { print "empty workspace!!\n"; my $switched; $switched = $i3->command("workspace 1")->cb(sub { print "Switched back to workspace 1\n"; undef $switched; }); } }

Using AnyEvent, you should basically never call ->recv and only use callbacks except in the outermost program.

Replies are listed 'Best First'.
Re^2: scripting i3 with Perl
by morgon (Priest) on Feb 02, 2019 at 07:38 UTC
    Thanks for the reply.

    Your attempt gets rid of the error message and prints "Switched back...", however the switch itself does not happen - the command does not seem to be executed...

      Maybe there is an error? The AnyEvent::i3 documentation mentions that ->command returns a result:

      use Data::Dumper; sub handle { my($data)=@_; if($data->{change} eq "empty") { print "empty workspace!!\n"; my $switched; $switched = $i3->command("workspace 1")->cb(sub { my( $reply ) = @_; print "Switched back to workspace 1\n"; undef $switched; use Data::Dumper; print Dumper $reply; }); } }
        This is what I get:
        empty workspace!! Switched back to workspace 1 $VAR1 = bless( { '_ae_sent' => [ [ { 'success' => bless( do{\(my $o = +1)}, 'JSON::PP::Boolean' ) } ] ] }, 'AnyEvent::CondVar' );
        However no switching occurs. No clue what is going on there...