general intermediate

How many years is Perl?

Question

How many years is Perl?

The question "How many years is Perl?" typically refers to calculating the age of Perl as a programming language, i.e., how many years have passed since Perl was first released. Perl is a highly influential and versatile scripting language created by Larry Wall, first released in December 1987.

To determine how many years Perl has existed, you can calculate the difference between the current year and the year Perl was initially released. This is a great example to demonstrate how to work with dates and times in Perl, how to extract the current year, and perform simple arithmetic.

Calculating Perl's Age in Years

Perl's initial public release was in December 1987. As of 2024, Perl is around 36-37 years old, depending on the month.

Here’s how to write a Perl script that calculates the exact number of years Perl has existed as of today. This example will:

  • Get the current year and month.
  • Compare it to Perl's initial release date (December 1987).
  • Calculate the number of full years.

This example covers some important Perl concepts:

  • localtime function to get the current date/time components.
  • Scalar context for localtime to get individual date parts.
  • How to do date math manually without external modules.
#!/usr/bin/perl
use strict;
use warnings;

# Perl first release year and month: December 1987 (month = 12)
my ($perl_year, $perl_month) = (1987, 12);

# Get current local time components
my ($sec, $min, $hour, $mday, $mon, $year) = localtime();

# localtime month is zero-based (0 = Jan), so add 1
$mon += 1;

# localtime year is years since 1900, so add 1900
$year += 1900;

# Calculate elapsed years between current date and Perl's release date
my $years_elapsed = $year - $perl_year;

# If current month is before Perl's release month, subtract one year
if ($mon < $perl_month) {
    $years_elapsed--;
}

print "Perl was first released in December 1987.\n";
print "As of today ($year-$mon), Perl is $years_elapsed years old.\n";

Explanation

The localtime function in list context returns the current second, minute, hour, day of the month, month (0-11), and year (years since 1900), among other fields. By adjusting these, you can get the current calendar year and month.

We then subtract Perl’s first release year from the current year. If the current month is before December, we know the full anniversary year has not yet passed, so we subtract one.

Perl Specific Notes

  • Perl’s localtime function is a core method of working with dates, without any external modules required.
  • Note the use of $mon + 1 and $year + 1900 to convert from Perl’s zero-based offsets.
  • This kind of manual date arithmetic is sufficient since we only deal with years and months here.

Common Pitfalls

  • Not adjusting the $mon and $year returned by localtime properly.
  • Confusing zero-based months (0–11) with standard months (1–12).
  • Failing to consider the month when calculating elapsed years can result in incorrect age calculation.

This simple example can be extended for days, hours, or more precise calculations using CPAN modules like DateTime, but for an intermediate Perl programmer, this example captures the basics well.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 6ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Perl was first released in December 1987.
As of today (2025-12), Perl is 38 years old.
STDERR
(empty)

Was this helpful?

Related Questions