Powerful perl paradigm I don't find in python

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Fri Jan 15 05:42:24 EST 2016


On 15.01.2016 10:43, Peter Otten wrote:
> Charles T. Smith wrote:
>
>> while ($str != $tail) {
>>      $str ~= s/^(head-pattern)//;
>>      use ($1);
>> }
>
> For those whose Perl's a little rusty: what does this do?
> A self-contained example might also be useful...
>

Right, an explanation would certainly get you a lot more responses.

If I'm guessing correctly what the snippet is supposed to do (and, yes, 
my Perl definitely is rusty), isn't the Python equivalent of the regex 
part of your question fairly obvious if you're using the re module:

things = []
while some_str != tail:
     m = re.match(pattern_str, some_str)
     things.append(some_str[:m.end()])
     some_str = some_str[m.end():]

# do something with things

I have no idea why you'd want to *import* all the things parsed out of 
some_str, but for this part you may look at importlib.import_module.

P.S.: the while loop above never ends if tail is not in some_str, but I 
guess your Perl snippet has the same problem?





More information about the Python-list mailing list