Created
May 8, 2009 02:05
-
-
Save rjray/108556 to your computer and use it in GitHub Desktop.
Some playing-around with the Linux::Inotify2 Perl module, for tracking what/how many files are loaded as Perl runs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# Playing around with acme's sample code around Linux::Inotify2. | |
# See http://acme.vox.com/library/post/what-files-does-your-perl-load.html | |
use strict; | |
use warnings; | |
use Linux::Inotify2; | |
use File::Find::Rule; | |
use Getopt::Long; | |
our $inotify = new Linux::Inotify2 | |
or die "Unable to create new inotify object: $!"; | |
# Very basic args to add/exclude dirs from watching-- "--dir DIR" adds a | |
# dir to the list, "--no DIR" excludes one. After command-line additions | |
# are processed, everything in @INC is added unless it was flagged via --no. | |
our (@dirs, $noinc, $showhelp, %exclude); | |
our $USAGE = <<"EO_Usage"; | |
Usage: | |
$0 [ --help ] [ --noinc ] [ --directory DIR ] [ --nodir DIR ] | |
Where: | |
--help Display this summary | |
--noinc Do not add the contents of \@INC to the list of | |
watched dirs | |
--directory DIR Add the specified DIR to the set of watched dirs. | |
May appear more than once. | |
--nodir DIR Remove the specified DIR from the list of watched | |
dirs (such as dirs that are children of other | |
watched dirs, or part of \@INC) | |
EO_Usage | |
GetOptions('directory=s' => \@dirs, | |
'noinc' => \$noinc, | |
'help' => \$showhelp, | |
'nodir=s' => sub { $exclude{$_[1]}++ }) | |
or die "$USAGE"; | |
if ($showhelp) | |
{ | |
print $USAGE; | |
exit; | |
} | |
unless ($noinc) | |
{ | |
for (@INC) | |
{ | |
push(@dirs, $_) unless $exclude{$_}; | |
} | |
} | |
foreach my $inc (@dirs) | |
{ | |
next if $inc eq '.'; | |
foreach my $directory (File::Find::Rule->new->directory->in($inc)) | |
{ | |
$inotify->watch($directory, IN_OPEN | IN_ACCESS) | |
or die "watch creation failed on $directory"; | |
} | |
} | |
while () | |
{ | |
my @events = $inotify->read; | |
unless (@events > 0) | |
{ | |
print "read error: $!\n"; | |
last; | |
} | |
foreach my $event (@events) | |
{ | |
printf("[%s] %s\n", $event->IN_OPEN ? 'O' : 'A', $event->fullname); | |
} | |
} | |
#!/usr/bin/perl | |
# Playing around with acme's sample code around Linux::Inotify2. | |
# See http://acme.vox.com/library/post/what-files-does-your-perl-load.html | |
use strict; | |
use warnings; | |
use Linux::Inotify2; | |
use File::Find::Rule; | |
use Getopt::Long; | |
our $inotify = new Linux::Inotify2 | |
or die "Unable to create new inotify object: $!"; | |
# Very basic args to add/exclude dirs from watching-- "--dir DIR" adds a | |
# dir to the list, "--no DIR" excludes one. After command-line additions | |
# are processed, everything in @INC is added unless it was flagged via --no. | |
our (@dirs, $noinc, $showhelp, %exclude); | |
our $USAGE = <<"EO_Usage"; | |
Usage: | |
$0 [ --help ] [ --noinc ] [ --directory DIR ] [ --nodir DIR ] | |
Where: | |
--help Display this summary | |
--noinc Do not add the contents of \@INC to the list of | |
watched dirs | |
--directory DIR Add the specified DIR to the set of watched dirs. | |
May appear more than once. | |
--nodir DIR Remove the specified DIR from the list of watched | |
dirs (such as dirs that are children of other | |
watched dirs, or part of \@INC) | |
EO_Usage | |
GetOptions('directory=s' => \@dirs, | |
'noinc' => \$noinc, | |
'help' => \$showhelp, | |
'nodir=s' => sub { $exclude{$_[1]}++ }) | |
or die "$USAGE"; | |
if ($showhelp) | |
{ | |
print $USAGE; | |
exit; | |
} | |
unless ($noinc) | |
{ | |
for (@INC) | |
{ | |
push(@dirs, $_) unless $exclude{$_}; | |
} | |
} | |
foreach my $inc (@dirs) | |
{ | |
next if $inc eq '.'; | |
foreach my $directory (File::Find::Rule->new->directory->in($inc)) | |
{ | |
$inotify->watch($directory, IN_OPEN | IN_ACCESS) | |
or die "watch creation failed on $directory"; | |
} | |
} | |
while () | |
{ | |
my @events = $inotify->read; | |
unless (@events > 0) | |
{ | |
print "read error: $!\n"; | |
last; | |
} | |
foreach my $event (@events) | |
{ | |
printf("[%s] %s\n", $event->IN_OPEN ? 'O' : 'A', $event->fullname); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment