#!/usr/bin/perl
use strict;
use warnings;
my $input = 'infile.txt' ;
my $output = 'outfile.txt';
my $t0 = time();
open my $in_fh, '<', $input or die "Can't open $input: $!";
open my $out_fh, '>', $output or die "Can't open $output: $!";
my $s = 1234;
my @id = (); my $count = 0;
while (my $line = <$in_fh>){
my @pos = ();
while ($line =~ m/([»«›‹])/g) {
my $q = $1;
if ($q =~ /[»›]/){
push @pos,[pos($line),qq{}];
push @id,$s++
} else {
my $e = pop @id;
push @pos,[pos($line),qq{}];
}
}
# reverse to preserve positions after replacement
for (reverse @pos){
substr($line,$_->[0]-1,1) = $_->[1];
}
print $out_fh $line;
++$count;
}
close $in_fh or die $!;
close $out_fh or die $!;
my $dur = $t0-time();
print "$count lines processed in $dur seconds\n";