Pragmatism in the real world

Prefixing every key in a hash ref in Perl

I needed to prepend some text to every element in a Perl hash ref, so I came up with: $hashref->{"prefix_$_"} = delete($hashref->{$_}) foreach (keys %$hashref); which prefixes each key of $hashref with "prefix_". After talking it over with Cliff, he was concerned with modifying the list in place while simultaneously iterating over it and suggested this solution: %$hashref = map { +"prefix_$_" => $hashref->{$_} } keys %$hashref; One interesting thing about this solution is the… continue reading.

Missing mime-info database for use with File::MimeInfo

A requirement I have on a current project means that I need to determine the mime type of a file I load from disk. One way to do this is to use the File::MimeInfo::Magic CPAN module like this: use File::MimeInfo::Magic; my $mime_type = mimetype($path_to_file); However, on OS X, an error was raised: WARNING: You don't seem to have a mime-info database. The shared-mime-info package is available from http://freedesktop.org/ . The easiest way to solve this… continue reading.