Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: compiling perl scripts aka why is perl not as fast as C

by ikegami (Pope)
on Mar 22, 2010 at 17:10 UTC ( #830116=note: print w/replies, xml ) Need Help??


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

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.

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

        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

        I think the main issue is that we're talking about different levels: When I look at Perl code, I don't see that my @a = @b does a coercion to list and back again - so to me it doesn't happen. You seem to know that either by knowing the core or by memorizing some rules that are not obvious from looking at Perl code, or the documentation in perlsyn et al.

        So to me the isomorphism of a list of a single scalar and a list containing a single scalar also does not exist - When I see $foo, I think "this is a scalar" and not "this is a scalar, also constituting a list of a single scalar". It's the assignment that turns the RHS of my @a = $b into a list, and that's what I call coercion.

        Perl 6 - links to (nearly) everything that is Perl 6.
Re^5: compiling perl scripts aka why is perl not as fast as C
by Hue-Bond (Priest) on Mar 22, 2010 at 17:50 UTC
    @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 }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://830116]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2018-06-20 16:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (116 votes). Check out past polls.

    Notices?