Keep working with perl script

I have a perl script that subscribes to a message queue using AnyEvent :: MQTT.

At that moment, when all I want to do is print it when he receives the message. I am completely unfamiliar with perl, so I use the demo code in which it is included, including the bit that publishes something on STDIN as a message - this works fine, and I see all the messages received.

This code is as follows

#!/usr/bin/perl
use strict;
use warnings;

use AnyEvent::MQTT;
my $mqtt = AnyEvent::MQTT->new;
my $cv = $mqtt->subscribe(topic => '/AlarmMode',
                           callback => sub {
                               my ($topic, $message) = @_;
                                       print $topic, ' ', $message, "\n"
                                   });

my $qos = $cv->recv; # subscribed, negotiated QoS == $qos



  # publish line-by-line from file handle
      $cv =  $mqtt->publish(handle => \*STDIN,
                            topic => '/topic');

The problem is that if I delete everything after the comment publish line-by-line from file handle, then my application will exit as soon as it starts.

I tried turning on the while loop, which sleeps for 5 seconds, but this does not work (the application just looks like it was hanging).

, - , , , : -)

+4
1

-, AE (, sleep), AE .

, AE,

my $done_cv = AE::cv;
$done_cv->recv;

$done_cv->send; . , 5 ,

my $sleep_cv = AE::cv;
my $w = AE::timer(5, 0, $sleep_cv);  # Short for AE::timer(5, 0, sub { $sleep_cv->send });
$sleep_cv->recv;
+6

Source: https://habr.com/ru/post/1624675/


All Articles