Why are use warnings; use strict; not default in Perl?

It’s for backwards compatibility. Perl 4 didn’t have strict at all, and there are most likely still scripts out there originally written for Perl 4 that still work fine with Perl 5. Making strict automatic would break those scripts. The situation is even worse for one-liners, many of which don’t bother to declare variables. Making … Read more

What’s the difference between iterating over a file with foreach or while in Perl?

For most purposes, you probably won’t notice a difference. However, foreach reads each line into a list (not an array) before going through it line by line, whereas while reads one line at a time. As foreach will use more memory and require processing time upfront, it is generally recommended to use while to iterate … Read more

What does =~ do in Perl? [closed]

=~ is the operator testing a regular expression match. The expression /9eaf/ is a regular expression (the slashes // are delimiters, the 9eaf is the actual regular expression). In words, the test is saying “If the variable $tag matches the regular expression /9eaf/ …” and this match occurs if the string stored in $tag contains … Read more

How do I convert a date/time to epoch time (unix time/seconds since 1970) in Perl?

If you’re using the DateTime module, you can call the epoch() method on a DateTime object, since that’s what you think of as unix time. Using DateTimes allows you to convert fairly easily from epoch, to date objects. Alternativly, localtime and gmtime will convert an epoch into an array containing day month and year, and … Read more

Character Translation using Python (like the tr command)

See string.translate import string “abc”.translate(string.maketrans(“abc”, “def”)) # => “def” Note the doc’s comments about subtleties in the translation of unicode strings. And for Python 3, you can use directly: str.translate(str.maketrans(“abc”, “def”)) Edit: Since tr is a bit more advanced, also consider using re.sub.

What reasons are there to prefer glob over readdir (or vice-versa) in Perl?

You missed the most important, biggest difference between them: glob gives you back a list, but opendir gives you a directory handle. You can pass that directory handle around to let other objects or subroutines use it. With the directory handle, the subroutine or object doesn’t have to know anything about where it came from, … Read more