http://www.perlmonks.org?node_id=761559

Count chars from STDIN, takes 1 command line argument that lists all characters to be counted. RegExp does all of the work
use re 'eval';$#ARGV<0&&exit 1;$s.=$_ while<STDIN>;$s=~/(?{$z=quotemeta shift@ARGV})((??{"[$z]"})(?{$c++})|.)*(?{print$c})/m;

Replies are listed 'Best First'.
Re: Char Count
by jwkrahn (Abbot) on May 03, 2009 at 18:28 UTC

    You are using the /m option but you are not using either of the ^ or $ anchors.   It looks like you should be using the /s option so that . will match a newline as well as other characters.

Re: Char Count
by afoken (Chancellor) on May 05, 2009 at 08:55 UTC
    $s.=$_ while <STDIN>;$s=~s/.../.../;

    ... lacks slurp mode and introduces an unneeded variable $s:

    undef$/;$_=<STDIN>;s/.../.../;
    $#ARGV<0&&exit 1;

    Much typing to test if @ARGV has elements, and exit 1 can be shortened to die, if the additional error message does not disturb:

    @ARGV||die;
    perl -e'use re "eval";';$#ARGV<0&&exit 1;...'

    ... can be shortened on the perl command line:

    perl -Mre=eval -e '@ARGV||die;...'

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)