Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement IO::CatHandle.handles
Lets the user work on source handles individually, which is
especially handy when working with $*ARGFILES. e.g.:

    # print at most the first 2 lines of each file in $*ARGFILES:
    .say for flat $*ARGFILES.handles.map: *.lines: 2

Addresses R#1546 #1546
  • Loading branch information
zoffixznet committed Feb 25, 2018
1 parent 2a8b17a commit d5baa03
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/core/IO/CatHandle.pm
Expand Up @@ -95,6 +95,31 @@ my class IO::CatHandle is IO::Handle {
$!active-handle)
}

method handles(IO::Handle:D: --> Seq:D) {
Seq.new: class :: does Iterator {
has $.cat;
has $!gave-active;

method !SET-SELF(\cat) { $!cat := cat; self }
method new(\cat) { nqp::create(self)!SET-SELF: cat }

method pull-one {
nqp::if(
$!gave-active,
nqp::if(
nqp::eqaddr((my $h := $!cat.next-handle), Nil),
IterationEnd,
$h),
nqp::stmts(
($!gave-active := True),
nqp::eqaddr(
(my $ah := nqp::decont(nqp::getattr($!cat, IO::CatHandle,
'$!active-handle'))),
Nil) ?? IterationEnd !! $ah))
}
}.new: self
}

method chomp (::?CLASS:D:) is rw {
Proxy.new:
:FETCH{ $!chomp },
Expand Down

6 comments on commit d5baa03

@usev6
Copy link
Contributor

@usev6 usev6 commented on d5baa03 Feb 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this doesn't work on the JVM backend:

$ ./perl6-j -e 'use nqp; my $cat = IO::CatHandle.new; my $nh := $cat.next-handle; dd $nh; say nqp::eqaddr($nh, Nil)'
Nil
0

Would it be possible to use a construct with nqp::defined here (and in line 115)? (At least nqp::defined(self.next-handle) is used multiple times in this file.)

[Please note: I don't know how to fix the underlying issue with nqp::eqaddr. I've tried to look at it once and I vaguely remember it relied on hashCode on the JVM backend.]

@zoffixznet
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That means =:= operator doesn't work either and a number of fast-path optimizations don't work either.

Is there even a ticket for this bug?

@usev6
Copy link
Contributor

@usev6 usev6 commented on d5baa03 Feb 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only problem I remember wrt nqp::eqaddr was noted here: https://rt.perl.org/Ticket/Display.html?id=128041

I don't really understand under which conditions nqp::eqaddr gives the wrong answer. Mostly, it seems to work just fine (we have quite a lot of nqp::eqaddr in rakudo's core.)

@usev6
Copy link
Contributor

@usev6 usev6 commented on d5baa03 Feb 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, after hitting the comment button, it occured to me that a nqp::decont might make a difference:

$ ./perl6-j -e 'use nqp; my $cat = IO::CatHandle.new; my $nh := $cat.next-handle; dd $nh; say nqp::eqaddr(nqp::decont($nh), Nil)'
Nil
1

@zoffixznet
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in eb06492213

@usev6
Copy link
Contributor

@usev6 usev6 commented on d5baa03 Feb 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

Please sign in to comment.