First, if you have a next statement that evaluates to True, there is "no next after that next"! Next in Perl is like "continue" in other languages.
This code doesn't make sense:
next if ($source =~ m/^(\s)*$/);
next if ($source =~ m/^(\s)*$/); #redundant!
There are 5 white space characters: \s,\n,\t,\r,\f.
So if you have a program that generates new pages,
just count the number of \f's!
There is obviously some point that I am missing here!
substr() is highly unlikely to produce usable results. Better probably is a REGEX that counts the number of pages or use \f for that.