Quote:
Originally Posted by ecchi
I disagree, Took me a few hours to learn the basics and write my first useful bit of code. I could not guess how long it took to learn more throughly because from then on I learnt more as I needed it. Although in the early days some of the jobs were hard to write, and I did occasionally suffer from the "spend five hours writing code that will save you two hours work" syndrome, that was simply because I was still learning. Once it is learnt (and learning it is not hard) writing good Perl is relatively easy.
|
There's a difference though between "good" as in syntactically correct, and "good" as in maintainable -- the latter bit is very difficult due to the way that Perl allows you to solve problems in a variety of ways, some are good, some aren't. Especially if you're dealing with code that needs to perform well and still remain maintainable.
Practical example being, suppose I have an array full of, say, usernames, and I want to de-dupe them. There's at least 2 ways to do it:
Code:
# usernames
my @usernames = ('a'..'z','a'..'z'); # a thru z, twice
# method 1
sub method_one {
my %usernames = (map { $_ => 1 } @usernames);
print sort keys %usernames;
}
# method 2
sub method_two {
my %usernames = ();
$usernames{$_}++ for(@usernames);
print sort keys %usernames
}
Most people I show this one to will say that method #1 will be the most efficient, however if you run this through benchmark:
Code:
Rate Method #1 Method #2
Method #1 16181/s -- -41%
Method #2 27624/s 71% --
The 2nd method is almost twice as fast, even though a lot of people will say map would be much more efficient.
Another fun one; say you have to http escape a string by turning the string into it's own hex interpretation, the one I see most (I've asked this question to applicants at job interviews) is this:
Code:
my $string = 'http://someurl/';
printf "%02X", $_ for(split //, $string);
Nothing wrong with that one, but...
Code:
my $string = 'http://someurl/';
printf '%02X' x length($string), map { ord $_ } (split //, $string);
Is faster:
Code:
one 53763/s -- -16%
two 64103/s 19% --
Anyhow, not trying to be a dick (probably am looking like one right now), just trying to illustrate the difference between knowing the language and
knowing the language
Now, for fun, let's both solve this problem: Write a perl script that outputs the numbers from 1 to 100, 10 per line, zero-padded.
Example output:
Code:
001 002 003 004 005 006 007 008 009 010
011 012 013 014 015 016 017 018 019 020
.
.
.
.
I'll post my solution later on

And sorry for the thread hijack
