"TSa (Thomas Sandlaß)" skribis 2005-05-19 21:06 (+0200):
> >The above is more commonly written as
> >
> >    my @b = ([1,2,[3,4]);
> Assuming you meant @b = ([1,2,[3,4]]) what do the parens accomplish
> here? 

Thanks for the correction. That is indeed what I meant.

The parens do absolutely nothing, except indicate to a less skilled
reader that the [] aren't enclosing @b's future elements.

> Would it even need to be @b = (,[1,2,[3,4]]) to have the list
> contructing comma?

A single scalar in list context acts as a single item list, so a
specific list constructor is not needed.

> Does the following hold: @a = (1,2,3); @b = @a; [EMAIL PROTECTED] == 1?

No.

    @a = (1,2,3);

The array @a now contains 3 elements: 1, 2 and 3.

    @b = @a;

@a is in list context. An array in list context, evaluates to a list of
its elements. Three elements are assigned to @b. Assignment copies the
values.

    [EMAIL PROTECTED] == 1;

This expression is false. @b is in Num context. An array in numeric
context evaluates to its number of arguments. The number of arguments of
@b is 3, because it was just assigned three elements.

Do note that @a[0] == @b[0], but they are different variables:
assignment copies.

> My pivot question here is if +[1,2,[3,4]] == +(1,2,[3,4]) == 3 and

That is: +[ LIST ] == +(ITEM, ITEM, ITEM). The comma in scalar context
makes generates an anonymous array and returns a reference to that.
Effectively, this makes () apparently equal to [] in this statement.

> why then is @a = [1,2,[3,4]]; [EMAIL PROTECTED] == 1 but $a = [1,2,[3,4]]; 
> +$a == 3.

Because @a contains one element and @$a contains three elements. $a is
not an array, it is a reference to an array, just like @a[0].

[EMAIL PROTECTED] is 3, just like [EMAIL PROTECTED] (shortened as +$a).

> Perl6 has automatic deref and ref

An array in scalar context evaluates to a reference to itself.

A hash in scalar context evaluates to a reference to itself.

An array in list context evaluates to a list of its elements.

A hash in list context evaluates to a list of its elements (as pairs).

Array context is a scalar context.

A subroutine argument with the @ sigil represents an array that is
passed by reference. It can be accessed directly, without explicit
dereferencing.

A subroutine argument with the % sigil represents a hash that is
passed by reference. It can be accessed directly, without explicit
dereferencing.

    sub ($bar)       { the array is @$bar }  # non-array allowed in call
    sub (Array $bar) { the array is @$bar }
    sub (@bar)       { the array is @bar }
    
    $anyofthose.(@array) is the same as $anyofthose.([EMAIL PROTECTED])

    sub ([EMAIL PROTECTED])      { arguments are in list context, @bar has 
aliases }

    $thatthing.(@array)   # passes the elements, not @array itself
    $thatthing.([EMAIL PROTECTED])  # passes a reference to @array into @bar[0]
    $thatthing.(15)       # passes 15 into @bar[0]
    
> Otherwise $a = @b in Perl6 weren't the equivalent of Perl5's $a = [EMAIL 
> PROTECTED]

I hate the term "automatic referencing" because it sounds much more
magical than it actually is.

In Perl 5, @array in scalar context returned the number of elements.

In Perl 6, @array in scalar context returns a reference.

In Perl 6, you can further specify scalar context:

    * In Num context, @array returns its number of elements.
    * In Str context, @array returns its elements joined on whitespace.
    * In Bool context, @array returns true iff it has elements.
    * In Array context, @array returns a reference.
    * In Scalar context, @array returns a reference.

> BTW, how does @a = <one two three> compare to $a = <one two three>?

@a is an array, $a is a reference to another. The elements of the arrays
are equal, but not the same.

> Is [EMAIL PROTECTED] == 3?

Yes.

> Is $a[1] == 'two'?

Yes. Do note that this is really @$a[1] or $a.[1].

> this is the Perl6 language list, not a Perl5 beginners list.

Thank you for this reassurance.

> And I see a lot of things changed from Perl5 to Perl6, including
> referential semantics.

They haven't changed as much as you think. In fact, only how arrays and
hashes behave in scalar context has really changed.


Juerd
-- 
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html 
http://convolution.nl/gajigu_juerd_n.html

Reply via email to