On Sun, 30 Aug 2020, ToddAndMargo via perl6-users wrote:
> >    - You are calling .lines on the value of .IO.open which is an
> >      IO::Handle. IO::Handle.lines does not take a named argument
> >      :chomp, so passing one is useless.
> 
> That explains it.
> 
> Butttttttt:
>      https://docs.raku.org/type/IO::Path#method_lines
> 
>      (IO::Path) method lines
>      Defined as:
> 
>      method lines(IO::Path:D: :$chomp = True, :$enc = 'utf8', :$nl-in =
> ["\x0A", "\r\n"], |c --> Seq:D)
> 
>      Opens the invocant and returns its lines.
> 
>      The behavior is equivalent to opening the file specified
>      by the invocant, forwarding the :$chomp, :$enc, and
>      :$nl-in arguments to IO::Handle.open, then calling
>      IO::Handle.lines on that handle, forwarding any of
>      the remaining arguments to that method, and returning
>      the resultant Seq.
> 
> The "signature" line (cryptogram) clearly stated that
> ":$chomp" is a parameter of the method.
> 
> Now you are obviously correct that :chomp is ignored.
> If I am not mistaken, I have just tripped over another
> error in the documentation.  Your take?
> 

You confuse two methods that have the same name "lines". One of them,
which exists in the IO::Path class, has a :chomp argument. The other,
on IO::Handle does not.

  "path".IO.lines()       <-- calls lines on an IO::Path (supports :chomp)
  "path".IO.open.lines()  <-- calls lines on an IO::Handle (does not support 
:chomp)

In the second case, if you want chomping to happen you have to pass
the :chomp to the *open* call, as you do below:

> $ p6 'dd "Lines.txt".IO.open(:chomp).lines()[3,2];'
> ("Line 3", "Line 2")
> 
> $ p6 'dd "Lines.txt".IO.open(:!chomp).lines()[3,2];'
> ("Line 3\n", "Line 2\n")
> 
> $ p6 'say "Lines.txt".IO.open(:!chomp).lines()[3,2];'
> (Line 3
>  Line 2    <-- why the space?
> )
> 

The better question is "whose space". You print again the List of lines
you obtain by indexing the return value of .lines, so expect output in
the form of "(item1 item2 ...)". This is where your space comes from.
Lists print that way. They insert spaces between their elements.

In this case, you had :chomp disabled, so the "Line 3" item printed
"Line 3\n", including the newline that was not chomped, then the space
and then the next item, "Line2\n" with its newline, then the closing
parenthesis which is also part of printing a List.

This serves to better illustrate where one element of the List ends
and where the next begins. The space separate them:

  $ rakudo -e 'say "Lines.txt".IO.open(:!chomp).lines()[3,2].map({ "«$_»" });'
  («Line 3
  » «Line 2
  »)

Best,
Tobias

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk

Reply via email to