[Israel.pm] annoying bugs

Gaal Yahas gaal at forum2.org
Sun Dec 28 08:58:44 PST 2008

On Sun, Dec 28, 2008 at 6:22 PM, Gabor Szabo <szabgab at gmail.com> wrote:
> On Sun, Dec 28, 2008 at 12:25 PM, sawyer x <xsawyerx at gmail.com> wrote:
>> I don't think this is too difficult to explain.
>> The problem lies in the mistake of trying to do an action on one that
>> does not receive such action.
>> They want to split an array. You cannot split an array, an array is
>> already split. You want to split a string. This could have simply been
>> a typo. If they would, for instance, do (under vim) :%s/@argv/$argv/g
>> everything would be okay.
> To put slightly more context on the example, the actual code was
> trying to split @ARGV.
> So the person who wrote this - and I've already encounter this many
> times - has not
> yet clearly understood that the things he types on the command line are already
> separated into distinctive elements and are placed in @ARGV.
> Usually they write such code after I teach them about arrays,
> the command line and @ARGV but *before* I teach about context.
> So I cannot really explain the problem by the context. At least not during
> the exercise.
> It is also slightly irrelevant as what the students should really
> understand is the
> conceptual reason why you cannot split an array:
> The best explanation I read in this thread was the
> "You cannot split an array as it is already split"
> and that
> "you are trying to do an operation (split) on something that cannot be
> split (an array)".

This is a good isolation of the problem; it has nothing to do with
context, which is only relevant when you try to explain not what the
user did wrong, but what Perl (confusingly or not) thought the user

If your audience are beginning Perl programmers who nonetheless have
experience in some other language, I am sure you can translate this to
the language they do know and clarify the mistake. For example, let's
assume C++ and a library function Split():

  vector<string> input_array;

  vector<string> split_output;
  Split(input_array, &split_output);

This of course does not compile, and the error message it gives is
pretty ugly (though it does state what the problem is: Split() does
not accept a vector in its first argument).

> Thanks for the responses.
> Now to put this mail even further in context I have been planning to
> add a "beginner mode" to Padre where it would catch such bugs.
> We have a ticket http://padre.perlide.org/ticket/52 for this on Padre
> and I have just committed the first implementation of this catching
> the above described case.

Interesting, but sounds extremely difficult to accomplish.

Again with the Perl 6, in some cases (I'm assuming you're shooting for
overt and common problems, after all), multidispatch might help in
some of these cases.

  multi split (Rule $foo, @array) { die "looks like you are trying to
split on an array. split is meant to work on strings."; }

This is runtime, not compile time (or even edit time), but it might
still be useful -- it's certainly easier to implement!

> regards
>   Gabor
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl

Gaal Yahas <gaal at forum2.org>

More information about the Perl mailing list