I've been known to reinvent wheels from time to time, but this particular wheel has been around the block a few times...
CPAN. Specifically Bio::Matrix::PSM::IO::masta. Per the docs, this module will
convert a set of aligned sequences:to a PFM (SiteMatrix object)ACATGCAT ACAGGGAT ACAGGCAT ACCGGCAT
Given the number of PFM-related modules within Bioperl I would imagine anything you want to do is already written.
Unfortunately I don't have time to provide an example. However, I would be very interested to see what you come up with if you go this route. Please post your solution when you are done so others can learn from your experience.
<code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>