in reply to Re^2: compiling perl scripts aka why is perl not as fast as C
in thread compiling perl scripts aka why is perl not as fast as C

So you define Perl5's types as being scalar, array, hash and few others

Right, these are the user exposed types; strings and integers (PV/IV) are hidden from the user as much as possible, so I don't see them as types in Perl 5. (This attitude is influenced by me not writing any XS code, I think).

No, no coercions occur between any of of those types.
my $scalar = @array; # or my @array = $scalar;

Is that no coercion?

Talking of static vs dynamic typing sounds a lot like talking about strong vs weak typing.

In my book there's a difference: For example the C programming language has static typing (determined at compile time), but weak (you can cast anything to anything else even if it doesn't really makes sense).

But I'm guess I use these words not in the same sense as everybody else does, nor is there any shared consensus on what these things actually mean.

Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^4: compiling perl scripts aka why is perl not as fast as C
by ikegami (Patriarch) on Mar 22, 2010 at 17:10 UTC

    Is that no coercion?

    • my @array = $scalar;

      Definitely not. List assignment takes a list of scalars, and that's exactly what you provided. It's no more a scalar being coerced into an array than my @array = foo(); is a function call being coerced into an array.

    • my $scalar = @array;

      Type coercion is the changing an entity from one type to another, but that's not what that statement does. The array isn't assigned to $scalar as a different type. It's count is assigned, and that's a completely different entity.

      Furthermore, no array is ever present to be coerced. @array acts like a function. It takes an arg and returns a value based on the input and context. Note that there does not exist a function anywhere in Perl's code to coerce an array into a scalar.

      Definitely not. List assignment takes a list of scalars, and that's exactly what you provided.

      Call me naive, but I only see a scalar, not a list of scalars. Still a list of of scalars is a list, not an array, so even @array = ($a, $b) looks to me like a coercion. Likewise @array = foo() coerces the result list to an array. So you have $variable_of_one_type = $variable_of_other_type where the LHS dictates the type of the result.

      it's count is assigned, and that's a completely different entity.

      The count is an intrinsic property of the @array, and as far as I understand coercion, it is allowed to lose information - just like in other languages coercing a float to int loses the fractional part. I haven't seen a definition of coercion yet that requires the coerced object to retain the essence of what it was, however you define "essence".

      So you haven't convinced me in either case that not coercion is happening.

      Perl 6 - links to (nearly) everything that is Perl 6.

        Call me naive, but I only see a scalar, not a list of scalars.

        A list of one scalar is still a list of scalars. You know that parens aren't the what make lists.

        Still a list of of scalars is a list, not an array

        Ok, so you say the coercion that occurs is from list to array. If that's true, then

        my @a = @b;

        also does coercion (array to list, then list to array), and the topic becomes irrelevant and silly. It may be true, but the concept is completely useless and not worth discussing.

        So you haven't convinced me in either case that not coercion is happening.

        I concede it's at best a grey area, and not one I care much about.

        Update: Added last bit.

      @array acts like a function. It takes an arg and returns a value based on the input and context.

      Excuse me but, what would be the argument?

      --
       David Serrano
       (Please treat my english text just like Perl code, i.e. feel free to notify me of any syntax, grammar, style and/or spelling error. Thank you!).

        The array / array name / pad slot
        sub array { my ($self) = @_; wantarray ? $self->length : $self->contents }
Re^4: compiling perl scripts aka why is perl not as fast as C
by ikegami (Patriarch) on Mar 22, 2010 at 18:38 UTC

    But I'm guess I use these words not in the same sense as everybody else does,

    I think it's more a question of context than definition. Yes, there is static typing in Perl. But there is also dynamic typing. It depends on how zoomed in your view is. If you're concerned with optimising arithmetic, for example, you'll find yourself extremely limited by Perl's dynamic typing. Do you have a string? An integer? An object with overloads? Something magical?

    And then you try to optimise sub calls, and all you know is that you have a list of scalars. Are they the right type? If you think Perl is statically typed, then yes they are the right type since they are scalars. But if you take that view, you can't optimise anything. In reality, it's not good enough to know they are scalars.