[Israel.pm] annoying bugs

Avishalom Shalit avishalom at gmail.com
Sun Dec 28 02:40:36 PST 2008


but they are splitting a string,
the array in scalar context is it's length.  (1 which becomes "1")
the question is "what are they trying to do?"

they could have meant $argv[0] , the first string. (perhaps they even
meant to store the original as a string. (but coming from another
language they had mentally called it a character array))

or they could have meant
map{[split /X/]} @argv

either way, what needs explaining is the what is an array in list
context, what is an an array in scalar context
what is the difference between an array and a list,
what happens when you assign a list to a scalar, etc.

like i said, this is the biggest respect in which perl is conceptually
different.
in other languages, you have overloaded operators or functions, but
these are defined explicitly
in perl, most built in functions are overloaded and do different
things when the context is different.
this is implicit, and not defined anywhere explicitly , (you could
read a bit of C code and eventually you understand that the linker
will make the right call based on datatypes, you need to read a lot of
perl before you begin to suspect that the functions are ambiguous and
just what is the syntax making of a list vs a scalar e.g. print
"x","x" vs. "x"."x")
so for perl, this has to be taught specifically,
$a=@b
versus
$a=(1,2,3)
versus
($a)=(1,2,3)

etc. etc.
but this is very basic.
there is a great and important difference between /(regex)/ in scalar
and list context


2008/12/28 sawyer x <xsawyerx at gmail.com>:
> 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.
>
> On Sun, Dec 28, 2008 at 12:03 PM, Gabor Szabo <szabgab at gmail.com> wrote:
>>
>> As I am teaching I see beginners make lots of annoying mistakes that
>> perl does not catch
>> and is hard to explain.
>> Eg.  What does the following code do and why?
>>
>> Gabor
>>
>> use strict;
>> use warnings;
>>
>> my @argv = ("abcdXezXq");
>>
>> my @view = split ("X" , @argv );
>> print @view;
>> _______________________________________________
>> Perl mailing list
>> Perl at perl.org.il
>> http://perl.org.il/mailman/listinfo/perl
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>



-- 
-- vish



More information about the Perl mailing list