in reply to Re: unpacking wmic command's unicode output
in thread unpacking wmic command's unicode output

$#ARGV is 0 whether there are zero or 1 arguments.

That's not true. $#ARGV is -1 when there are no arguments.

And by the way, I think -CS is unnecessary and even harmful. wmic probably outputs characters based on your local, so use open ':std', ':locale'; would be more appropriate.

Replies are listed 'Best First'.
Re^3: unpacking wmic command's unicode output
by goibhniu (Hermit) on Nov 12, 2008 at 18:33 UTC

    Hrmmm . . . I copied only the relevant code from my script, above:

    C:\chas_sandbox\columns-by-name> copy con ARGVtest.pl #!/usr/bin/perl -WCD use strict; use warnings; use Data::Dumper; $\ = $/; my $debug = 1; #array of fields to display my @processFields = ('Caption','ParentProcessId','ProcessId','CommandL +ine'); #ARGV processing my $searchfor = $ARGV[0] ? join(' ',@ARGV) : die("I need a process to +look for." ); ^Z 1 file(s) copied. C:\chas_sandbox\columns-by-name> notepad ARGVtest.pl

    and changed it a little (in notepad, above) and tested it:

    C:\chas_sandbox\columns-by-name> type ARGVtest.pl #!/usr/bin/perl -WCD use strict; use warnings; use Data::Dumper; $\ = $/; my $debug = 1; #array of fields to display my @processFields = ('Caption','ParentProcessId','ProcessId','CommandL +ine'); #ARGV processing my $searchfor = $#ARGV ? join(' ',@ARGV) : die("I need a process to lo +ok for."); # ^^^^^^ change here C:\chas_sandbox\columns-by-name> ARGVtest.pl I need a process to look for. at C:\chas_sandbox\columns-by-name\ARGVt +est.pl lin e 14. C:\chas_sandbox\columns-by-name> ARGVtest.pl chas I need a process to look for. at C:\chas_sandbox\columns-by-name\ARGVt +est.pl lin e 14.

    and added some debug (in notepad again) and tested again:

    C:\chas_sandbox\columns-by-name> type ARGVtest.pl #!/usr/bin/perl -WCD use strict; use warnings; use Data::Dumper; $\ = $/; my $debug = 1; #array of fields to display my @processFields = ('Caption','ParentProcessId','ProcessId','CommandL +ine'); #ARGV processing print Dumper(\@ARGV); print $#ARGV; my $searchfor = $#ARGV ? join(' ',@ARGV) : die("I need a process to lo +ok for."); C:\chas_sandbox\columns-by-name> ARGVtest.pl $VAR1 = [ '' ]; 0 I need a process to look for. at C:\chas_sandbox\columns-by-name\ARGVt +est.pl lin e 16. C:\chas_sandbox\columns-by-name> ARGVtest.pl chas $VAR1 = [ ' chas' ]; 0 I need a process to look for. at C:\chas_sandbox\columns-by-name\ARGVt +est.pl lin e 16.

    . . . and it sure looks like $#ARGV is 0 either way. I totally trust your experience, so I conclude that either my test is wrong or my conclusions are. Does the she-bang line arguments mess this up or something?


    #my sig used to say 'I humbly seek wisdom. '. Now it says:
    use strict;
    use warnings;
    I humbly seek wisdom.
      You have:
      my $searchfor = $#ARGV ? join(' ',@ARGV) : die("I need a process to lo +ok for.");
      • If there are no arg, $#ARGV is -1, $#ARGV is true, $search_for = join(' ', ()).
      • If there is one arg, $#ARGV is 0, $#ARGV is false, die("I need a process to look for.").
      • If there is two arg, $#ARGV is 1, $#ARGV is true, $search_for = join(' ', $ARGV[0], $ARGV[1]).
      • ...

      You want

      my $searchfor = $#ARGV >= 0 ? join(' ',@ARGV) : die("I need a process +to look for.");

      Or better yet:

      my $searchfor = @ARGV ? join(' ',@ARGV) : die("I need a process to loo +k for.");

      I don't understand why that's one one line. It doesn't save anything. I just adds complexity.

      @ARGV or die("I need a process to look for."); my $searchfor = join(' ',@ARGV);

        What about the print $#ARGV; statement that printed 0 where you seem to have predicted it should print -1?

        I take your point about the difference between $#ARGV (highest index) and @ARGV in scalar context (number of elements) and agree I should have used the number of elements. That's a mental block for me and has caused me problems in the past.

        I also take your point about complexity. I think I was looking for something parallel in style to open() || die(); for consistency.


        #my sig used to say 'I humbly seek wisdom. '. Now it says:
        use strict;
        use warnings;
        I humbly seek wisdom.
Re^3: unpacking wmic command's unicode output
by goibhniu (Hermit) on Nov 12, 2008 at 18:36 UTC

    Thanks and ++ for the tip about :std or :locale. It's working in place as is, so I'll file this away for the next time I'm working on it. Again, it's a one-off script, but this is a good place to document the caveats for others who might be researching similar things.


    #my sig used to say 'I humbly seek wisdom. '. Now it says:
    use strict;
    use warnings;
    I humbly seek wisdom.

      Quick note, it's not :std or :locale. They work in conjunction.