📂 Found in /usr/share/perl/5.34/pod/perlfaq4.pod
| Use Case | Command | Description |
|---|---|---|
| 📅 Compare two dates | use Time::Piece; my $date1 = localtime($some_time); my $date2 = localtime($some_other_time); if($date1 < $date2) {...} |
Use Time::Piece objects with overloaded comparison operators |
| 📅 Get date difference | my $date_diff = $date1 - $date2; print $date_diff->days; |
Subtraction returns a Time::Seconds object |
| 📅 Yesterday's date (correct) | use DateTime; my $yesterday = DateTime->now->subtract(days => 1); |
Uses calendar, handles DST correctly |
| 📅 Yesterday's date (Date::Calc) | use Date::Calc qw(Today_and_Now Add_Delta_DHMS); my @date_time = Add_Delta_DHMS(Today_and_Now(), -1, 0, 0, 0); |
Alternative calendar-based approach |
| 📝 Validate input | use Regexp::Common; use Business::ISBN; use Email::Valid; |
Use specialized validation modules for different data types |
| 💾 Randomly update binary file | perl -i -pe 's{window manager}{window mangler}g' /usr/bin/emacs |
Simple patch with one-liner, or fixed-size record approach |
| ⏰ Set system time | system('date ...'); $ENV{TZ} = "MST7MDT"; |
Use date(1) program or set environment variable |
🧑💻 (contributed by brian d foy)
You could just store all your dates as a number and then subtract. Life isn't always that simple though.
The Time::Piece module, which comes with Perl, replaces localtime with a version that returns an object. It also overloads the comparison operators so you can compare them directly:
use Time::Piece;
my $date1 = localtime( $some_time );
my $date2 = localtime( $some_other_time );
if( $date1 < $date2 ) {
print "The date was in the past\n";
}
You can also get differences with a subtraction, which returns a Time::Seconds object:
my $date_diff = $date1 - $date2;
print "The difference is ", $date_diff->days, " days\n";
If you want to work with formatted dates, the Date::Manip, Date::Calc, or DateTime modules can help you.
🧑💻 (contributed by brian d foy)
To do it correctly, you can use one of the "Date" modules since they work with calendars instead of times. The DateTime module makes it simple, and give you the same time of day, only the day before, despite daylight saving time changes:
use DateTime;
my $yesterday = DateTime->now->subtract( days => 1 );
print "Yesterday was $yesterday\n";
You can also use the Date::Calc module using its "Today_and_Now" function.
use Date::Calc qw( Today_and_Now Add_Delta_DHMS );
my @date_time = Add_Delta_DHMS( Today_and_Now(), -1, 0, 0, 0 );
print "@date_time\n";
Most people try to use the time rather than the calendar to figure out dates, but that assumes that days are twenty-four hours each. For most people, there are two days a year when they aren't: the switch to and from summer time throws this off. For example, the rest of the suggestions will be wrong sometimes:
Starting with Perl 5.10, Time::Piece and Time::Seconds are part of the standard distribution, so you might think that you could do something like this:
use Time::Piece;
use Time::Seconds;
my $yesterday = localtime() - ONE_DAY; # WRONG
print "Yesterday was $yesterday\n";
The Time::Piece module exports a new "localtime" that returns an object, and Time::Seconds exports the "ONE_DAY" constant that is a set number of seconds. This means that it always gives the time 24 hours ago, which is not always yesterday. This can cause problems around the end of daylight saving time when there's one day that is 25 hours long.
You have the same problem with Time::Local, which will give the wrong answer for those same special cases:
# contributed by Gunnar Hjalmarsson
use Time::Local;
my $today = timelocal 0, 0, 12, ( localtime )[3..5];
my ($d, $m, $y) = ( localtime $today-86400 )[3..5]; # WRONG
printf "Yesterday: %d-%02d-%02d\n", $y+1900, $m+1, $d;
🧑💻 (contributed by brian d foy)
There are many ways to ensure that values are what you expect or want to accept. Besides the specific examples that we cover in the perlfaq, you can also look at the modules with "Assert" and "Validate" in their names, along with other modules such as Regexp::Common.
Some modules have validation for particular types of input, such as Business::ISBN, Business::CreditCard, Email::Valid, and Data::Validate::IP.
📂 Found in /usr/share/perl/5.34/pod/perlfaq5.pod
If you're just trying to patch a binary, in many cases something as simple as this works:
perl -i -pe 's{window manager}{window mangler}g' /usr/bin/emacs
However, if you have fixed sized records, then you might do something more like this:
my $RECSIZE = 220; # size of record, in bytes
my $recno = 37; # which record to update
open my $fh, '+
Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-12 21:47 @216.73.217.60
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)