in reply to Re: trying to read files in @ARGV but getting GLOB error! :(
in thread trying to read files in @ARGV but getting GLOB error! :(

Is writing data to @ARGV considered good form?

Replies are listed 'Best First'.
Re^3: trying to read files in @ARGV but getting GLOB error! (updated)
by haukex (Archbishop) on Jun 26, 2017 at 09:29 UTC

    I agree with choroba, and just wanted to add that additionally I might consider it acceptable in a longer script to use local @ARGV in a small scope (keeping in mind that this is dynamic scoping, so any function called from within that block will also see the localized values). One neat trick is to combine this with with $^I to do inplace editing of files, like with the -i switch:

    $ perl -i -ple '$_.="\n" if $. % 4 == 0; close ARGV if eof' -- FILES

    Becomes:

    { # new scope for local local *ARGV; @ARGV = @files; local $^I = ""; # -i command line switch while (<>) { print $_; print "\n" if $. % 4 == 0; close ARGV if eof; } }

    (From here.)

    Update: Fixed this potential issue in the above example code.

Re^3: trying to read files in @ARGV but getting GLOB error! :(
by choroba (Cardinal) on Jun 26, 2017 at 09:07 UTC
    I wouldn't object to it in a short script. In a large program or application, it's changing the global state, so definitely a don't.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re^3: trying to read files in @ARGV but getting GLOB error! :(
by Discipulus (Canon) on Jun 26, 2017 at 09:31 UTC
    yes, why not?

    @ARGV is something perl make at disposal for you: if you know what are you doing you can modify at your will: inserting or shifting from it.

    Rare items Perl gives you are bad to modify: some of the puncuation variables are a good example to be bad to modify. What the need to modify $. current line number for example? Or what you gain setting $^O the operating system label?

    @ARGV and many other varibles you have at disposal are useful to modify. Getopt::Long make a lot of work on @ARGV even if principally shifting it.

    But consider you want to write a simple program, possibly a oneliner, that glob files and unfortunately you are working in a user unfriendly OS that does not glob files on command line. You can use glob expanding @ARGV in a BEGIN block:

    perl -lne "BEGIN { @ARGV =  map glob, @ARGV; print qq(considering ).scalar @ARGV.qq( files\n);} ....

    Or you want to have the first file processed to be the comparing stone and the following ones searched just for lines present in the first one:

    perl -lne "%ln;BEGIN{open $f,shift;map{chomp;$ln{$_}++}<$f>}print qq($ +ARGV line\t$.\t[$_]) if exists $ln{$_};close ARGV if eof" dog.txt cat.txt other.txt

    Or, if you have enough hubrys, want to evaluate some line number range popped from @ARGV while printing just some line of a file:

    perl -E    "say+(0,<>)[eval pop]"                          linenumber.txt 1,3..5

    So, imho, modify @ARGV if you need to!

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      > What the need to modify $. current line number for example?

      Hmm, let me supersearch it for you... here it is: assign a value to $INPUT_LINE_NUMBER

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Hi, Discipulus.

      yes, why not?

      It just seemed odd to me to take an array which represents input to the script and write to it.

      It clearly can be done; that doesn't necessarily mean it should.
      However, just because it's odd doesn't necessarily mean it shouldn't.

      Thanks to you and all who provided their perspectives.