title: Perl: how to skip a loop if the task takes too long
body: <p>This perl code processes txt files in a folder and it's subdirectories into a header, a body and an xml.</p>
  
  <pre><code>    #!perl -w
  
  use strict;
  use utf8;
  use File::Copy;
  use File::Basename;
  
  our @folders=();
  our %errors=();
  our $page_errors='';
  our $folder_out='';
  our $folder_in='';
  our $sub_folder="";
  our $dev=0;
  our $anker='#a_';
  our $coded_lb=0;
  our $line_cnt=0;
  
  sub get_complete_filename
  {
      my $return = $_[0];
      $return=~m/([^\d]+)(\d+)/;
      return $return if (!$1 || !$2);
      my $name=$1;
      my $number=$2;
  
      open (IN,"&lt;:encoding(utf-8)","..\\..\\complete_filenames.txt");
      while (&lt;IN&gt;)
      {
          my $line=$_; chomp($line);
          next if ($line eq '' || $line=~m/Datei/);       
          if ($line=~m/$name[a-zA-Z_-]+$number/)
          {
              $return = $line;
              last;
          }       
      }   
      close IN;
      return $return;
  }
  
  sub get_files 
  {   
      my $dir = $_[0];
      my $file;
      opendir(DIR, $dir) || die "Unable to open $dir: $!";
      my @fl = grep {!/^\.\.?$/ } readdir(DIR);
      closedir(DIR);
      foreach (@fl) 
      {
          if (-d ($file = "$dir\\$_")) 
          {
              push(@folders,$file); 
              get_files($file); 
          } 
      }
  }
  
  sub header
  {
      my $fn=$_[0];
      my $folder_in=$_[1];
      my $folder_out=$_[2];
  
      if (-e ($folder_in."\\".$fn.".teih"))
      {
          open (IN,"&lt;:encoding(utf-8)", $folder_in."\\".$fn.".teih");
          my $input = do { local $/; &lt;IN&gt; };
          close IN;
          my @lines=split(/[\n\r]/,$input);   
          my $read=0;
          my $output="";
          foreach my $line (@lines) 
          {
              chomp($line);
              if($line=~m/&lt;\/teiHeader&gt;/)
              {
                  $read=0;
                  $output.=$line."\n";
                  last;
              }
              elsif ($read eq 1 || $line=~m/&lt;teiHeader&gt;/)
              {
                  $read=1;
                  $output.=$line."\n";
              }
          }           
          open (OUT,"&gt;:encoding(utf-8)", $folder_out.($dev eq 0 ? "\\".$sub_folder : "")."\\".$fn.".teih");
          print OUT $output;
          close OUT;
          #copy($folder_in."\\".$fn.".teih", $folder_out."\\".$fn."\\".$fn.".teih"); 
      }
      else
      {   
      open (H,"&gt;:encoding(utf-8)", $folder_out.($dev eq 0 ? "\\".$sub_folder : "")."\\".$fn.".teih");
      print H "\n\t&lt;!--\n\t copy of the main_header or empty header!!!\n\t please update its content\n\t--&gt;\n".
  '&lt;teiHeader&gt;
      &lt;fileDesc&gt;
          &lt;titleStmt&gt;
              &lt;title/&gt;
              &lt;respStmt&gt;
                  &lt;resp/&gt;
                  &lt;name/&gt;
              &lt;/respStmt&gt;
          &lt;/titleStmt&gt;
          &lt;publicationStmt&gt;
              &lt;distributor/&gt;
          &lt;/publicationStmt&gt;
          &lt;sourceDesc&gt;
              &lt;bibl/&gt;
          &lt;/sourceDesc&gt;
      &lt;/fileDesc&gt;
  &lt;/teiHeader&gt;';
      close H;
      }
  
      return '&lt;?xml version="1.0" encoding="utf-8"?&gt;'; 
  }
  
  sub check_linebreak
  {
      my $line=$_[0]; my $pg=$_[1];
      my $ret="";
  
      $line_cnt++;
      if ($line=~m/([აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ\-]+)\/([აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ\-]+[[:punct:]]*)/)
      {                   
          my $tmp1=$1; my $tmp2=$2;
          my $z="TRENNENDERZBTRENNENDERZB".($line_cnt+1)."TRENNENDERZBTRENNENDERZB";
          $line=~s/\Q$tmp1\E\/\Q$tmp2\E/$tmp1$z$tmp2/; $line.=" ";
          if ($coded_lb eq 0)
          {
              $ret=$pg."NORMALERZBNORMALERZB".$line_cnt."NORMALERZBNORMALERZB ".$line;
          }
          else
          {
              $ret=$pg.$line; 
          }
          $coded_lb=1;        
      }
      else
      {
          if ($coded_lb eq 0)
          {
              $ret=$pg."NORMALERZBNORMALERZB".$line_cnt."NORMALERZBNORMALERZB ".$line;
          }
          else
          {
              $ret=$pg.$line;                     
          }
          $coded_lb=0;
      }
      return $ret;
  }
  
  sub anfangs_verarbeitung
  {
      my $tmp=$_[0];
  
      $tmp =~ s/^\x{FEFF}//;  # removes BOM
  
  
      $tmp =~ s/#(?:(?:\r\n)|\n|\r)+(\-{2,})/#$1\n/mg;
      $tmp =~ s/^p\s*$//g;
  
      $tmp =~ s/^\s*([pP]\d+)\s*([cC]\s*[0-9IVX]+)/$1\n$2/g;
  
      $tmp =~ s/(?&lt;=#)\|(?=[pppctPCT])//g;
      $tmp =~ s/\|(?=[pppctPCT])/#/g;
      $tmp =~ s/(?&lt;![\|#])([pppcPC]\s*[\dIVXMC]+)+/#$1/g;
      $tmp =~ s/\|(\d+)/#p$1/g;
  
      $tmp =~ s/«(?=[აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ])/»/g;
      $tmp =~ s/»(?![აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ])/«/g;
      $tmp =~ s/&lt;&lt;(?=[აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ])/»/g;
      $tmp =~ s/&gt;&gt;(?![აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ])/«/g;
      $tmp =~ s/(?:„|,,|")([აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ]+)«/»$1«/g;
      $tmp =~ s/»([აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ]+)[“"']/»$1«/g;
  
  
  
      $tmp =~ s/^(\d{4}\s*წ\.)\s*$/#d $1/g;
  
      $tmp =~ s/&lt;pol&gt;/&lt;pol&gt;/g;    
  
      $tmp =~ s/&lt;ა&gt;/&lt;a&gt;/g;    
      $tmp =~ s/&lt;\?([athzee])&gt;/&lt;\/$1&gt;/g;                  #&lt;?a&gt;           
      $tmp =~ s/[&lt;&gt;]\/([athzee])(?![&lt;&gt;])/&lt;\/$1&gt;/g;            #&lt;/a &gt;/a            
      $tmp =~ s/&lt;([athzee])\/&gt;/&lt;\/$1&gt;/g;                  #&lt;a/&gt;
      $tmp =~ s/[&lt;&gt;]\/([athzee])[&lt;&gt;]/&lt;\/$1&gt;/g;                #&gt;/a&gt; etc.          
      $tmp =~ s/[&lt;&gt;]([athzee])[&lt;&gt;]/&lt;$1&gt;/g;                    #&gt;a&lt; etc.
      $tmp =~ s/&lt;([athzee])(?![&lt;&gt;])/&lt;$1&gt;/g;                   #&lt;a &gt;a          
      $tmp =~ s/(?&lt;=[^&gt;&lt;\/#])([athzee])[&lt;&gt;]/&lt;$1&gt;/g;           #a&lt; a&gt;          
      $tmp =~ s/(?&lt;=[^&gt;&lt;])\/([athzee])[&lt;&gt;]/&lt;\/$1&gt;/g;      #/a&lt; /a&gt;    
  
      #$tmp =~ s/&lt;\/([athze])&gt;([^&lt;]+)&lt;\/[^\1]&gt;/&lt;$1&gt;$2&lt;\/$1&gt;/gm;
  
      ##$tmp =~s/&lt;([athz])&gt;([^&lt;]+)&lt;\/[^(?:$1)]&gt;/&lt;$1&gt;$2&lt;\/$1&gt;/g;   
      ##$tmp =~s/&lt;([athz])&gt;([^&lt;]+)&lt;[^(?:\1)]&gt;/&lt;$1&gt;$2&lt;\/$1&gt;/g;
  
      $tmp =~ s/&lt;([pol])&gt;([^&lt;]+)&lt;\/\1&gt;-&lt;\1&gt;([^&lt;]+)&lt;\/\1&gt;/&lt;$1&gt;$2-$3&lt;\/$1&gt;/g;
      #$tmp =~ s/&lt;([athze])&gt;([^&lt;]+)&lt;\/\1&gt;[\-\-]&lt;([athze])&gt;([^&lt;]+)&lt;\/\3&gt;/&lt;$1&gt;$2-$4&lt;\/$1&gt;/g;
  
      ##$tmp =~ s/([^\s]+)\-&lt;([athz])&gt;([^&lt;]+)&lt;\/\2&gt;/&lt;$2&gt;$1\-$3&lt;\/$2&gt;/g;
      ##$tmp =~ s/&lt;name([^&gt;]+)&gt;([^&lt;]+)&lt;\/name&gt;//g;
  
      $tmp =~ s/&lt;\/&lt;/&lt;/g;
  
      ####$tmp =~ s/&lt;\/(?![athzee])//g;
  
      $tmp =~ s/#{2,}/#/g;
  
      $tmp =~ s/\(\/\/\?([^\)]*)\)/&lt;unclear&gt;$1&lt;\/unclear&gt;/gm;
      $tmp =~ s/&lt;unclear&gt;&lt;\/unclear&gt;/&lt;unclear\/&gt;/g;
      $tmp =~ s/\(\/\/([^\)]+)\)/&lt;corr&gt;$1&lt;\/corr&gt;/gm;
  
      #$tmp =~ s/&lt;s(\d+)&gt;([^&lt;]+)&lt;\/s\1&gt;/$2&lt;ref target="#a$1" type="noteAnchor"&gt;$1&lt;\/ref&gt;/gm;
      $tmp =~ s/&lt;[sS](\d+)&gt;/&lt;ref target="a$1" type="noteAnchor"&gt;/g;
      $tmp =~ s/&lt;\/[sS]\d+&gt;/&lt;\/ref&gt;/g;
      $tmp =~ s/\([sS](\d+)=?\s*([^\)]+)\)/&lt;note xml:id="a$1" type="footnote"&gt;$2&lt;\/note&gt;\n/gm;
  
      #$tmp =~ s/#f(\d+)\s*(.*)([^#\|]+)/&lt;note xml:id="a$1" type="footnote"&gt;$2&lt;\/note&gt;\n\n/gm;
      #$tmp =~ s/\(s\s*(\d+)\s*([^\)]+)\)/&lt;note xml:id="a$1" type="footnote"&gt;$2&lt;\/note&gt;\n\n/gm;
      $tmp =~ s/\n{1,}&lt;\/note&gt;/&lt;\/note&gt;/gm;
  
      #$tmp =~ s/\s*#\-{2,}//gm;  
  
      $tmp=~s/ვი\$/ჳ/g;
      $tmp=~s/ხ\$/ჴ/g;
      $tmp=~s/ე\$/ჱ/g;
      $tmp=~s/ი\$/ჲ/g;
      $tmp=~s/ფ\$/ჶ/g;
      $tmp=~s/ვ\$/უ/g;
      $tmp=~s/ო\$/ჵ/g;
  
      $tmp=~s/#\.{2,}/#\-\-\-\-\-\-\-\-\-\-\-\-\-\-/g;
  
      return $tmp;
  }
  
  sub end_verarbeitung
  {
      my $tmp=$_[0];
      $tmp =~ s/[\n\r]{2,}/\n/g;
      $tmp =~ s/&lt;p&gt;\s+/&lt;p&gt;/g;
      $tmp =~ s/&lt;\/p&gt;\s+/&lt;\/p&gt;/g;
      $tmp =~ s/&lt;p&gt;&lt;\/p&gt;//g;
      $tmp =~ s/&lt;div&gt;&lt;p&gt;&lt;div type="dateline"&gt;/&lt;div type="dateline"&gt;/g;
      $tmp =~ s/&lt;p&gt;&lt;div type="dateline"&gt;/&lt;div type="dateline"&gt;/g;
  
      $tmp =~ s/&lt;pol&gt;([^&lt;]+)&lt;\/pol&gt;/&lt;term type="political"&gt;$1&lt;\/term&gt;/g;
      $tmp =~ s/&lt;term type="political"&gt; ([^&lt;]+)&lt;\/name&gt;/ &lt;term type="political"&gt;$1&lt;\/term&gt;/g;
      $tmp =~ s/&lt;a&gt;&lt;name/&lt;name/g;
  
      $tmp =~ s/&lt;t&gt;([^&lt;]+)&lt;\/t&gt;/&lt;name type="toponym"&gt;$1&lt;\/name&gt;/g;
  
      $tmp =~ s/&lt;z&gt;([^&lt;]+)&lt;\/z&gt;/&lt;name type="zoonym"&gt;$1&lt;\/name&gt;/g;         
      $tmp =~ s/&lt;h&gt;([^&lt;]+)&lt;\/h&gt;/&lt;name type="hydronym"&gt;$1&lt;\/name&gt;/g;           
      $tmp =~ s/&lt;e&gt;([^&lt;]+)&lt;\/e&gt;/&lt;name type="ethnonym"&gt;$1&lt;\/name&gt;/g;           
  
      #$tmp =~ s/&lt;a&gt;([^&lt;]+)/&lt;name type="anthroponym"&gt;$1&lt;\/name&gt;/g;
      #$tmp =~ s/([^&gt;]+)&lt;\/a&gt;/&lt;name type="anthroponym"&gt;$1&lt;\/name&gt;/g;
  
      $tmp =~ s/&lt;u&gt;([^&lt;]+)&lt;?\/u&gt;/&lt;name type="unknown"&gt;$1&lt;\/name&gt;/g;
  
      $tmp =~ s/\s+([\.:,!\?\)])/$1/g;
      $tmp =~ s/(\()\s+/$1/g;
  
      $tmp=~s/&lt;p&gt;#&lt;\/p&gt;//g;
      $tmp=~s/&lt;div&gt;&lt;\/div&gt;//g;
  
      $tmp=~s/\.\s+\./\.\./g;
      $tmp=~s/\.\.(?!&lt;\.)/\.\.\./g;
      $tmp=~s/\.\.\./…/g;
      $tmp=~s/…\s*\./…/g; 
      $tmp=~s/ +([,\.…;:!\?])/$1/g;
      #$tmp=~s/([,\.…;:!\?])(?!&lt; )/$1 /g;
      $tmp=~s/-/–/g;
      $tmp=~s/,–/, –/g;
      $tmp=~s/([\.:,!\?\)])–/$1 -/g;
      $tmp=~s/\. &lt;/\.&lt;/g;
  
      $tmp=~s/xml: id/xml:id/g;
  
      $tmp=~s/#-{2,}//g;
      $tmp=~s/&lt;p&gt;&lt;\/p&gt;//g;
      $tmp=~s/\s*&lt;\/p&gt;&lt;p&gt;/&lt;\/p&gt;\n\t\t\t\t&lt;p&gt;/g;
      $tmp=~s/ +/ /g;
  
      #$tmp =~ s/„([^„“]+)„/„$1“/g;
  
      #$tmp=~s/&lt;pb n="(\d+)"\/&gt;(?:\r\n)*n*\s*&lt;\/div&gt;/&lt;pb n="$1"\/&gt;/gm;
      #$tmp=~s/&lt;div type="Section"&gt;(?:\r\n)*\n*\s*&lt;head&gt;([^&lt;]+)&lt;\/head&gt;/&lt;div type="Section"&gt;\n\t\t\t\t&lt;head&gt;$1&lt;\/head&gt;\n\t\t\t\t&lt;\/div&gt;/gm;
      #$tmp=~s/\s*&lt;pb n="(\d+)"\/&gt;(?:\r\n)*\n*\s*&lt;div type="Section"&gt;/&lt;div type="Section"&gt;\n\t\t\t\t&lt;pb n="$1"\/&gt;/gm;
  
      $tmp=~s/&lt;\/p&gt;&lt;lg&gt;/&lt;\/p&gt;\n\t\t\t\t&lt;lg&gt;/g;
      $tmp=~s/&lt;\/p&gt;&lt;\/div&gt;/&lt;\/p&gt;\n\t\t\t\&lt;\/div&gt;/g;
  
      $tmp=~s/(&lt;name[^&gt;]*&gt;) +/ $1/g;
      $tmp=~s/([^აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ]*) +&lt;\/name&gt;/&lt;\/name&gt;$1 /g;
      $tmp=~s/…\s*&lt;\/name&gt;/&lt;\/name&gt;…/g;
      $tmp=~s/,\s*\./\./g;
      $tmp=~s/ +/ /g;
  
      $tmp=~s/NORMALERZBNORMALERZB(\d+)NORMALERZBNORMALERZB/\n\t\t\t\t\t&lt;lb n="$1"\/&gt; /g;
      $tmp=~s/TRENNENDERZBTRENNENDERZB(\d+)TRENNENDERZBTRENNENDERZB/&lt;lb n="$1"\/&gt;/g;  
      $tmp=~s/PAGE PAGE PAGE PAGE PAGE(\d+)PAGE PAGE PAGE PAGE PAGE/&lt;pb n="$1"\/&gt;/g;  #//&lt;pb n=\"".$current_page."\"/&gt;";  
      $tmp=~s/&lt;\/p&gt;(&lt;pb n="\d+"\/&gt;)/&lt;\/p&gt;\n\t\t\t\t$1/g;
      $tmp=~s/ (&lt;pb n="\d+"\/&gt;)/$1/g;
      $tmp=~s/&lt;\/p&gt;[\r\n]+\s+&lt;p&gt;(&lt;pb n="\d+"\/&gt;)&lt;\/p&gt;/$1&lt;\/p&gt;/g;
  
      $tmp=~s/&lt;\/l&gt;(&lt;pb n="\d+"\/&gt;)/$1&lt;\/l&gt;/g;
  
      $tmp=~s/ +/ /g;
  
      $tmp=~s/&lt;a&gt;&lt;name/&lt;name/g;
  
      $tmp=~s/&lt;head&gt;&lt;\/head&gt;//;
  
      my $sperr="";   
      if ($_[1]!~m/(?:04|07|11).1857/ &amp;&amp; $_[1]!~m/(?:04|08).1858/)
      {
          while ($tmp=~m/(?&lt;![აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ])((?:[აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ][^აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ~\–\-]){3,})/)
          {
              my $sperr_org=$1;
              my $sperr_edit=$1;
              my $rest="";
              $sperr_edit=~s/ //g;
              $sperr.=$sperr_edit."\n";
              if ($sperr_edit=~m/([^აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ]+)$/)
              {
                  $rest=($1 ne "&lt;"?" ":"").$1;
                  $sperr_edit=~s/[^აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ]+$//;
              }       
              else { $rest=" "; }
              $tmp=~s/(?&lt;![აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ])\Q$sperr_org\E/&lt;hi rend="letter-spacing"&gt;$sperr_edit&lt;\/hi&gt;$rest/;
          }   
          if ($sperr ne "")
          {
              open (OUT,"&gt;&gt;:encoding(utf-8)", $_[2]."\\000_sperrschrift.txt");
              print OUT $_[1]."\n\t".$sperr;
              close OUT;
          }
      }
  
      $tmp=~s/&gt;([^&lt;])&lt;\/name&gt;\./&gt;$1\.&lt;\/name&gt;/g;
  
      return $tmp;
  }
  
  sub go_go_gadget
  {
      my $file_xml=$_[0];
      my $file_html='';   my $output='';  my $output_filename=''; 
      my $chapter=0;  my $div=0; my $p=0; my $last_p=0;  my $v=0;
      my $input_xml=''; my $chapter_type=''; my $written=0;
      my $page=0; my $started=0;
  
      (my $fn,my $pn)=fileparse $file_xml;
      return if ($fn=~m/instruqcia/);
  
      print "\tkonvertiere $fn\n";
      $fn=~s/\.txt//g;
      $fn=~s/(\d+)_/$1+/;
      $fn=~s/_/-/g;   $fn=~s/^([a-z]+)-/$1_/g;
      $sub_folder="";
      if ($fn=~m/^([^_]+_[a-zA-Z]+)/)
      {   
          $sub_folder=$1;     
      }
  
      $file_xml=~s/(?:\/|\\+)/\\/g;
  
      open (IN,"&lt;:encoding(utf-8)", $file_xml) || die "konnte die datei nicht oeffnen: $!\n"; 
      $input_xml = do { local $/; &lt;IN&gt; } ;                        # Eingabedatei komplett in String einlesen
      close IN;
  
      # -----------------------------------------
      $input_xml=anfangs_verarbeitung($input_xml);
      # ----------------------------------------- 
  
      $div=0;
      my $last_line='';   
      my @lines=split(/\n/,$input_xml);   
      $line_cnt=0;    
      my $group_cnt=0;
      my $verse_cnt=0;
      my $pg='';
      my $first_page=0;
      my $last_page=0;
      my $has_chapters=0;
      my $ut=0;
      my $quote_open=0;
      my $section_cnt=0;
      my $chapter_cnt=0;
  
      $coded_lb=0;
      $chapter_type="Section";
      $has_chapters=1 if ($input_xml=~m/#\s*[cC]\s*[\dIVXMC]+[\–\-]?[\dIVXMC]*/);
      if ($has_chapters eq 0)
      {
          $output='&lt;div type="Section"&gt;' ;
          #$chapter=1; 
      }
  
      $output='&lt;div type="Content" n="1"&gt;'."\n";
      foreach my $line (@lines)
      {   
          $line=~s/^L\s*//;
  
          chomp($line); $line=~s/\n//g; $line=~s/\r//g;   $line=~s/(\s){2,}/$1/g; $line =~ s/^\s+//g; $line =~ s/\s+$//g;
          #$line=~s/([აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ])([,;\.])([აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ])/$1$2 $3/g;
  
          if ($has_chapters eq 1 &amp;&amp; $line =~ m/^\s*#?\s*\|?[cC]\s*(\d+)\s*(.*)/)      # chapter
          {
              $output.="&lt;/note&gt;" if ($ut == 1);
              if ($p eq 1) { $output.="&lt;/p&gt;";   } 
              elsif ($v eq 1) { $output.="\n\t\t\t\t&lt;/lg&gt;"; }
              if ($started eq 1)
              {
                  if ($div eq 1) { $output.="\n\t\t\t&lt;/div&gt;"; }
                  elsif ($chapter eq 1) { $output.="\n\t\t\t&lt;/div&gt;"; }
              }
  
              my $title=$2;
              if ($title) { $title=~s/&lt;ref target="#a(\d+)" type="noteAnchor"&gt;/&lt;ref target="#a_$page\_$1" type="noteAnchor"&gt;/g; }
              $chapter_cnt=$1;
              $output.="\n\t\t\t".'&lt;div type="Chapter" n="'.$chapter_cnt.'"&gt;'."\n\t\t\t\t&lt;head&gt;".($title?check_linebreak($title,$pg):$pg)."&lt;/head&gt;";
  
              $chapter=1; $chapter_type="Chapter"; #$div=0;
              $p=0;  $written=0;  $v=0; $ut=0;
              $last_line=""; $started=0;
              $pg='';
          }
          elsif ($line =~ m/^\s*#\s*[pP]\s*(\d+)/)                # page break
          {
              if ($v ne 1 &amp;&amp; $p eq 0) 
              { 
                  if ($div == 0)
                  {
                      if ($chapter_type eq "Section" || $has_chapters == 0) { $section_cnt++; $output.='&lt;div type="Section" n="'.$section_cnt.'"&gt;'; }
                      else { $output.='&lt;div type="Chapter" n="'.$chapter_cnt.'"&gt;'; }
                      $div=1;
                  }
                  $output.="&lt;p&gt;"; $p=1;  
              }
              #$output.="&lt;pb n=\"".$1."\"/&gt;"; 
  
              # --- detecting page errors
              my $current_page=$1; #0;            
              #if ($first_page &gt; 0)
              #{
              #   $current_page=$1;
              #   if ($current_page-$last_page&lt;1)
              #   {
              #       $current_page=$last_page+1;
              #       $page_errors.=$fn."\t".$last_page."\n";
              #   }               
              #   elsif ($current_page-$last_page&gt;1)
              #   {
              #       $page_errors.=$fn."\t".$last_page."\n";
              #   }               
              #}
              #else
              #{
              #   $first_page=$1;
              #   $current_page=$1;
              #}  
              #$last_page=$current_page;
              # ----
  
              $pg.="PAGE PAGE PAGE PAGE PAGE".$current_page."PAGE PAGE PAGE PAGE PAGE";   
              #$p=0;
              $page=$1;
              #$written=0; 
              $last_line="";
              $line_cnt=0;
          }
          elsif ($line =~ m/\s*#[tT]\s*(.+)/)             # title
          {
              $output.="&lt;/note&gt;" if ($ut == 1);
              if ($p eq 1) { $output.="&lt;/p&gt;"; }
              elsif ($v eq 1) { $output.="\n\t\t\t\t&lt;/lg&gt;"; }
  
              if (($chapter eq 1 || $div eq 1) &amp;&amp; $chapter_type ne 'chapter')
              {
              #   if($chapter_type eq 'chapter') 
              #   {
              #       if ($started eq 1)
              #       {   $output.="\n\t\t\t&lt;/div&gt;\n\t\t\t".'&lt;div type="Chapter" n="'.$1.'"&gt;';}
              #       else { $output.='&lt;div type="Chapter" n="'.$1.'"&gt;';}
              #   }
              #   else
              #   { 
                      if ($started eq 1) { $section_cnt++; $output.="\n\t\t\t&lt;/div&gt;\n\t\t\t".'&lt;div type="Section" n="'.$section_cnt.'"&gt;'; }       
                      else { $section_cnt++; $output.="\n\t\t\t".'&lt;div type="Section" n="'.$section_cnt.'"&gt;'; }
              #   }               
              }
              else
              {
                  #$section_cnt++;
                  #$output.='&lt;div type="Section" n="'.$section_cnt.'"&gt;';
                  #$div=1;
              }
              #$line_cnt++;
              $output.="\n\t\t\t\t&lt;head&gt;".$pg.$1."&lt;/head&gt;"; 
              $pg='';
              $p=0; $written=0; $v=0; $ut=0;
              $last_line=''; $started=1;
          }
          elsif ($line =~ m/#v\s*(.+)/)                           # verse
          {
              $output.="&lt;/note&gt;" if ($ut == 1);
              if ($p eq 1) { $output.="&lt;/p&gt;" ;}
              if ($v eq 0) { $group_cnt++; $verse_cnt=0; $output.="\n\t\t\t\t".'&lt;lg n="'.$group_cnt.'"&gt;';  }
              $verse_cnt++;
              $last_line=$1;
              $line_cnt++;            
              $output.="\n\t\t\t\t\t".'&lt;l n="'.$verse_cnt.'"&gt;'.$pg."NORMALERZBNORMALERZB".$line_cnt."NORMALERZBNORMALERZB".$1."&lt;/l&gt;";         
              $p=0; 
              $written=1; $v=1;   
              $started=1; $pg=''; $ut=0;
          }
          elsif ($line=~m/#\s*\-{2,}/) #elsif ($line eq '' &amp;&amp; $last_line ne '') # &amp;&amp; $last_line!~m/[\.!\?]\s*$/)          # paragraph
          {           
              if ($written eq 1)
              {
                  $output.="&lt;/note&gt;" if ($ut == 1); 
                  if ($p eq 1) { $output.="&lt;/p&gt;"; $p=0;  }
                  elsif ($v eq 1 &amp;&amp; $written eq 0) { $output.="\n\t\t\t&lt;/lg&gt;\n\t\t\t\t"; $v=0; }
              }               
              #if ($p eq 0 &amp;&amp; $v eq 0) { $output.="\n\t\t\t\t&lt;p&gt;"; $p=1; }
  
              $written=0; $last_line=''; $ut=0;
          }
          elsif ($line =~ m/^(?:#d)?\s*(\d{4}\s*წ\.)$/ || $line=~m/^\s*(\d{4}(?: – \d+\s*წ*\.)?\s*)$/ || $line=~m/^\s*([0-9]+\s*[აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ]+\s*[0-9]+\s*[აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ]*)$/)          # dateline
          {
              if ($div eq 1 || $chapter eq 1)
              { 
                  $output.="&lt;/note&gt;" if ($ut == 1);
                  if ($p eq 1) { $output.="&lt;/p&gt;"; }
                  elsif ($v eq 1) { $output.="\n\t\t\t\t&lt;/lg&gt;"; }
                  $output.="\n\t\t\t&lt;/div&gt;"; 
                  $chapter=0; $div=0; $ut=0;
              }
              $line_cnt++;
              $output.="\n\t\t\t\t&lt;div type=\"dateline\"&gt;&lt;p&gt;".$pg."NORMALERZBNORMALERZB".$line_cnt."NORMALERZBNORMALERZB".$1."&lt;/p&gt;&lt;/div&gt;"; 
              $p=0;  $written=0; $v=0; $pg=''; 
              $last_line="";
          }
          elsif ($div eq 1 || $chapter eq 1 || ($div eq 0 &amp;&amp; $chapter eq 0))
          {
              if ($line!~m/^\s*$/)
              {
                  $output.="\n\t\t\t\t&lt;/lg&gt;" if ($v eq 1);
                  if ($div eq 0 &amp;&amp; $chapter eq 0) { $div=1; $section_cnt++; $output.="\n\t\t\t".'&lt;div type="Section" n="'.$section_cnt.'"&gt;';}
                  if ($p eq 0) { $output.="\n\t\t\t\t&lt;p&gt;";  }
                  $line=~s/\s*#\s*//g;
  
                  # --- quotes
                  $line=~s/([\.,;\?!:])„/$1“/g;       #„ “
                  if ($line=~m/^\s*„/ &amp;&amp; $line!~m/“/ &amp;&amp; $line=~m/[\.\?!:]+\s*$/)
                  {   
                      $line.="“"; 
                  }               
                  else 
                  {
                      $line=~s/„//g;
                  }
                  $line=~s/“//g if ($line=~m/“/ &amp;&amp; $line!~m/„/);
                  # ---
  
                  # --- ref
                  $line=~s/&lt;ref target="a(\d+)" type="noteAnchor"&gt;/&lt;ref target="#a_$page\_$1" type="noteAnchor"&gt;/g;
                  $line=~s/&lt;note xml:id="a(\d+)" type="footnote"&gt;/&lt;note xml:id="a_$page\_$1" type="footnote"&gt;/g;          
                  # ---
  
                  if ($line=~m/\|ut/)
                  {
                      $line=~s/\|ut/&lt;note type="comment"&gt;/;
                      $ut=1;
                  }
  
                  $output.=check_linebreak($line,$pg);
                  #$line_cnt++;
                  #if ($line=~m/([აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ\-]+)\/([აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰჱჲჳჴჵჶ\-]+[[:punct:]]*)/)
                  #{                  
                  #   my $tmp1=$1; my $tmp2=$2;
                  #   my $z="TRENNENDERZBTRENNENDERZB".($line_cnt+1)."TRENNENDERZBTRENNENDERZB";
                  #   $line=~s/\Q$tmp1\E\/\Q$tmp2\E/$tmp1$z$tmp2/; $line.=" ";
                  #   if ($coded_lb eq 0)
                  #   {
                  #       $output.=$pg."NORMALERZBNORMALERZB".$line_cnt."NORMALERZBNORMALERZB ".$line;
                  #   }
                  #   else
                  #   {
                  #       $output.=$pg.$line; 
                  #   }
                  #   $coded_lb=1;
                  #}
                  #else
                  #{
                  #   if ($coded_lb eq 0)
                  #   {
                  #       $output.=$pg."NORMALERZBNORMALERZB".$line_cnt."NORMALERZBNORMALERZB ".$line;
                  #   }
                  #   else
                  #   {
                  #       $output.=$pg.$line;                     
                  #   }
                  #   $coded_lb=0;
                  #}              
                  #$output.=$pg." "."NORMALERZBNORMALERZB".$line_cnt."NORMALERZBNORMALERZB".$line;
                  $last_line=$line;
                  $p=1; $written=1; $v=0; $started=1;$pg='';
              }
          }
      }
  
      if ($p eq 1) { $output.="&lt;/p&gt;";}
      elsif ($v eq 1) { $output.="\n\t\t\t\t&lt;/lg&gt;"; }
      if ($div eq 1) { $output.="\n\t\t\t&lt;/div&gt;"; }
      elsif ($chapter eq 1) { $output.="\n\t\t\t&lt;/div&gt;"; }
  
      # -----------------------------------------
      $output=end_verarbeitung($output,$fn,$folder_out)."&lt;/div&gt;";
      # -----------------------------------------
  
      #$fn=get_complete_filename($fn);    
      mkdir($folder_out."\\".$sub_folder,0777) if ($dev eq 0 &amp;&amp; !(-d $folder_out."\\".$sub_folder));  
      my $txt='&lt;text rend="Section" xml:lang="kat"&gt;';
      $txt='&lt;text rend="'.($section_cnt?"Section ":"").'Chapter" xml:lang="kat"&gt;' if ($has_chapters eq 1);
      $output=header($fn,$folder_in,$folder_out)."\n\t".$txt.'    
          &lt;body&gt;
              '.$output.' 
          &lt;/body&gt;
      &lt;/text&gt;';
  
      $output=~s/(&lt;body&gt;(?:\r\n)*\s*&lt;pb n="\d+"\/&gt;)(?:\r\n)*\s*&lt;\/div&gt;/$1/g;
  
      $output_filename=$folder_out.($dev eq 0 ? "\\".$sub_folder : "")."\\".$fn.".xml";           
      open (OUT, "&gt;:encoding(utf-8)", $output_filename);
      print OUT '&lt;?xml version="1.0" encoding="utf-8"?&gt;'."\n".'&lt;!DOCTYPE TEI [
      &lt;!ENTITY header SYSTEM "'.$fn.'.teih"&gt;
      &lt;!ENTITY text SYSTEM "'.$fn.'.txml"&gt;
  ]&gt;
  &lt;TEI xmlns="http://www.tei-c.org/ns/1.0" xmlns:gnc="http://iness.uib.no/ns/1.0"&gt;
      &amp;header;
      &amp;text;
  &lt;/TEI&gt;';
      close (OUT);
      $output_filename=~s/\.xml/\.txml/gi;
      open (OUT, "&gt;:encoding(utf-8)",$output_filename) || die "konnte die ausgabedatei \"$output_filename\" nicht oeffnen: $!\n";
      print OUT $output;
      close OUT;          
  }
  
  sub main
  {
      print "\nBeginne...\n";
  
      my $root="D:\\bla";
      my @startfolders=($root."\\");
      $folder_out="D:\\bla";
      foreach my $startfolder(@startfolders)
      {
          @folders=();
          get_files($startfolder);
          if (scalar(@folders)&lt;1) { push(@folders,$startfolder); }
          $root=~s/0_Eingabe/1_Ausgabe\\1/;
          foreach $folder_in(@folders)
          {
              $page_errors='';
              $folder_out=$folder_in;
              $folder_out=~s/0_Eingabe/1_Ausgabe/;
              # creating subfolders too
              #my $tmp=$folder_out;
              #$tmp=~s/\Q$root\E//;           
              #my @arr_tmp=split("\\\\",$tmp);
              #$tmp="";
              #foreach my $dings (@arr_tmp)
              #{              
              #   next if ($dings eq '');
              #   $tmp.="\\".$dings;                              
              #   mkdir($root.$tmp,0777) if (!(-d $root.$tmp));
              #}#
              # ----- 
  
              $folder_out=~s/\\+/\\/g;
              $dev = 1;   # entwicklermodus an bei 1
              $folder_out=~s/1_Ausgabe.*/1_Ausgabe/ if ($dev eq 1);
  
              print "Ordner ".$folder_in."\n";
              foreach my $file_xml(&lt;${folder_in}/*.txt&gt;)
              {
                  go_go_gadget($file_xml);
              }
  
              next;
  
              if ($page_errors ne '')
              {
                  $folder_in=~m/0_Eingabe\\(.+)/;
                  my $tmp=$1;
                  $tmp=~s/\\+/__/g;           
                  open (OUT, "&gt;:encoding(utf-8)", $root."\\".$tmp.".txt") || die "\n\tPage errors to file ".$tmp.": ".$!."\n";
                  print OUT $page_errors;
                  close OUT;
              }
          }
      }
  
      print "Fertig!\n\n";
  }
  
  main();
  </code></pre>
  
  <p>However some files are taking too long to process.
  I would like to skip either of the steps if they take more than 6 seconds. 
  In such way that if the handled file takes too long to convert, it skips to the next one.
  Any suggestions on how I can do this via timeout? </p>