use constant RE_MAGIC => bless [], 'RE_MAGIC'; use constant C_STYLE => bless [], 'C_STYLE'; { my ($re,$str); sub strtok { my ($s,$pat,$opt) = @_; if (defined $s) { local $^W; # in case $pat/$opt isn't a ref $re = ref($pat) eq "RE_MAGIC" ? qr/(?!\s*$)\s*(\S*)/ : $opt && ref($opt) eq "C_STYLE" ? qr/(?!(?:$pat)*$)((?:(?!$pat).)*)(?:(?:$pat)+|$)/ : qr/(?!(?:$pat)*$)((?:(?!$pat).)*)(?:(?:$pat)|$)/; $str = $s; } else { croak "no pattern for strtok()" if not defined $re; } $str =~ /\G$re/g and return $1; undef $re; undef $str; } } my $rec = strtok($source, ';'); while (defined $rec) { # ... $rec = strtok(); }