Devel::Util - a collection of general-utility development subroutines
use Devel::Util qw(dt tz oiaw printr forked);
Devel::Util contains a collection of subroutines to be used during the development phase, and also possibly in production.
Devel::Util
By default Devel::Util does not export any subroutines.
dt { my $i = 10_000_000; rand while $i--; } '10 million rands';
Executes BLOCK and print a timing message to STDERR when done, similar to the Linux time commands, reporting real, user and sys times taken by the execution of block up to the millisecond.
time
The NAME argument is optional and inteneded to help identify the timed block in the printed message. If none is provided the filename and line of the dt command is used.
dt
Similarly to do, dt returns the value of the last command of the block, evaluated with the same context as the dt command itself.
do
dt does not print any timing message if $Devel::Util::QUIET is true.
$Devel::Util::QUIET
Setting $Devel::Util::QUIET to true prevents dt from printing any timing message.
tz { printf "Current time is %s\n", scalar localtime } 'Pacific/Honolulu';
Executes BLOCK, locally setting the TZ environement variable and calling POSIX::tzset
tzset
Similarly to do, tz returns the value of the last command of the block, evaluated with the same context as the tz command itself.
tz
my $i = 0; my $t = time; while (time-$t < 3) { ++$i; oiaw { print STDERR "\rprogress: $i" } 0.5; }
Execute BLOCK only when at least TIME seconds have passed since the last call to that same block.
TIME can be fractional.
oiaw can also return a code reference to be used afterward:
oiaw
my $i = 0; my $t = time; my $progress = oiaw { print STDERR "\rprogress: $i" } 0.5; while (time-$t < 3) { ++$i; $progress->(); }
This second form is faster as the closure is only created once, and also makes it easier to silence by setting the code reference to an empty sub.
printr 'this'; sleep 1; printr 'is'; sleep 1; printr 'simple';
printr prints STR to STDERR, replacing anything formerly printr-ed on the same line.
printr
It uses \b characters to delete previous messages in a controlled way, playing nice with \n, shorter strings, or strings with a common prefix.
If more than one argument is used, printr formats its output similarly to what printf does:
printf
printr '1+1=%d', 1+1; # same as: printr sprintf('1+1=%d', 1+1);
It makes sure everything that is common is not deleted, and that everything that is longer than the new message.
Of course any interlaced direct call to print STDERR will prevent printr to work properly on a given line.
print STDERR
Setting $Devel::Util::QUIET to true makes printr no-op.
if (forked) { warn 'We are in a forked process' } else { warn 'We are in the main process' }
Returns true if current PID is different than when the module was loaded.
If set to true, dt and printr do not print anything.
oiaw is not affected as it can be used to do other things than printing progress.
This variable has also no effect on tz and forked.
forked
Nothing by default. Functions can be imported explicitly, by their short or long aliases
# short use Devel::Util qw(dt tz oiaw printr forked); # long use Devel::Util qw(do_time timezone once_in_a_while print_refresh forked); # everything use Devel::Util qw(:all);
Scalar::Util, List::Util
Thomas Drugeon, <tdrugeon@cpan.org>
Copyright (C) 2010-2021 by Thomas Drugeon
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10 or, at your option, any later version of Perl 5 you may have available.
To install Devel::Util, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Devel::Util
CPAN shell
perl -MCPAN -e shell install Devel::Util
For more information on module installation, please visit the detailed CPAN module installation guide.