[Catalyst] BindLex removal helper script

Paul Makepeace paulm at paulm.com
Fri May 28 23:43:40 GMT 2010


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;
}



More information about the Catalyst mailing list