in reply to Re^4: chopping a string into slices - is there a more elegant way to do it?
in thread chopping a string into slices - is there a more elegant way to do it?

It's not in that one case either. Except when empty, parens never do more than control precedence. Except when empty, they never create a list context.

It's almost always used with lists because "," has very low precedence, but it's the commas that create the list, not the parens.

  • Comment on Re^5: chopping a string into slices - is there a more elegant way to do it?
  • Download Code

Replies are listed 'Best First'.
Re^6: chopping a string into slices - is there a more elegant way to do it?
by LanX (Saint) on Nov 04, 2008 at 17:41 UTC
    thanx, actually I knew this once ... : (

    But in my example they do no harm, just emphasizing the right-to-left priority auf the evaluation. My explanation anyway was wrong!

    Anyway: I'm a great fan of the syntactical cleansing in Perl 6 and longing to use it ASAP, with or without parrot!

    UPDATE: You know, sometimes Perl-syntax is like "fizzbin" - the card game Kirk invents on the Mob planet

Re^6: chopping a string into slices - is there a more elegant way to do it?
by LanX (Saint) on Nov 30, 2008 at 11:17 UTC
    > Except when empty, parens never do more than control precedence. Except when empty, they never create a list context.

    And what about ($a)=@_; ???

    Unfortunately, it's not that easy!

    Cheers Rolf

    PS: Just Another example of perl-Fizzbin! ; )

      I'm sure I read somewhere that this form can be best understood in terms of the implied or transparent comma -- from memory, that would be a Unicode "ZERO WIDTH NO-BREAK COMMA". (Known to some as the fairy or pixie comma, spreading a little List Context magic while remaining tantalizingly just out of sight. I believe that in Ireland this is also known as one of the extensive family of O'Mission leprechauns.)

      To correct myself, there are very special situations where parens affect the result. eof vs eof() and (...)x... vs ...x..., for example. And of course ($x)=... vs $x=....

      But while parens sometimes do more than control precedence, I can't think of anywhere where non-empty parens create a list context.

      The parens in ($a)=@_ don't affect context. They cause a list assignment operator (aassign) to be used instead of a scalar assignment operator (sassign).

      That said, it's very convenient to think that some parens create a list, even when it isn't technically true. But the code being discussed definitely does not contain parens which could be thought of as "list forming".

        I've just been tripped up by:

          my @foo = (2, 3,  4  x 17, 3, 2, 0, 0)

        being quite different from:

          my @foo = (2, 3, (4) x 17, 3, 2, 0, 0)

        which looks as if the '()' confer some quality of list-ness. The documentation says:

        .... In scalar context or if the left operand is not enclosed in parentheses, it returns a string consisting of the left operand repeated the number of times specified by the right operand. In list context, if the left operand is enclosed in parentheses or is a list formed by qw/STRING/, it repeats the list.

        so the brackets are part of the syntax... I confess that I was (for reasons unknown) expecting 'x' to do listy things in List Context and scalary things otherwise. (Many things take lists of arguments, so I guess it would be inconvenient for (e.g.) '-' x 24 to yield a list in List Context.)

        Similarly, of course,  foreach VAR(LIST) BLOCK reenforces the feeling that '()' have something to do with the making of lists.

        > The parens in ($a)=@_ don't affect context. They cause a list assignment operator (aassign) to be used instead of a scalar assignment operator (sassign).

        I can't follow your interpretation ... aassign implies listcontext!

        DB<54> $a= join ",",(1..5) DB<55> p $a 1,2,3,4,5 DB<56> print scalar split /,/,$a 5 DB<57> ($A)=split /,/,$a DB<58> print $A 1
        split is executed in listcontext!

        Cheers Rolf