Separator hold mode in perl split ()

split /PATTERN/,EXPR

In the book I read the following:

When you use a template in split , be sure to avoid the memory brackets in the template, since this is the trigger hold mode of the trigger.

I cannot find documentation that explains this in detail. Can someone explain the storage mode of Seperator and its possible use for a short while?

+5
source share
2 answers

This is confirmed in perldoc -f splitto the end (the comment in the code is my own):

PATTERN , , , ( , , _); - , undef . , , , (.. , ), count LIMIT. , ( ):

split(/-|,/, "1-10,20", 3)       # ('1', '10', '20')
                                 # No retention, '-', ',' consumed

split(/(-|,)/, "1-10,20", 3)     # ('1', '-', '10', ',', '20')
                                 # Split on and retain '-' or ','
                                 # 5 elements returned

split(/-|(,)/, "1-10,20", 3)     # ('1', undef, '10', ',', '20')
                                 # undef because '-' matches

split(/(-)|,/, "1-10,20", 3)     # ('1', '-', '10', undef, '20')
                                 # undef because ',' matches

split(/(-)|(,)/, "1-10,20", 3)   # ('1', '-', undef, '10', undef, ',', '20')
                                 # one match per capturing group. (-) matches -, but
                                 # (,) returns undef on trying to match -.
                                 # 7 elements (!)

, , :

  • undef , , - PATTERN

  • , LIMIT $n, $n

+7

, , , split.

+1

All Articles