&split の動作


split /PATTERN/,EXPR の PATTERN の違いによる微妙な違いがよく分からなくて混乱してきたのでメモ。後でまた必要になったら読み返す。

% perl -de 0
DB<22> print join(' :', split(' ', '    hi   there'))
hi :there

DB<23> print join(' :', split(/\s+/, '    hi   there'))
 :hi :there

DB<24> print join(' :', split(/ /, '    hi   there'))
 : : : :hi : : :there

DB<25> print join(' :', split(//, '    hi   there'))
  :  :  :  :h :i :  :  :  :t :h :e :r :e

perldoc -f split より引用

A "split" on "/\s+/" is like a "split(’ ’)" except that any leading whitespace produces a null first field. A "split" with no arguments really does a "split(’ ’, $_)" internally.