in reply to Re: 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

Let me clarify that Perl does have static typing - it is implied by sigils,>

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

However instead of type check failures, you get coercions in many places,

No, no coercions occur between any of of those types. There's only coercion if you consider Perl to have dynamic typing (signed integer, unsigned integer, UTF8=0 string, UTF8=1 string, float, regex, IO, etc) in addition to static typing.

The more a compiler knows about a program, the more it can optimize.

Indeed, and Perl5 doesn't have that much information available at compile time. Yeah, you can consider scalar to be a static type, but considering all subs take a list of scalars for argument, it's a rather useless type from the perspective of optimising sub calls.

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

  • Comment on Re^2: compiling perl scripts aka why is perl not as fast as C

Replies are listed 'Best First'.
Re^3: compiling perl scripts aka why is perl not as fast as C
by moritz (Cardinal) on Mar 22, 2010 at 09:56 UTC
    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.

      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.
        @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!).

      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.