Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: The joys of bad code

by TomDLux (Vicar)
on Nov 15, 2004 at 19:01 UTC ( #407904=note: print w/ replies, xml ) Need Help??

in reply to The joys of bad code

Came across some code I just had to share. How many steps is this programmer from good, idiomatic perl?

sub padfield { my ( $value, $fieldlength ) = @_; while ( length $value < $fieldlength ) { my $pad = '0'; $value = join( '', $pad, $value ); } return $value; }
  1. A creative use of join(). But why not just use simple string contcatenation: $value = $pad . $value ?
  2. You could use the string repetition operator $value = ("0" x ($fieldlength - length $value )) . $value.
  3. Or you could use take the right substring of an over-padded string.
  4. But even a C or Java programmer should know printf padded formats: return sprintf "%0$fieldlengthd", $value.


Comment on Re: The joys of bad code
Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://407904]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2015-11-26 15:54 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (701 votes), past polls