Skip to content

Instantly share code, notes, and snippets.

@treyharris
Last active June 20, 2019 21:06
Show Gist options
  • Select an option

  • Save treyharris/489a71a643511895ac272e963eecc973 to your computer and use it in GitHub Desktop.

Select an option

Save treyharris/489a71a643511895ac272e963eecc973 to your computer and use it in GitHub Desktop.
~/src/perl6/sandbox/yes | head
y
y
y
y
y
y
y
y
y
y
Failed to write bytes to filehandle: Broken pipe
  in sub MAIN at /home/trey/src/perl6/sandbox/yes line 3
  in block <unit> at /home/trey/src/perl6/sandbox/yes line 1

Unhandled exception: Failed to write bytes to filehandle: Broken pipe
   at SETTING::src/core/Exception.pm6:485  (/home/trey/perl6/rakudo-star/rakudo-star-2019.03/install/share/perl6/runtime/CORE.setting.moarvm:<anon>)
 from gen/moar/stage2/NQPHLL.nqp:1854  (/home/trey/perl6/rakudo-star/rakudo-star-2019.03/install/share/nqp/lib/NQPHLL.moarvm:command_eval)
 from src/Perl6/Compiler.nqp:38  (/home/trey/perl6/rakudo-star/rakudo-star-2019.03/install/share/nqp/lib/Perl6/Compiler.moarvm:command_eval)
 from gen/moar/stage2/NQPHLL.nqp:1773  (/home/trey/perl6/rakudo-star/rakudo-star-2019.03/install/share/nqp/lib/NQPHLL.moarvm:command_line)
 from gen/moar/main.nqp:48  (/home/trey/perl6/rakudo-star/rakudo-star-2019.03/install/share/perl6/runtime/perl6.moarvm:MAIN)
 from gen/moar/main.nqp:1  (/home/trey/perl6/rakudo-star/rakudo-star-2019.03/install/share/perl6/runtime/perl6.moarvm:<mainline>)
 from <unknown>:1  (/home/trey/perl6/rakudo-star/rakudo-star-2019.03/install/share/perl6/runtime/perl6.moarvm:<main>)
 from <unknown>:1  (/home/trey/perl6/rakudo-star/rakudo-star-2019.03/install/share/perl6/runtime/perl6.moarvm:<entry>)
#!/usr/bin/env perl6
sub MAIN(Str $output="y") {
loop {
say $output;
}
}
@b2gills
Copy link

b2gills commented Jun 20, 2019

This doesn't even work:

sub MAIN(Str $output="y") {
  loop {
    CATCH { default { last }}
    say $output;
  }
}

# this doesn't change anything
CATCH { default {}}
y
y
y
y
y
y
y
y
y
y
Unhandled exception: Failed to write bytes to filehandle: Broken pipe

It seems that something outside of our control is printing that final line.
(Even if you redirect $*ERR somewhere else, it still gets printed to STDERR.)


A work-around is to also close $*ERR.
(Rakudo can't print anything to STDERR if it is already closed.)

sub MAIN(Str $output="y") {
  loop {
    # this works basically the same as previous example
    last unless try {say $output};
  }

  $*ERR.close;
}

(Without checking, I would guess that this returns an exitcode of 1.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment