Hello Monks!

This question is all about keeping the written code as clean, clear and simple as possible.

Please consider the following piece of code (irrelevant parts are replaced by [...]):

[...] if ($text =~ /FATAL: Error while copying /) { my $dt = substr($text,0,20); my $me = index($text,' from '); my $ml = $me - 47; my $msg = substr($text,47,$ml); $me += 6; my $se = index($text,' to '); my $sl = $se - $me; my $source = substr($text,$me,$sl); my $sbs = $me + 2; my $sbl = $se - $sbs; my $sourcebis = q{\\\\ENCNRW0010}.substr($text,$sbs,$sbl); $se += 4; my $de = index($text,', error was:'); my $dl = $de - $se; my $destination = substr($text,$se,$dl); File::Copy::copy $source.$msg,$destination; push (@mail,$dt.$msg.' from '.$sourcebis.' to '.$destination." +\n"); $text =~ s/FATAL: Error while copying /FATAL (Fixed): Error wh +ile copying /g; print $text."\n"; } [...]

2 example lines of what $text could be:

2017-11-16 11:42:20 FATAL: Error while copying MX000017105279_2448299. +1523788.IN.EDI from D:\EnecoEDIELArchive\B2B_ELEK\ to \\ENCNRW0012\En +ecoData\EDIEL_IN\B2B_ELEK\, error was: 2017-11-16 11:42:21 FATAL: Error while copying MX000017105328_3626588. +1523787.IN.EDI from D:\EnecoEDIELArchive\B2B_ELEK\ to \\ENCNRW0012\En +ecoData\EDIEL_IN\B2B_ELEK\, error was:

So, with my code I need to extract several strings from $text. In case of the first example: 2017-11-16 11:42:20, MX000017105279_2448299.1523788.IN.EDI, D:\EnecoEDIELArchive\B2B_ELEK\ and \\ENCNRW0012\EnecoData\EDIEL_IN\B2B_ELEK\. Every other string found there is a constant by the way, which is why I can use those in my index().

Problem is these strings can be pretty much anything with any number of characters (apart from the datetime of course), the only way I know how to safely extract them is shown in the code snippet. I don't know about you but if I would see this in a code written by someone else, my head would start spinning.

The code works, so I would not classify this as a 'problem', but if for example next year something changes about the way the inputfiles are written, I will be having a hard time deciphering what I did here again. So I was just wondering if there is a clearer way of doing what I do here?

Maybe something like my $msg = "the string that comes between" 'copying ' "and" ' from ';? Is there a function that does what the double-quoted text says?

Thank you in advance!


In reply to Alternatives for index() ... substr() ? by zarath

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.