There's obviously a reason there are 25,000 Perl help forums
(whether it be a community, like PerlMonks, or just some
messageboard). And it's not just because people are having
trouble learning the language, although that is a big part
of it. There are several problems that arise from trying to
learn Perl:
- Which book/books to use? There are a lot of bad ones,
but how does a newbie know a book is bad?
- Which online resources to use? Again, there are a lot
of bad tutorials and HORRIBLE depositories of Perl programs,
but if you're learning by example, the first examples you
see are the "best".
- Doesn't know that Perl comes with its own extensive
documentation (probably because they didn't read that part
of the GOOD book they bought, or they bought a bad book, or
they aren't using a book at all, and the resource they are
using isn't too good).
- Doesn't know enough about the basics of Perl, so
everything is difficult for them.
- Does not have the mindset of a programmer -- rather, the
person does not know how to solve a problem logically, and
therefore they can't solve it (efficiently, or at all) in
Perl.
- Does not recognize patterns!
That last one is from my mantra: Programming is about finding
patterns. If you can't find patterns, you're going to have
a very hard time. There are patterns in the documentation
and there are patterns in code. It would be a very silly
language indeed if there was nothing discernable from some
previous knowledge.
Take the "how to get unique elements from a list" question.
How would you do this as a human? If you can't answer that
question, then chances are you won't be able to get Perl to
do it for you. And even if you do, you probably don't know
HOW Perl did it. So sit down, and think about it in human
terms -- Perl might afford you a different approach, but at
least be able to do the task in real life.
You (I, in this case) would go through the list of things and
put all like things in respective piles. Then I would take
one thing from each pile.
Or, I would take the things one at a time, and if I'd already
put one in the "unique" pile, I wouldn't put it there a
second time.
Those represent two different ways of approaching the problem
as a human, and can be directly translated into Perl:
# method 1
{
my %uniq;
@uniq{@elements} = (); # placing them into piles
@uniq = keys %uniq; # getting one from each pile
}
# method 2
{
my %seen;
@uniq = ();
for (@elements) {
push @uniq if !$seen{$_}++; # inserting if not seen
}
}
Sure, there are less compact ways of writing those, and you
would probably not see these idiomatic methods right away,
but the point is that you should be able to think in human
terms and translate to Perl (if you can't already think in
Perl
;)).
Which gets me to my main point. Patterns. If someone asks
"how can I get the length of a string?", and you respond by
telling them about the (poorly-named)
length()
function, and they ask how they use it... that's a case of
a person not seeing a pattern. Have they never used a Perl
function before? Do they not know how to look up a function
for themselves?
Another example: someone wants to know how to arguments in
a function. You tell them "the
@_ array." They
ask how they get arguments from it.
It's just like any
other array! (That's a little white lie, but nevermind.)
Has the person never used arrays before? Do they not know
how array indexing works (that is a HIGH possibility, seeing
as how tons of people use
@array[$x] when
they shouldn't)?
Or here's a good one. "How do I tell if a number is even or
odd?" I am not going to touch that one.
Ok, that's my rant. It boils down to my mantra. Programming
is about finding patterns. That's the test.
$_="goto+F.print+chop;\n=yhpaj";F1:eval
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.