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

UPDATE:(Dec 1. 2008) further down the thread I admit and show that I was wrong in this node! No need to discuss it further I don't think so, IMHO you are getting at first hand a list which is later reevaluated in scalar context!

... I'll run some tests to find the "truth" ; )

UPDATE: I think your example is equivalent to

my @a= ( $str =~ /.{1,$ml}/g ) ; $a=scalar @a; print $a;
nevertheless is =~ evaluated in list-context!
  • Comment on Re^4: chopping a string into slices - is there a more elegant way to do it?
  • Download Code

Replies are listed 'Best First'.
Re^5: chopping a string into slices - is there a more elegant way to do it?
by ikegami (Patriarch) on Nov 04, 2008 at 17:33 UTC

    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.

      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

      > 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".

Re^5: chopping a string into slices - is there a more elegant way to do it?
by LanX (Saint) on Nov 04, 2008 at 17:28 UTC
    I apologize, you were right!
    $a= ( test() ); print $a; #> 9 sub test { return (1,2) if wantarray; return 9; }
    the paranthesis don't force list context, they seem to be interpreted for the precedence of the lexer!