[Chicago-talk] More code that will send me to Perl Purgatory
Steven Lembark
lembark at wrkhors.com
Sun Oct 25 21:23:01 PDT 2009
On Thu, 22 Oct 2009 03:04:03 -0500
Jonathan Rockway <jon-chicagotalk at jrock.us> wrote:
> * 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};
Sorry for the delayed response.
Minimal installer for a named sub:
use Symbol qw( qualify_to_ref );
my $handler = \&JSON::False;
my $ref = qualify_to_ref 'false', 'JSON';
# undef avoids warnings w/strict.
undef &$ref;
# assign a ref to a glob => install symbol.
*$ref = $handler;
with added sanity check:
...
my $ref = qualify_to_ref 'false', 'JSON';
*{ $ref{ CODE ] } # it's already there
or # or
*$ref = $handler; # put it where belongs.
Given that False may not be defined (i.e., this is the
pre-deprecation code):
my $src = qualify_to_ref 'JSON::False';
if( my $handler = *{ $src }{ CODE } )
{
my $dst = qualify_to_ref 'JSON::False';
if( *{ $dst }{ CODE } )
{
# already got falsies, maybe need pasties?
}
else
{
# ok, False exists, false does not: add false.
*$dst = $handler;
}
}
else
{
# no Falsies here at all.
}
Symbol makes all of this a helluva lot easier.
--
Steven Lembark 85-09 90th St.
Workhorse Computing Woodhaven, NY, 11421
lembark at wrkhors.com +1 888 359 3508
More information about the Chicago-talk
mailing list