×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

You need double quote " for command in system().

Here is my example:

open(HWIDS,"devid.txt") or die !;
while  {
    print  "About to disable the device $_,\\n");
    system("devcon disable $_");  #<--------- err line
}
close(HWIDS);
Another option, you can use ` to issue the system command and get the result back.
use strict;  #good habit to use this.
my $output;
open(HWIDS,"devid.txt") or die !;
while  {
    print  "About to disable the device $_,\\n");
    $output = `devcon disable $_`;
    print "Result:\\n", $output;
}
close(HWIDS);
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / Perl programming question
    ******************
    open (HWIDS,"devid.txt") or die !;
    while <HWIDS> {
    system(devcon disable $_); <--------- err line
    }
    close(HWIDS);
    ******************

    devcon disable is windows command for disabling hardware in system.

    i got something like "can't locate object method devcon via package disable..."

    doing my $result = `devcon disable $_` get what i want but i do not need any return...

    someone has any idea?
    • try: system("devcon disable $_");
      • I did, same error...
        • system("/bin/ls -l /var/tmp"); works on unix. don't know why your command doesn't work
          • my though
            i am not sure what i say

            i think it is because after command Devcon, there is sub-command Disable, you can't find this kind of command under unix or linux, but under windows, this is quite common.

            if i write system (dir c:), it will work because there is not sub-command after dir.

            but i still doubt that Perl can not handle this kind of command.
    • path
      • i tried, no luck.
        • u sure u can run devcon disable via cmd line without referring to the full path leading to devcon thou?
          • yes.
            i already put it in system32 et put it in same directory as perl script. if not, `devcon disable $_` will not work either, right?

            the error indicate it found devcon but can not interpret it in script.
    • I may be wrong, but you can either assign to a variable the whole command string with subcommand and argument(s), then do system($myvar); or invoke command in array mode like system("devcon", "disable", $_);
      • i did tried the first method, got same error, i will try the array mode monday. thanks.
        • if hardware id contains special chars (it usually does), be sure to escape them well such as \.
          my modem hw id is PCI\VEN_1057&DEV_5600&SUBSYS_03001436&REV_00, I can disable/enable in perl.

          F:\MyC>perl dev1.pl
          PCI\VEN_1057&DEV_5600&SUBSYS_03001436&REV_00\3&61AAA01&0&70 : Enabled on reboot
          Not all of 1 device(s) enabled, at least one requires reboot to complete the operation.
          • no luck either for array mode, but i did not escape the special char in device line. i do not understand why it works with backticks but not system function.
            • almost certain your " is overloaded.
    • You need double quote " for command in system().

      Here is my example:

      open(HWIDS,"devid.txt") or die !;
      while  {
          print  "About to disable the device $_,\\n");
          system("devcon disable $_");  #<--------- err line
      }
      close(HWIDS);
      
      Another option, you can use ` to issue the system command and get the result back.
      use strict;  #good habit to use this.
      my $output;
      open(HWIDS,"devid.txt") or die !;
      while  {
          print  "About to disable the device $_,\\n");
          $output = `devcon disable $_`;
          print "Result:\\n", $output;
      }
      close(HWIDS);
      
      • thanks for advice, i will keep backticks method to get device enable or disable.