in reply to Don't blindly recommend code

Couldn't resist posting some of the more amusing code samples from this particular guestbook.

Ignoring the "local", notice the typo in the tr///;

sub ucase { local($s1) = @_; $s1 =~ tr/[a-z]/[A-Z}/; return $s1; }
Here's an interesting attempt to check for a valid email address:
$chk =~ /(.*@.*\.[a-zA-Z]{2,3}$)/ && $chk !~ /(^\.)|(\.$)|( )|(\.\.)|( +@\.)|(\.@)|(@.*@)/
Mindboggling formatting (quick, where does the "if" end):
sub readem { local $insFile = shift; local $back = ""; if (open(FL, "<$insFile")) { while (<FL>) {$back .= $_;} close(FL); } else {&showErr('Fatal File Read Error');} return $back; }
Regarding the above code snippet, I won't post the &showErr sub because he has lines of HTML over 600 characters long!!!

Hmmm... I wonder how he parses form data (incidentally, he reproduces the following code in many different files. If he tries to fix any of the bugs, he's got many different places to fix it in):

@pairs = split(/&/, $query_string); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $name =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/~!/ ~!/g; $name =~ s/~!/ ~!/g; $value =~ s/<([^>]|\n)*>//g if $name ne "comtext" && $name ne +"ONld" && $name ne "edstext" && $name ne "mtxt"; $name =~ s/<!--#(.|\n)*-->//g; $value =~ s/<!--#(.|\n)*-->//g; $value =~ s/("|')//g if $name ne "comtext" && $name ne "ONld" +&& $name ne "edstext" && $name ne "mtxt"; $value =~ s/(`|\0|\\[^\\n])//g; $name =~ s/(`|\0|\\)//g; $FORM{$name} = $value; }
In the above snippet, he reproduced every error I pointed out in use CGI or die; and even came up with some new ones. I tried to find an email address for this person, but gave up -- though I would admit that I didn't try hard. Is it appropriate to email someone to tell them how many problems they have with their code?

Okay, I'll stop now. I think the point is made :)

Cheers,
Ovid

Update: Ooh, ooh, stop me before I kill again! Here's another beauty:

sub date_time { my($s1) = @_; my($mon,$year); ($mon,$year) = (gmtime($s1))[4,5]; $mon++; if ($year < 39) { $year = "20$year" } elsif ($year > 99 && $year < 2000) { $year = 2000 + ( $year - 100 +) } elsif ($year > 38) { $year = "19$year" } return ($mon,$year); }
Okay, so he's using "windowing" on the year. That's pretty bad, but here's how he calls it:
$datetime = &date_time(&date_time(0));
Code like this makes me not feel so bad about my Perl limitations :)

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: (Ovid) Re: Don't blindly recommend code
by davorg (Chancellor) on Mar 26, 2001 at 19:04 UTC
    Is it appropriate to email someone to tell them how many problems they have with their code?

    This is something I've been doing more and more recently. For example, the author of Date::MMDDYY has now removed his module from CPAN (tho' he said I wasn't the only person to write to him).

    I think that as long as you're polite and point out why what they are doing is wrong, then it's a very positive thing to do.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: (Ovid) Re: Don't blindly recommend code
by chromatic (Archbishop) on Mar 26, 2001 at 22:43 UTC
    Wow, date_time() is the clearest case of Missing The Point I've seen in a long time!

    The best part is subtracting 100 from $year then adding 2000, but only if it's greater than 99 or less than 2000. Classic.

    It's like Dostoyevsky wrote Perl -- paid by the line, a torturous and twisty romp through the mind of a killer.

    (For people who don't know why that's wrong, gmtime and localtime and such return the year component as the number of years since 1900.)