#!usr/bin/perl
use strict;
use warnings;
my @var= ('now is the time', #space, tab between "now" and "is"
' for all good men', #leading tab, no space
'to come to the aid of their party.' #space, tab before party
);
#hbm's method:
my $count = 0;
my $linecount = 0;
for my $var(@var) {
$linecount = $var =~ tr/ \t/ \t/;
print "\$linecount: $linecount\n";
$count += $linecount;
}
print "$count \n";
#hbm's method with \t (tr doesn't know from "\s"
my $count_s = 0;
my $linecount_s = 0;
for my $var(@var) {
$linecount_s = $var =~ tr/\t/|\t/;
print "\$linecount_s: $linecount_s\n";
$count_s += $linecount_s;
}
print "$count_s \n";
=head OUTPUT
$linecount: 4
$linecount: 4
$linecount: 8
16 # WTF? with tabs converted to spaces, I count 17 as I have my tabs set.
$linecount_s: 1
$linecount_s: 1
$linecount_s: 1
3
=cut
####
# and now using \s & /g # output smells bad
print "and now using \\s only, with /g modifier\n";
my $count_s = 0;
my $linecount_s = 0;
for my $var(@var) {
$linecount_s = scalar ( $var =~ s/\s/_/g );
print "\$linecount_s: $linecount_s, \$var after substitution: $var\n";
$count_s += $linecount_s;
}
print "\$count_s: $count_s \n\n";
####
and now using \s only, with /g modifier
$linecount_s: 3, $var after substitution: now_|is_the_time
$linecount_s: 3, $var after substitution: |for_all_good_men
$linecount_s: 7, $var after substitution: to_come_to_the_aid_of_their_|party.
$count_s: 13