http://www.perlmonks.org?node_id=481465

The newbie has questions... the wise old monks have answers... but all too often newbie can't communicate the question, and the wise old monks fall over each other trying to guess what the newbie might have meant -- perhaps what merlyn meant when he wrote "the only personality trait Perl programmers have in common is that they're all pathologically helpful" (Programming Perl, "Perl Culture"). Examples of this happen all the time -- see for instance the recent post at Regex doubt.

Well, pathological helpfulness (or xp greed :) ) has its bright side as a cultural feature, but enough is enough.

With this series of posts I want to help everyone save time, newbies and wise old owls alike, by providing templates for how to ask certain common types of questions. This is intended to be an alternative place to point newbies to, rather than the (also very helpful, but in a more general way) How (Not) To Ask A Question.

As a bonus feature, newbies learning this will also learn Test::More, a powerful tool every monk should incorporate into their life.

The first example will be short. My comments are in ~ . Here goes.

******************BEGIN QUESTION TEMPLATE************************

O Wise Monks, I am having a problem I hope you can help me with.

As you can see, in the following code, my zero test for the "multiplies_by_seven" function fails. ~the problem is isolated~

I tried googling on "perldoc shift" but no dice. Am I searching with the wrong search terms? ~the newbie tried to help himself first before going to the perlmonks~

What the heck am I doing wrong?

~The novice now posts his code using code tags: see Writeup Formatting Tips~

# use strict; #~the newbie has used the strictures~ use warnings; use Test::More qw(no_plan); my ($result, $expected); # ~convenient way to structure your input and expected output is to ~ # ~suck in the test data, which is everything after the __DATA_ line.~ while (<DATA>) { #~splits a string on spaces by default; see perldoc -f split~ my ($input, $expected) = split; my $output = multiply_by_seven($input); is($output, $expected, "testing $input"); } sub multiply_by_seven { my $number = shift or die "no number"; return $number*7; } #~convenient way to structure your input and expected output, #assuming that you can use whitespace as the delimiter. __DATA__ 1 7 2 14 3 21 0 0
I'm on Windows XP system using the standard ActiveState perl distribution. ~The novice mentions what system he is on.~

I got both standard and error output out of my test, into one output file, by running this with perl multiplyBySeven.pl > output.txt 2>&1 output.txt. ~Enables newbie to communicate his test result with a minimum of putzing around~

The output is:

ok 1 - testing 1 ok 2 - testing 2 ok 3 - testing 3 no number at multiplyBySeven.pl line 18, <DATA> line 4. 1..3 # Looks like your test died just after 3.

Any ideas? Thanks very much in advance, o wise monks :)

******************END QUESTION TEMPLATE************************

That's basically it. Why the 0 test fails is left as an exercise for the reader :)


Update: More tutorials in this series to come. See tphyahoo's homenode for tutorials under construction :)