Perl 6 iterate through command line arguments












8














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.










share|improve this question






















  • 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
















8














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.










share|improve this question






















  • 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














8












8








8







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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












2 Answers
2






active

oldest

votes


















8














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






share|improve this answer























  • You don't need the use strict; there :)
    – Scimon
    Nov 13 '18 at 9:04






  • 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





















5














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.






share|improve this answer





















    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    8














    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






    share|improve this answer























    • You don't need the use strict; there :)
      – Scimon
      Nov 13 '18 at 9:04






    • 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


















    8














    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






    share|improve this answer























    • You don't need the use strict; there :)
      – Scimon
      Nov 13 '18 at 9:04






    • 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
















    8












    8








    8






    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






    share|improve this answer














    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 21:32

























    answered Nov 13 '18 at 2:14









    Kingsley

    2,26011023




    2,26011023












    • You don't need the use strict; there :)
      – Scimon
      Nov 13 '18 at 9:04






    • 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




















    • You don't need the use strict; there :)
      – Scimon
      Nov 13 '18 at 9:04






    • 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


















    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















    5














    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.






    share|improve this answer


























      5














      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.






      share|improve this answer
























        5












        5








        5






        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 9:12









        Scimon

        1,5541010




        1,5541010






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python