Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Grabbing inotifywait output in perl?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
rev138
l33t
l33t


Joined: 19 Jun 2003
Posts: 848
Location: Vermont, USA

PostPosted: Sun Aug 03, 2008 3:23 am    Post subject: Grabbing inotifywait output in perl? Reply with quote

Invoking inotifywait with the -m flag makes it run indefinitely and print to stdout whenever an event is triggered.

I'd like to be able to invoke this from a perl script and have the script do something every time inotifywait writes to stdout, but I can't figure out how to accomplish this.

Any help is appreciated.
_________________
Vermont Free PC
http://www.vtfreepc.org
Back to top
View user's profile Send private message
widremann
Veteran
Veteran


Joined: 14 Mar 2005
Posts: 1314

PostPosted: Sun Aug 03, 2008 4:07 am    Post subject: Reply with quote

You'll want to use the IPC::Open2 module (perldoc IPC::Open2) to open a filehandle to your inotifywait process. You can then read from it in a loop in your script.
Back to top
View user's profile Send private message
rev138
l33t
l33t


Joined: 19 Jun 2003
Posts: 848
Location: Vermont, USA

PostPosted: Sun Aug 03, 2008 4:37 am    Post subject: Reply with quote

Thanks, but I'm afraid I don't have a ton of experience with file handles in perl, and that doc talks just slightly over my head.

Could I trouble you for a short code example?

Thanks again.
_________________
Vermont Free PC
http://www.vtfreepc.org
Back to top
View user's profile Send private message
zeek
Guru
Guru


Joined: 16 Nov 2002
Posts: 480
Location: Bantayan Island

PostPosted: Sun Aug 03, 2008 6:08 am    Post subject: Reply with quote

Why not go to CPAN and get an Inotify module?

http://search.cpan.org/~mlehmann/Linux-Inotify2-1.1/Inotify2.pm
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21631

PostPosted: Sun Aug 03, 2008 4:47 pm    Post subject: Reply with quote

Using an inotify module is a better solution in this case. For the more general question, consider this trivial script that runs /usr/bin/tac to print your /etc/make.conf in reverse, and sleeps as it goes:
Code:
open (CHILD, "/usr/bin/tac /etc/make.conf |") or die "Unable to open child for reading: $!";
while (<CHILD>) {
    sleep 1;
    print;
}
close(CHILD);


The key point is the presence of the pipe at the end of the open statement, which directs Perl to spawn a child process, with the child's stdout set to the write end of a pipe and the read end of the pipe stored in CHILD. If you need to both read and write the child, then a more complex construct is required.
Back to top
View user's profile Send private message
rev138
l33t
l33t


Joined: 19 Jun 2003
Posts: 848
Location: Vermont, USA

PostPosted: Sun Aug 03, 2008 9:00 pm    Post subject: Reply with quote

Thanks all! :)
_________________
Vermont Free PC
http://www.vtfreepc.org
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum