use 5.014; $/ = \8192; while (<>) { # like so state $buf .= $_ . ' 'x eof; $buf =~ s{ \s* (\S+) \s }{ process($1), "" }xge; } while (<>) { # ..or so state $buf .= $_ . ' 'x eof; $buf = pop( my $tok = [split ' ', $buf, -1] ); process(@$tok); } sub process { say for @_ }