#!/usr/bin/perl use warnings; my $text=shift; my($wc, $last)=(0, ' '); for my $x(split //, $text) { ($wc, $last)=(($x ne ' ' and $last eq ' ')? $wc+1: $wc, $x); } print "$wc\n";