http://www.perlmonks.org?node_id=723242


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

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.

Replies are listed 'Best First'.
Re^4: unpacking wmic command's unicode output
by ikegami (Patriarch) on Nov 12, 2008 at 19:10 UTC
    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.

        I ran your exact script and got the right result.

        >ARGVtest.pl $VAR1 = []; -1

        I think your system's handling of .pl files is broken.

        1. First, get the file type from the default key of HKEY_CLASSES_ROOT\.pl. For me, it's "Perl".
        2. Then, set the default key of HKEY_CLASSES_ROOT\Perl\shell\Open\command to "c:\perl\bin\perl.exe" "%1" %* (where "Perl" is the file type from the first step).

        As a .reg:

        Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.pl] @="Perl" [HKEY_CLASSES_ROOT\Perl] @="Perl File" [HKEY_CLASSES_ROOT\Perl\shell] [HKEY_CLASSES_ROOT\Perl\shell\Open] [HKEY_CLASSES_ROOT\Perl\shell\Open\command] @="\"c:\\perl\\bin\\perl.exe\" \"%1\" %*"