#!/usr/bin/perl -w
my $new_root = 'UI';
my @corpa = split $/,<<'PKG';
package ::;
package :: ;
package ::Text;
package ::Text ;
package Audio::Ecasound;
@::RD::bigarray =
 @::RD::bigarray =
$::RD::-data 
 ::Text->loop;
::Text->loop;
::->prepare ;
 ::->prepare ;
(::->start);
our  @ISA = "::";
our  @ISA = " :: ";
our  @ISA = ":: ";
our  @ISA = " ::";
our  @ISA = " ::Text";
use ::Text;
use ::Text::Very::Unusual;
 use  ::;
isa '::';
::Module
 ::Module
(::Module
 (::Module
q(::);
	"::"
 '::Module'
'::Module'
   alsdkjfaldksjf asdlfkjasdklfjaskdlf '::Module';
$#::Track::by_index;
 $#::Track::by_index;
PKG
my $i;
map { print "input ", $i++,": ", $_, $/;
	  print "output"," : ", &subst($_), $/, $/;} @corpa;


sub subst {
	my $text = shift;
	# Substitution 1: symbols containing a sigil

		$text =~ s/([\%\$\@\&\#])(::)/$1$new_root$2/mg;

	# Substitution 2: package declarations (two steps)

		$text =~ s/^(\s*package\s*)(::)/$1$new_root$2/mg;
		$text =~ s/^(\s*package.*)(::)(\s*;)/$1$3/mg;

	# Substitution 3: @ISA (presumes 'our')

		#/^(\s*our\s+\@ISA.*\W)(::)(\W)/ and print "match 3.1"; #$1$new_root::$3/g;
		$text =~ s/^(\s*our\s+\@ISA.*\W)(::)(\W)/$1$new_root$3/mg;
		$text =~ s/^(\s*our\s+\@ISA.*\W)(::)((\w+::)*(\w+))+/$1$new_root$2$3/mg;

	# Substitution 4: method calls 1) ::->

		$text =~ s/(\W|^)(::->)/$1$new_root->/mg;
	  
	# Substitution 5: method calls 2) ::Graphical->refresh;
 							 	#  ::Text->loop;
	
		$text =~ s/(\W|^)(::)((\w+::)*(\w+)->)/$1$new_root$2$3/mg;

	# Substitution 6: use ::Module;
	
		$text =~ s/^(\s*use\s+)(::)(\W)/$1$new_root$3/mg;
		$text =~
		s/^(\s*use\s+)(::)((\w+::)*(\w+))+/$1$new_root$2$3/mg; 



	# Substitution 7: lone '::' and friends
		$text =~ s/(['"(])  # an open
					(::)	# a :: (class root)
					(['")])/$1$new_root$3'/gx;



	# Substitution 8: bare class
		#$text =~ s/(^|\s+)(::)((\w+::)*(\w+))+/$1$new_root$2$3/mg; 
		$text =~ s/(^|[\s'(])(::)((\w+::)*(\w+))+/$1$new_root$2$3/mg; 

$text;
}
