Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Pearls (not really) of Perl programming

by revdiablo (Prior)
on Nov 24, 2004 at 18:35 UTC ( [id://410206]=note: print w/replies, xml ) Need Help??


in reply to Pearls (not really) of Perl programming

Everyone loves to post bad examples from other peoples' code. But I will [once again] bravely delve into my own code archives for terrible examples!

This is at the beginning of a script that I must have written shortly after I learned about use strict. I didn't really understand lexical scope, I just wanted the errors to go away. Can we say "missing the point"?

my($all,$com,$csv,$ctr,$desc,$dir,$fname,$found,$fsize, $line,$longline,$null,$one); my($parm,$parms,$pattern,$seekrc,$value); my(@coms,@csvin,@grep,@parms,@seekrc);

Here's another fun one. I can think of one or two easier ways to do this nowadays....

($start,$stop,$adv,$desc) = split(/\,/,$line); $start =~ s/^\s+//g; $stop =~ s/^\s+//g; $adv =~ s/^\s+//g; $desc =~ s/^\s+//g;

Or how about something like this. I guess I didn't yet understand how to access nested hashes, because the use of %temphash is just bizarre (trimmed for pasting):

while(defined($line = <IN>)) { ... $temphash{'size'} = $size; $temphash{'crc'} = $crc; if($dir eq "" || $csvdirs == 1) { ... $temphash{'dir'} = $good; } elsif($csvdirs == 2) { ... $temphash{'dir'} = $good . "/" . $dir; } else { $temphash{'dir'} = $dir; } $bighash{$name} = { %temphash }; }

I could go on all day, to my own humiliation. I'll leave it at that though. Perhaps others will now be tempted to post their own bad code, rather than someone else's. :-)

Replies are listed 'Best First'.
Re^2: Pearls (not really) of Perl programming
by hardburn (Abbot) on Nov 24, 2004 at 18:45 UTC

    This is at the beginning of a script that I must have written shortly after I learned about use strict.

    I think everyone goes through a phase like that. I certainly have code with "global lexicals" from my early use strict days.

    $start =~ s/^\s+//g;

    Is there a reason for the /g?

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

      Is there a reason for the /g?

      Since the code is several years old (perhaps 5 or 6), I don't remember exactly. But the most likely explanation is that I used to be a pretty bad cargo-cult programmer.

      An obvious reason. Habit. I'm always typing ~s/.../.../g out of habit, never thinking of doing a replace only once.

      ----
      Then B.I. said, "Hov' remind yourself nobody built like you, you designed yourself"

Re^2: Pearls (not really) of Perl programming
by Nkuvu (Priest) on Nov 25, 2004 at 14:04 UTC
    This is at the beginning of a script that I must have written shortly after I learned about use strict. I didn't really understand lexical scope, I just wanted the errors to go away. Can we say "missing the point"?
    I'm still in the bad habit of doing this. I blame C for the habit.
      It's funny that you mention C - whenever I had to deal with it, I would always create additional blocks inside a func just to limit variable scope.
      int a() { { int a, b; ... } /* move on */ { int a, b, c; } }
      That did result in some excessive indentation though.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-25 04:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found