open (my $handle, '<', $file) or die "Can't read '$file': $!"; my $contents = do { local $/; <$file> }; #### my $pos = 0; while (<$handle>){ my $line_len = length $_; # do that before chomping chomp; while (m/(\w+)/g){ my $word = $1; my $word_pos = $pos + pos; } $pos += $line_len; }