T
Ted Zlatanov
I find this function very useful:
{
my %counters = ();
sub every
{
my $id = shift @_;
my $when = shift @_;
return (0 == ++$counters{$id} % $when);
}
}
I call it like this:
print "debug info: ..."
if every("debug spot", 50);
print "extra info: ..."
if every("extra spot", 100);
So the debug info will be printed on every 50th call to every(). It's
small and convenient, but I have to make up a new ID every time.
I'd like to preserve the functional interface, so no OOP cheating. How
can I avoid the $id parameter, creating an automatically localized
every() call each time? I thought of __FILE__ and __LINE__ but those
are not unique enough, since you could potentially have
if (every(50)||every(33)) {}
on a line... I also thought of every(\&action_sub, 50) but that avoids
the problem sideways (it doesn't create a unique key, but makes the
action the key, which is not the same thing).
Ted
{
my %counters = ();
sub every
{
my $id = shift @_;
my $when = shift @_;
return (0 == ++$counters{$id} % $when);
}
}
I call it like this:
print "debug info: ..."
if every("debug spot", 50);
print "extra info: ..."
if every("extra spot", 100);
So the debug info will be printed on every 50th call to every(). It's
small and convenient, but I have to make up a new ID every time.
I'd like to preserve the functional interface, so no OOP cheating. How
can I avoid the $id parameter, creating an automatically localized
every() call each time? I thought of __FILE__ and __LINE__ but those
are not unique enough, since you could potentially have
if (every(50)||every(33)) {}
on a line... I also thought of every(\&action_sub, 50) but that avoids
the problem sideways (it doesn't create a unique key, but makes the
action the key, which is not the same thing).
Ted