Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: AnyEvent weirdness w/ open() and/or ``'s

by james2vegas (Chaplain)
on Nov 02, 2013 at 17:06 UTC ( #1060961=note: print w/replies, xml ) Need Help??


in reply to AnyEvent weirdness w/ open() and/or ``'s

Why do you need to read your data synchronously? Why not read lines from your command asynchronously, and create the messages in a callback? There are many AnyEvent modules to make running and getting output back from external commands easy, but there is also AnyEvent::Util::fork_call (which serialises the whole output of your command before calling the callback).

Alternatively, you could probably use an IO watcher like this:
open my $fh, '-|', 'some command'; my $w; $w = AnyEvent->io( fh => $fh, poll => 'r', cb => sub { chomp(my $user = $fh->getline); AnyEvent::XMPP::IM::Message->new( body => construct_body( $msg->body, $from ), to => "$user\@" . HOST, type => "chat", )->send($conn); undef $w if eof($fh); } );

Replies are listed 'Best First'.
Re^2: AnyEvent weirdness w/ open() and/or ``'s
by pprocacci (Initiate) on Nov 03, 2013 at 07:30 UTC
    James,

    I appreciate your response. My familiarity w/ AnyEvent isn't too great and your response has helped greatly. What I ended up doing which I believe to look extremely ugly follows my comments. (again heavily modified)

    To provide further details to hopefully paint a full picture .... I have two sources. One is from a command, and another is from a file. Both sources are pulled into an array and what I believe the below achieves is sending messages to the intersection of the arrays.

    The below works with my limited testing and I appreciate the guidance thus far you've provided. I don't need further help unless of coarse you see something glaringly wrong, but until then, thanks James.

    $muc->reg_cb( message => sub { my ( undef, undef, $msg ) = @_; my @connected_users = (); open my $fh, '-|', 'command_that_outputs_users' or die $!; my $w; $w = AnyEvent->io( fh => $fh, poll => 'r', cb => sub { my $user = $fh->getline; return if !defined($user); chomp($user); push(@connected_users, $user); if(eof($fh)){ close($fh); undef $w; open($fh, "< $file") or die $!; my $w2; $w2 = AnyEvent->io( fh => $fh, poll => 'r', cb => sub { my $user = $fh->getline; return if !defined($user); chomp($user); if($user ~~ @connected_users){ AnyEvent::XMPP::IM::Message->new( body => construct_body( $msg->body, $from ), to => "$user\@" . HOST, type => "chat", )->send($conn); close($fh); undef $w2; } } ); } } ); }, );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1060961]
help
Chatterbox?
[Lady_Aleena]: shmem, there is MP3::Info/
[Discipulus]: too much complex qx when you know perl are better to avoid
[marioroy]: might need to backslash \'*.mp3\'
[marioroy]: It's a quoting issue inside qx. It's File::Find or qx. LA chooses qx. Almost there.
[marioroy]: Also \"%S\\\n\" oh my, not fun.
[Discipulus]: good night folks
[marioroy]: I will make a mp3info script that takes the -p "%S\n" args and test qx. I'm on the road, so will come back.
[marioroy]: All because File::Find is not fun for LA.
[marioroy]: good night Discipulus.
[Discipulus]: perl dreams to you all

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2017-04-23 21:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (432 votes). Check out past polls.