Saturday, September 3, 2011

Working with DateTime.pm

Lately I have been using the DateTime module for handling dates. While it cuts down on some of the laborious aspects of dealing with time intervals and date formatting there are some methods that can be confusing.


1) delta always returns positive. I have been advised to use delta to get the differences between days, however it always returns a positive number. I imagine there is a good reason for this and it would be great if someone could tell me why it is useful when you have alternatives such as subtract().


2) now() always comes back in UTC Unless you read the docs it is easy to assume that you are going to get now() in your own time zone.


Given that daylight savings time observed in different ways in different countries, converting time-zones is confusing. The correct way to subtract dates with datetime according to the docs: Always do your date math in UTC. Then you can convert the dates to whatever timezone is appropriate when its time to display the results.


It looks to me like DateTime makes time manipulation simple enough without glossing over important details. Given it is tricky when the default is utc but one can easily come to an understanding of the module's behavior if they test appropriately.


For Docs Go to the CPAN

Thursday, September 9, 2010

Lazy Lists in Perl 6

Today I have been learning about laziness in perl6. Most specifically, I have been playing with gather { take }. You can find an excellent explanation in this Blog. Anyway. The example in the blog applies the x * (x + 1) as the function to be applied to the input list. I have played around putting in different functions and plugging in values. Eventually I came up with a script to spit out tax based on user input.



use v6;

my @tax_table = gather {
for 1..1000 {
take $_ * .0825;
}
}

my $amount = prompt("How much are you paying? ");

say "Tax Due is @tax_table[$amount]";


Everyone remotely interested in Perl 6 should go to the Perl 5 to 6 blog. So far it is the most useful Perl 6 resource I've seen.

Wednesday, June 2, 2010

One-liner: Finding files that include a match

These ARE the files you're looking for


Today I am going to share a one-liner I use often to find file names.
Say for example you would like to print all files in the current directory that include the word Fred. You could use this one-liner:

perl -wnl -e '/Fred/ and print $ARGV and close ARGV' *

w is for warnings
n is for looping
l is for line end processing

The "and close ARGV" part at the end is to save time and keep you from double-printing file names. If 'Fred' pops up 50 times in a file, and you don't add the "and close ARGV" then the file name will show up 50 times in standard out. Another benefit of this is that since you are closing the file after you find 'Fred' the first time, you will no longer continue to process the file and thus you are saving time.

Finding all matching lines


Another way I use this one liner is to find examples from config files. For example, say I have a directory full of configuration files and I want to see how many of them use the same option:

perl -wnl -e '/^option_name/ and print "$_\n"' /path/to/configs/*

This one-liner instead prints every match to standard out. We got rid of the and close ARGV this time, because we really don't need it. If we are matching the entire config option, it should only show up once in the file. Otherwise say we are trying to match multiple similar config options (e.g. option_name_1 option_name_2). In that case we would want to print out each match "and close ARGV" would only allow us to print the first one.

I hope this helps you the next time you need to glean information from a large number of files.

Friday, May 28, 2010

JLPT Module on CPAN?

I have yet to post anything to CPAN. I have been putting together a few ideas. The most likely will be a tool for studying for the Japanese Language Proficiency test. To start I would just like my code to generate vocabulary lists and keeps track of memorized words. Another thing to keep track of is memorized kanji.

I could then use it as the engine for a JLPT test preparation web app.