Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[6.d] Set $*ARGFILES to $*IN inside sub MAIN
- Per 6.d-prep
- This lets users use `lines()` and similar shortcuts that work on
    $*ARGFILES and get data from STDIN, without having arguments
    that drive the MAIN sub be interpreted as filenames
  • Loading branch information
zoffixznet committed Jul 29, 2018
1 parent d2116ef commit 84d45f0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Perl6/Actions.nqp
Expand Up @@ -1314,6 +1314,10 @@ class Perl6::Actions is HLL::Actions does STDActions {
$mainline := QAST::Op.new(
:op('call'),
:name('&MAIN_HELPER'),
QAST::WVal.new( # $*IN as $*ARGSFILES
value => $*W.find_symbol: [
'Bool', $*W.lang-ver-before('d') ?? 'False' !! 'True'
]),
$mainline,
);
}
Expand Down
12 changes: 10 additions & 2 deletions src/core/Main.pm6
Expand Up @@ -5,7 +5,7 @@
# * Allow exact Perl 6 forms, quoted away from shell
# * Fix remaining XXXX

my sub MAIN_HELPER($retval = 0) {
my sub MAIN_HELPER($IN-as-ARGSFILES, $retval = 0) {
# Do we have a MAIN at all?
my $m = callframe(1).my<&MAIN>;
return $retval unless $m;
Expand Down Expand Up @@ -225,7 +225,15 @@ my sub MAIN_HELPER($retval = 0) {
@matching_candidates .=grep: {!has-unexpected-named-arguments($_.signature, $n)};
# If there are still some candidates left, try to dispatch to MAIN
if +@matching_candidates {
$m(|@($p), |%($n));
if $IN-as-ARGSFILES {
my $*ARGFILES := IO::ArgFiles.new: (my $in := $*IN),
:nl-in($in.nl-in), :chomp($in.chomp), :encoding($in.encoding),
:bin(nqp::p6bool(nqp::isfalse($in.encoding)));
$m(|@($p), |%($n));
}
else {
$m(|@($p), |%($n));
}
return;
}

Expand Down

3 comments on commit 84d45f0

@lizmat
Copy link
Contributor

@lizmat lizmat commented on 84d45f0 Oct 9, 2018

Choose a reason for hiding this comment

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

@zoffixznet I'm not sure why $IN-as-ARGSFILES couldn't have been made a named variable, thus conserving the API that existed before. Is there a specific reason that you made it a positional I'm missing here?

Related: do you know where $retval is actually being used? I couldn't find any use of it.

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

Is there a specific reason that you made it a positional I'm missing here?

I don't recall any, other than the vague idea that positionals are faster than nameds (don't know if it's still true). I didn't consider MAIN_HELPER a public routine, so I didn't concern myself with any APIs.

Related: do you know where $retval is actually being used? I couldn't find any use of it.

Don't know.

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

(P.S.: if it's going to become an API, we should try to use kebob-case in its name for consistency with other things)

Please sign in to comment.