Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hello!

I'm still practise my skill in Perl. I walk slowly, but it's very nice travel by Camel's track.
So, I need some advices about style code.

Look please at this:
To try OO programing in Perl I've made module to calculate how many bills and coins I need to pay for example $133 = (100+20+10+2+1).
It's propably quite helpful for employers.
He can't get 5x$100 from bank for 6 workers ($135 + $80 + $66 + $150 + $69= $500),
but he can do this:
#!/usr/bin/perl use warnings; use strict; use payment; my @my_bills = qw (200 100 50 20 10 5 2 1); my @my_payments = qw{135 80 66 150 69}; my $mp = payment->new(); $mp->change(@my_bills); foreach (@my_payments) { $mp->add($_); } my %result = $mp->get(); foreach ( sort { $b <=> $a } keys(%result) ) { print "$_ \t: $result{$_}\n"; }
and this is code of payment.pm
package payment; use warnings; use strict; sub new { my $class = shift; my $self = {}; $self->{units} = undef; $self->{bills} = undef; bless $self, $class; } sub change { my $self = shift; @{ $self->{units} } = @_; %{ $self->{bills} } = map { $_ => 0 } @_; } sub add { my $self = shift; my @units = @{ $self->{units} }; my %pieces = %{ $self->{bills} }; my $temp = shift; my $unit; my $i = 0; while ($temp) { $unit = $units[$i]; while ( $temp >= $unit ) { $temp -= $unit; $pieces{$unit} += 1; } last if $i == (@units); $unit = $units[ ++$i ]; } %{ $self->{bills} } = %pieces; } sub get { my $self = shift; return %{ $self->{bills}}; } 1;
So, what do you thing about this? My style is good? Have you any advices for me? If it's good, I'm going to add some features into my class. ;-)

Thanks for your's advices and time.
Regards
Uksza

In reply to Payment changer by uksza

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (13)
    As of 2014-09-19 15:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (142 votes), past polls