use File::Slurp; my $constant = '$_'; $_ = read_file('filename.pl'); $count = () = /\Q$constant\E/g; print "$constant occurs $count times\n";