[Chicago-talk] More code that will send me to Perl Purgatory

Jonathan Rockway jon-chicagotalk at jrock.us
Thu Oct 22 01:04:03 PDT 2009


* On Tue, Oct 20 2009, Dan Rench wrote:
> At some point JSON.pm renamed its True() to true() and False() to
> false(). If there was a "deprecated" grace period that supported both
> it's past. The code I was working on needed to run on a set of servers
> with up-to-date JSON, as well as one black sheep that's running all
> ancient libraries on Perl 5.6.1. It couldn't get upgraded for various
> reasons.
>
> I ended up with this (comments removed because it's more fun that way):
>
> my $JSON_true = eval { JSON::true() } || eval { JSON::True() };
> my $JSON_false = eval { JSON::false() };
>
> if (! defined $JSON_false) {
>   $JSON_false = eval { JSON::False() };
> }

Uh, "exists" works on the symbol table:

  use JSON;
  say 'False' if exists $JSON::{False};
  say 'false' if exists $JSON::{false};

This means you can just ||= in a "reasonable default":

  BEGIN { $JSON::{False} ||= $JSON::{false} }

Regards,
Jonathan Rockway

--
print just => another => perl => hacker => if $,=$"


More information about the Chicago-talk mailing list