We have a moderately sized app with hundreds of lines of :Stashed -- I
wrote this script to help convert. No mistake, I still needed a bunch
of manual tweaking, mostly summarized in the comments, but it was
certainly easier than manually doing everything.

It's possible no-one on Earth else still uses BindLex, but in case they do...

#!/usr/bin/perl

use strict;
use warnings;

# Script to ease some of the pain of removing C::C::BindLex
# It looks for :Stashed vars and adds them to the stash when the sub ends.
# There are a few caveats,
#    if the sub returns before the stash assignments they won't get picked up
#    the program naively takes a } starting a line as a sub end
#    if the last statement in your sub is a return, move the stash
assignments up

# Paul Makepeace, Investor Dynamics, 2010

my $indent = "\t";      # adjust this to your preferred indentation scheme
my @vars;

sub to_stash {
        my ($type, $var) = @_;
        $type = "\\$type" unless $type eq '$';
        return "$indent\$c->stash->{$var} = $type$var;\n";
}

while (<>) {
        s/(use base.+Catalyst::Controller)::BindLex/$1/ and next;
        if (/^}\s*$/ and @vars) {       # detect end of sub & some stashed vars
                foreach my $var (sort {$a->[1] cmp $b->[1]} @vars) {
                        print to_stash @$var;
                }
                @vars = ();
        } elsif (s/(my ([...@\%])(\w+))\s*:\s*Stashed\s*/$1 /) {
                push @vars, [$2, $3];   # type, name
                s/ ;/;/g;
                s/$/$indent# Stashed/;
        }
} continue {
        print;
}

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Reply via email to