Gradual method renaming in Perl

We have a huge code­base of over 700,000 lines of Perl spread across a cou­ple dozen Gitrepos­i­to­ries at work. Sometimes refac­tor­ingis easy if the class­es and meth­ods involved are con­fined to one of those repos, but last week we want­ed to rename a method that was poten­tial­ly used across many of them with­out hav­ing to QA and launch so many changes. After get­ting some help from Dan Bookand Ryan Vootson the #perl libera.chat IRC chan­nel, I arrived at the fol­low­ing solution.

First, if all you want to do is alias the new method call to the old while mak­ing the least amount of changes, you can just do this:

*new_method = \&old_method;

This takes advan­tage of Perl’s type­globsby assign­ing to the new method­’s name in the sym­bol table a ref­er­ence(indi­cat­ed by the \char­ac­ter) to the old method. Methods are just sub­rou­tinesin Perl, and although you don’t need the &char­ac­ter when call­ing one, you do need it if you’re pass­ing a sub­rou­tine as an argu­ment or cre­at­ing a ref­er­ence, as we’re doing above.

I want­ed to do a bit more, though. First, I want­ed to log the calls to the old method name so that I could track just how wide­ly it’s used and have a head start on renam­ing it else­where in our code­base. Also, I did­n’t want to fill our logs with those calls—we have enough noise in there already. And last­ly, I want­ed future calls to go direct­ly to the new method name with­out adding anoth­er stack frame when using calleror Carp.

With all that in mind, here’s the result:

sub old_method {
    warn 'old_method is deprecated';
    no warnings 'redefine';
    *old_method = \&new_method;
    goto &new_method;
}

sub new_method {
    # code from old_method goes here
}

Old (and not-​so-​old) hands at pro­gram­ming are prob­a­bly leap­ing out of their seats right now yelling, ​“YOU’RE USING GOTO! GOTO IS CONSIDERED HARMFUL!” And they’re right, but this isn’t Dijkstra’s goto. From the Perl manual:

The goto &NAMEform is quite dif­fer­ent from the oth­er forms of goto. In fact, it isn’t a goto in the nor­mal sense at all, and does­n’t have the stig­ma asso­ci­at­ed with oth­er gotos. Instead, it exits the cur­rent sub­rou­tine (los­ing any changes set by local) and imme­di­ate­ly calls in its place the named sub­rou­tine using the cur­rent val­ue of @_. […]After the goto, not even callerwill be able to tell that this rou­tine was called first.

perl­funcman­u­al page

Computer sci­en­tists call this tail call elim­i­na­tion. The bot­tom line is that this achieves our third goal above: imme­di­ate­ly jump­ing to the new method as if it were orig­i­nal­ly called.

The oth­er tricky bit is in the line before, when we’re redefin­ing old_methodto point to new_methodwhile we’re still inside old_method. (Yes, you can do this.) If you’re run­ning under use warnings(and we are, and you should), you first need to dis­able that warn­ing. Later calls to old_methodwill go straight to new_methodwith­out log­ging anything.

And that’s it. The next step after launch­ing this change is to add a sto­ry to our back­logto mon­i­tor our logs for calls to the old method, and grad­u­al­ly refac­tor our oth­er repos­i­to­ries. Then we can final­ly remove the old method wrapper.


Discover more from The Phoenix Trap

Subscribe to get the latest posts sent to your email.

Mark Gardner Avatar

Hi, I’m Mark.

Hi, I’m Mark Gard­ner, and this is my personal blog. I show software developers how to level up by building production-ready things that work. Clear code, real projects, lessons learned.

Comments

6 responses to “Gradual method renaming in Perl”

  1. sigzero Avatar

    You have this up top:

    *new_​method = \&old_method;

    And this in your code:

    *old_​method = \&new_method;

    1. Mark Gardner Avatar

      That’s cor­rect. In the first instance I’m just adding an alias so that we can call the old method by the new method­’s name with­out chang­ing the old method­’s code. In the sec­ond instance I’m chang­ing the old method­’s code to alias itself to the new method, and then mov­ing the old method­’s code into the new method.

  2. Matthew Persico Avatar

    The ​“goto” here is kin­da like a ​“exec”: drop what you’re doing and start doing some­thing else.

  3. Evan Carroll Avatar
    Evan Carroll

    I cre­at­ed Sub::Deprecateafter read­ing this blog. You may find it useful.

    1. Mark Gardner Avatar

      There’s also Ovid’s Devel::Deprecate

      1. Evan Carroll Avatar

        Didn’t know that exist­ed. 😉 But yeah, I like your method bet­ter then Ovid’s there as you can dep­re­cate subs in remote pack­ages. I just want­ed to make it higher-​ordered. But where­as Ovid’s imple­men­ta­tion has warn/​die/​if, mine takes a call­back which allows you to be more clever in report­ing. Then again, I guess the call-​back does­n’t curent­ly receive the argu­ments to the function.

Your post must include a link to this post.

How does this work?
Write a response on your own website and include a link to this post. Then submit your response’s URL here. Your response may appear here after moderation. To update or remove it, edit or delete your original post and submit the same URL again. Learn more about Webmentions.