Perl 6 iterate through command line arguments
I want to write a Perl 6 program that performs the same operation on an undetermined number of files, just like how you can use rm file1.txt file2.txt file3.txt
in Linux.
My code looks like this:
#!/usr/bin/env perl6
my @args = @*ARGS.perl;
for @args -> $arg {
say $arg
}
However, when I invoke it as ./loop_args.pl6 file1.txt file2.txt
I get the following output: ["file1.txt", "file2.txt"]
What am I doing wrong? I just want it to be an array.
perl6
add a comment |
I want to write a Perl 6 program that performs the same operation on an undetermined number of files, just like how you can use rm file1.txt file2.txt file3.txt
in Linux.
My code looks like this:
#!/usr/bin/env perl6
my @args = @*ARGS.perl;
for @args -> $arg {
say $arg
}
However, when I invoke it as ./loop_args.pl6 file1.txt file2.txt
I get the following output: ["file1.txt", "file2.txt"]
What am I doing wrong? I just want it to be an array.
perl6
I would like to know what lead you to use.perl
, so that we can prevent others from going down this path in the future.
– Brad Gilbert
Nov 16 '18 at 16:07
Hey Brad, I misread the documentation here; I have a bad habit of only looking at the code: perl6maven.com/parsing-command-line-arguments-perl6
– Taylor Lee
Nov 16 '18 at 23:28
Ok. Short of asking Gabor to change that article, I suppose there's not much to do.
– Brad Gilbert
Nov 16 '18 at 23:41
add a comment |
I want to write a Perl 6 program that performs the same operation on an undetermined number of files, just like how you can use rm file1.txt file2.txt file3.txt
in Linux.
My code looks like this:
#!/usr/bin/env perl6
my @args = @*ARGS.perl;
for @args -> $arg {
say $arg
}
However, when I invoke it as ./loop_args.pl6 file1.txt file2.txt
I get the following output: ["file1.txt", "file2.txt"]
What am I doing wrong? I just want it to be an array.
perl6
I want to write a Perl 6 program that performs the same operation on an undetermined number of files, just like how you can use rm file1.txt file2.txt file3.txt
in Linux.
My code looks like this:
#!/usr/bin/env perl6
my @args = @*ARGS.perl;
for @args -> $arg {
say $arg
}
However, when I invoke it as ./loop_args.pl6 file1.txt file2.txt
I get the following output: ["file1.txt", "file2.txt"]
What am I doing wrong? I just want it to be an array.
perl6
perl6
asked Nov 13 '18 at 1:49
Taylor Lee
432
432
I would like to know what lead you to use.perl
, so that we can prevent others from going down this path in the future.
– Brad Gilbert
Nov 16 '18 at 16:07
Hey Brad, I misread the documentation here; I have a bad habit of only looking at the code: perl6maven.com/parsing-command-line-arguments-perl6
– Taylor Lee
Nov 16 '18 at 23:28
Ok. Short of asking Gabor to change that article, I suppose there's not much to do.
– Brad Gilbert
Nov 16 '18 at 23:41
add a comment |
I would like to know what lead you to use.perl
, so that we can prevent others from going down this path in the future.
– Brad Gilbert
Nov 16 '18 at 16:07
Hey Brad, I misread the documentation here; I have a bad habit of only looking at the code: perl6maven.com/parsing-command-line-arguments-perl6
– Taylor Lee
Nov 16 '18 at 23:28
Ok. Short of asking Gabor to change that article, I suppose there's not much to do.
– Brad Gilbert
Nov 16 '18 at 23:41
I would like to know what lead you to use
.perl
, so that we can prevent others from going down this path in the future.– Brad Gilbert
Nov 16 '18 at 16:07
I would like to know what lead you to use
.perl
, so that we can prevent others from going down this path in the future.– Brad Gilbert
Nov 16 '18 at 16:07
Hey Brad, I misread the documentation here; I have a bad habit of only looking at the code: perl6maven.com/parsing-command-line-arguments-perl6
– Taylor Lee
Nov 16 '18 at 23:28
Hey Brad, I misread the documentation here; I have a bad habit of only looking at the code: perl6maven.com/parsing-command-line-arguments-perl6
– Taylor Lee
Nov 16 '18 at 23:28
Ok. Short of asking Gabor to change that article, I suppose there's not much to do.
– Brad Gilbert
Nov 16 '18 at 23:41
Ok. Short of asking Gabor to change that article, I suppose there's not much to do.
– Brad Gilbert
Nov 16 '18 at 23:41
add a comment |
2 Answers
2
active
oldest
votes
For Perl6 the arguments array is @*ARGS
, this gives the code:
#! /usr/bin/env perl6
use v6;
for @*ARGS -> $arg
{
print("Arg $argn");
}
So you just need to remove the .perl
from your my @args = @*ARGS.perl;
line.
EDIT: Updated as per Ralphs's comments
You don't need theuse strict;
there :)
– Scimon
Nov 13 '18 at 9:04
2
To clarify @Scimon's comment you don't need theuse strict;
because P6 defaults to strict. While I'm writing a comment I'll also note that themy $arg;
declares a$arg
but it's redundant and misleading. The-> $arg
declares another$arg
that shadows themy
one inside the block.
– raiph
Nov 13 '18 at 19:24
add a comment |
So it depends on what you want to do with the files. If you want to read through them all you might want to take a look at $*ARGFILES
this is an IO::CatHandle Object that batches up all the arguments and treats them as a list of files. So to print them all out you could do.
#! /usr/bin/env perl6
use v6;
for $*ARGFILES.handles -> $IO {
$IO.path.say
}
Ok so that's a bit more convoluted than looking at @*ARGS. But what if you wanted to print the first 5 lines for each file?
#! /usr/bin/env perl6
use v6;
for $*ARGFILES.handles -> $IO {
say $IO.lines: 5;
}
Or maybe you just want to read the contents of all the files and print them out?
#! /usr/bin/env perl6
use v6;
.say for $*ARGFILES.lines;
Hope that gives you some ideas.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272620%2fperl-6-iterate-through-command-line-arguments%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
For Perl6 the arguments array is @*ARGS
, this gives the code:
#! /usr/bin/env perl6
use v6;
for @*ARGS -> $arg
{
print("Arg $argn");
}
So you just need to remove the .perl
from your my @args = @*ARGS.perl;
line.
EDIT: Updated as per Ralphs's comments
You don't need theuse strict;
there :)
– Scimon
Nov 13 '18 at 9:04
2
To clarify @Scimon's comment you don't need theuse strict;
because P6 defaults to strict. While I'm writing a comment I'll also note that themy $arg;
declares a$arg
but it's redundant and misleading. The-> $arg
declares another$arg
that shadows themy
one inside the block.
– raiph
Nov 13 '18 at 19:24
add a comment |
For Perl6 the arguments array is @*ARGS
, this gives the code:
#! /usr/bin/env perl6
use v6;
for @*ARGS -> $arg
{
print("Arg $argn");
}
So you just need to remove the .perl
from your my @args = @*ARGS.perl;
line.
EDIT: Updated as per Ralphs's comments
You don't need theuse strict;
there :)
– Scimon
Nov 13 '18 at 9:04
2
To clarify @Scimon's comment you don't need theuse strict;
because P6 defaults to strict. While I'm writing a comment I'll also note that themy $arg;
declares a$arg
but it's redundant and misleading. The-> $arg
declares another$arg
that shadows themy
one inside the block.
– raiph
Nov 13 '18 at 19:24
add a comment |
For Perl6 the arguments array is @*ARGS
, this gives the code:
#! /usr/bin/env perl6
use v6;
for @*ARGS -> $arg
{
print("Arg $argn");
}
So you just need to remove the .perl
from your my @args = @*ARGS.perl;
line.
EDIT: Updated as per Ralphs's comments
For Perl6 the arguments array is @*ARGS
, this gives the code:
#! /usr/bin/env perl6
use v6;
for @*ARGS -> $arg
{
print("Arg $argn");
}
So you just need to remove the .perl
from your my @args = @*ARGS.perl;
line.
EDIT: Updated as per Ralphs's comments
edited Nov 13 '18 at 21:32
answered Nov 13 '18 at 2:14
Kingsley
2,26011023
2,26011023
You don't need theuse strict;
there :)
– Scimon
Nov 13 '18 at 9:04
2
To clarify @Scimon's comment you don't need theuse strict;
because P6 defaults to strict. While I'm writing a comment I'll also note that themy $arg;
declares a$arg
but it's redundant and misleading. The-> $arg
declares another$arg
that shadows themy
one inside the block.
– raiph
Nov 13 '18 at 19:24
add a comment |
You don't need theuse strict;
there :)
– Scimon
Nov 13 '18 at 9:04
2
To clarify @Scimon's comment you don't need theuse strict;
because P6 defaults to strict. While I'm writing a comment I'll also note that themy $arg;
declares a$arg
but it's redundant and misleading. The-> $arg
declares another$arg
that shadows themy
one inside the block.
– raiph
Nov 13 '18 at 19:24
You don't need the
use strict;
there :)– Scimon
Nov 13 '18 at 9:04
You don't need the
use strict;
there :)– Scimon
Nov 13 '18 at 9:04
2
2
To clarify @Scimon's comment you don't need the
use strict;
because P6 defaults to strict. While I'm writing a comment I'll also note that the my $arg;
declares a $arg
but it's redundant and misleading. The -> $arg
declares another $arg
that shadows the my
one inside the block.– raiph
Nov 13 '18 at 19:24
To clarify @Scimon's comment you don't need the
use strict;
because P6 defaults to strict. While I'm writing a comment I'll also note that the my $arg;
declares a $arg
but it's redundant and misleading. The -> $arg
declares another $arg
that shadows the my
one inside the block.– raiph
Nov 13 '18 at 19:24
add a comment |
So it depends on what you want to do with the files. If you want to read through them all you might want to take a look at $*ARGFILES
this is an IO::CatHandle Object that batches up all the arguments and treats them as a list of files. So to print them all out you could do.
#! /usr/bin/env perl6
use v6;
for $*ARGFILES.handles -> $IO {
$IO.path.say
}
Ok so that's a bit more convoluted than looking at @*ARGS. But what if you wanted to print the first 5 lines for each file?
#! /usr/bin/env perl6
use v6;
for $*ARGFILES.handles -> $IO {
say $IO.lines: 5;
}
Or maybe you just want to read the contents of all the files and print them out?
#! /usr/bin/env perl6
use v6;
.say for $*ARGFILES.lines;
Hope that gives you some ideas.
add a comment |
So it depends on what you want to do with the files. If you want to read through them all you might want to take a look at $*ARGFILES
this is an IO::CatHandle Object that batches up all the arguments and treats them as a list of files. So to print them all out you could do.
#! /usr/bin/env perl6
use v6;
for $*ARGFILES.handles -> $IO {
$IO.path.say
}
Ok so that's a bit more convoluted than looking at @*ARGS. But what if you wanted to print the first 5 lines for each file?
#! /usr/bin/env perl6
use v6;
for $*ARGFILES.handles -> $IO {
say $IO.lines: 5;
}
Or maybe you just want to read the contents of all the files and print them out?
#! /usr/bin/env perl6
use v6;
.say for $*ARGFILES.lines;
Hope that gives you some ideas.
add a comment |
So it depends on what you want to do with the files. If you want to read through them all you might want to take a look at $*ARGFILES
this is an IO::CatHandle Object that batches up all the arguments and treats them as a list of files. So to print them all out you could do.
#! /usr/bin/env perl6
use v6;
for $*ARGFILES.handles -> $IO {
$IO.path.say
}
Ok so that's a bit more convoluted than looking at @*ARGS. But what if you wanted to print the first 5 lines for each file?
#! /usr/bin/env perl6
use v6;
for $*ARGFILES.handles -> $IO {
say $IO.lines: 5;
}
Or maybe you just want to read the contents of all the files and print them out?
#! /usr/bin/env perl6
use v6;
.say for $*ARGFILES.lines;
Hope that gives you some ideas.
So it depends on what you want to do with the files. If you want to read through them all you might want to take a look at $*ARGFILES
this is an IO::CatHandle Object that batches up all the arguments and treats them as a list of files. So to print them all out you could do.
#! /usr/bin/env perl6
use v6;
for $*ARGFILES.handles -> $IO {
$IO.path.say
}
Ok so that's a bit more convoluted than looking at @*ARGS. But what if you wanted to print the first 5 lines for each file?
#! /usr/bin/env perl6
use v6;
for $*ARGFILES.handles -> $IO {
say $IO.lines: 5;
}
Or maybe you just want to read the contents of all the files and print them out?
#! /usr/bin/env perl6
use v6;
.say for $*ARGFILES.lines;
Hope that gives you some ideas.
answered Nov 13 '18 at 9:12
Scimon
1,5541010
1,5541010
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272620%2fperl-6-iterate-through-command-line-arguments%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I would like to know what lead you to use
.perl
, so that we can prevent others from going down this path in the future.– Brad Gilbert
Nov 16 '18 at 16:07
Hey Brad, I misread the documentation here; I have a bad habit of only looking at the code: perl6maven.com/parsing-command-line-arguments-perl6
– Taylor Lee
Nov 16 '18 at 23:28
Ok. Short of asking Gabor to change that article, I suppose there's not much to do.
– Brad Gilbert
Nov 16 '18 at 23:41