how to calculate coverage of each base of mapped reads?
You are looking for bedtools genomecov, specifically -d option. See https://bedtools.readthedocs.io/en/latest/content/tools/genomecov.html , for example:

bedtools genomecov -i A.bed -g my.genome -d




I recommend not reinventing the wheel: use bedtools instead of reimplementing the same functionality in Perl.