#!/usr/bin/perl

use strict;
use Data::Dumper;

importer();

sub importer{
  local $/=undef;
  my $t=<>;
  my $chunks = extractor($t);
  print Dumper($chunks);
  print "[[", replacer($t,$chunks), "]]\n";
}

sub extractor{
  my $f = shift;
  my @chunks;
  my @types = qw(abc dot gnuplot);
  my $typespat = join("|",@types);    ## common to all the cases

  my $beginRegexp = qr{\\begin\{inline_($typespat)\}};
  my $endRegexp   = qr{\\end\{inline_(\1)\}};
  my $extractor   = qr{$beginRegexp(.*?)$endRegexp}s;

  while($f =~ m/$extractor/g){
     push(@chunks,[$-[0], $+[0],$1,$2]); 
  }
  return \@chunks
}

sub replacer{
  my ($p,$chunks)=@_;
  for my $ch(reverse @$chunks){
     substr($p,$ch->[0],$ch->[1]-$ch->[0]) = "batatas";
  }
  $p
}
