How to change atoms?

Hi,

I am really new using Perl language.
Does anybody know how can I change a determined number of atoms in a molecule?

For example:

I have a benzene and I want to change half of the hydrogens into nitrogens.

Thank you very much