in reply to Re: Re^4: Perl6 syntax being too much complex? How we will teach and read that?!
in thread Perl6 syntax being too much complex? How we will teach and read that?!

In fact, you're not even going to see
my $sizeofstring = length($string);
That's because "length" has been deemed to be an insufficiently specified concept, because it doesn't specify the units. Instead, if you want the length of something in characters you use
my $sizeinchars = chars($string);
and if you want the size in elements, you use
my $sizeinelems = elems(@array);
This is more orthogonal in some ways, insofar as you can now ask for the size in chars of an array, and it will add up all the lengths of the strings in it for you:
my $sizeinchars = chars(@array);
And if you ask for the number of elems of a scalar, it knows to dereference it:
my $ref = [1,2,3]; my $sizeinelems = elems($ref);
These are, in fact, just generic object methods:
@array.elems $string.chars @array.chars $ref.elems
And the functional forms are just method calls in disguise.

So maybe length() is a bad example now. Sorry. :-)

By the way, you can also use %hash.elems, which returns the number of pairs in the hash. I don't think %hash.chars is terribly useful, but it will tell you how many characters total there are in both keys and values.

Actually, the meaning of .chars varies depending on your current level of Unicode support. To be more specific, there's also:

$string.bytes $string.codepoints $string.graphemes $string.letters
...none of which should be confused with:
$string.columns
or its evil twin:
$string.pixels

Replies are listed 'Best First'.
Re: Re: Re: Re^4: Perl6 syntax being too much complex? How we will teach and read that?!
by BrowserUk (Patriarch) on Mar 25, 2004 at 07:51 UTC

    Better & better.

    Presumably my elems = $nonrefscalar.elems; is just an error?

    Do you need the parens for the functional form or can you do my $noffchars = chars $scalar:;


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
      Well, everything's an object underneath if you scratch it, so whether $nonrefscalar.elems returns an error probably depends on how $nonrefscalar is "tied" as a container object, to use the Perl 5 term. In general it's probably an error for the default implementation of scalars. Possibly it should return an undef containing an unthrown exception. Either that, or it should always return 1. :-)

      The parens are not necessary for an indirect object, which your colon forces it to be. Chances are it'll even work without the colon, because the syntactic function of the colon is to separate the indirect invocant from its arguments, and if there are no arguments, it's not ambiguous. It's only with arguments that the colon becomes obligatory.

      Basically, the dispatch rules are defined such that, for argumentless methods, it doesn't matter whether you call them as methods or subroutines--you end up at the same place. So you don't have to care whether

      close $handle;
      is defined as a global, multi-dispatched subroutine or as a normal method on filehandles.
        Well, everything's an object underneath if you scratch it,...

        That begs one more question, but I'll understand if it's too early for a difinitive answer:

        Is my $sub = { .... }; a private subroutine declaration? (And are while/map/reduce etc. methods of a code object?)


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail