zarath has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Alternatives for index() ... substr() ?
by choroba (Cardinal) on Nov 23, 2017 at 16:01 UTC | |
Re: Alternatives for index() ... substr() ?
by roboticus (Chancellor) on Nov 23, 2017 at 16:20 UTC | |
Re: Alternatives for index() ... substr() ?
by Eily (Monsignor) on Nov 23, 2017 at 15:43 UTC | |
Re: Alternatives for index() ... substr() ?
by Laurent_R (Canon) on Nov 23, 2017 at 16:01 UTC | |
Re: Alternatives for index() ... substr() ?
by zarath (Beadle) on Nov 24, 2017 at 10:02 UTC | |
by Eily (Monsignor) on Nov 24, 2017 at 10:48 UTC | |
by hippo (Archbishop) on Nov 24, 2017 at 10:49 UTC | |
Re: Alternatives for index() ... substr() ?
by Anonymous Monk on Nov 24, 2017 at 04:37 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |