ElAlx has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I'm writing a script which could automatically open a specific version of programm by using the path in windows registry using Win32::TieRegistry. How can i place a variable into the string so that will work?
use Win32::TieRegistry (Delimiter => '/'); my $version=2.1; my $mykey = $Registry->{'HKEY_LOCAL_MACHINE/Software/SPS/$version'}; my $mykeyval = $mykey->{'/AppsDir'}; $mykeyval2="$mykeyval"."\\start.exe"; system("$mykeyval2");
I tried to find something for my problem but with no luck. I would appreciate your help! Thank you!

Replies are listed 'Best First'.
Re: Open an application from windows registry
by poj (Abbot) on Feb 26, 2017 at 15:07 UTC

    Have you tried running the code as administrator ?

    Update : Change this line to use double quotes - see Quote-Like-Operators

    my $mykey = $Registry->{"HKEY_LOCAL_MACHINE/Software/SPS/$version"}

    poj
      Hello, if i sepcifiy the path in registry without using the content of $version it works. I tryied with "" and running it as admin, it makes unfortunately no difference.

        Single quotes don't interpolate variables. Double quotes do.

        use Win32::TieRegistry (Delimiter => '/'); my $version=2.1; my $key_single = 'HKEY_LOCAL_MACHINE/Software/SPS/$version'; my $key_double = "HKEY_LOCAL_MACHINE/Software/SPS/$version"; print "Single quotes: <$key_single>\n"; print "Double quotes: <$key_double>\n";

        See perlop

        edit: nevermind, it had typos and got me confused, i guess i should be asleep

        use Win32::TieRegistry (Delimiter => '/'); my $version=2000; { my $mykey0 = $Registry->{"HKEY_LOCAL_MACHINE/SOFTWARE/ActiveState/Acti +vePerl/$version"}; print 'MK0:'.$mykey0."\n"; } { my $mykey2 = $Registry->{"HKEY_LOCAL_MACHINE/Software/ActiveState/Acti +vePerl/$version"}; print 'MK2:'.$mykey2."\n"; } { my $mykey = $Registry->{"HKEY_LOCAL_MACHINE/SOFTWARE/ActiveState/Activ +ePerl/$version/"}; print 'MK1:'.$mykey."\n"; my $mykeyval = $mykey->{'Help//'}; print 'MV1:'.$mykeyval."\n"; } { my $part='TroubleShooters'; my $mykey = $Registry->{"HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/DeviceM +anager/$part/"}; print 'ms1:'.$mykey."\n"; my $mykeyval = $mykey->{'/TroubleShooter-3'}; print 'ms2:'.$mykeyval."\n"; } __END__ my $version='2.1'; my $mykey = $Registry->{"HKEY_LOCAL_MACHINE/SOFTWARE/SPS/$version/"}; my $mykeyval = $mykey->{'/AppsDir'}; $mykeyval2="$mykeyval"."\\start.exe"; system("$mykeyval2");
        This works just fine, no caps, no slash needed at end, does need double quotes.


        this is original with typos

        use Win32::TieRegistry (Delimiter => '/'); my $version=2000; { my $mykey0 = $Registry->{"HKEY_LOCAL_MACHINE/SOFTWARE/ActiveState/Acti +vePerl/$version"}; print 'MK0:'.$mykey."\n"; } { my $mykey2 = $Registry->{"HKEY_LOCAL_MACHINE/Software/ActiveState/Acti +vePerl/$version"}; print 'MK2:'.$mykey."\n"; } { my $mykey = $Registry->{"HKEY_LOCAL_MACHINE/SOFTWARE/ActiveState/Activ +ePerl/$version/"}; print 'MK1:'.$mykey."\n"; my $mykeyval = $mykey->{'Help//'}; print 'MV1:'.$mykeyval."\n"; } { my $part='TroubleShooters'; my $mykey = $Registry->{"HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/DeviceM +anager/$part/"}; print 'ms1:'.$mykey."\n"; my $mykeyval = $mykey->{'/TroubleShooter-3'}; print 'ms2:'.$mykeyval."\n"; } __END__ my $version='2.1'; my $mykey = $Registry->{"HKEY_LOCAL_MACHINE/SOFTWARE/SPS/$version/"}; my $mykeyval = $mykey->{'/AppsDir'}; $mykeyval2="$mykeyval"."\\start.exe"; system("$mykeyval2");
        Result
        MK0: MK2: MK1:Win32::TieRegistry=HASH(0xb6e354) MV1:C:\Perl\html\index.html ms1:Win32::TieRegistry=HASH(0xa3ec64) ms2:hcp://help/tshoot/Err_Hardw_Error3.htm
        First you need the double quotes,
        Second SOFTWARE is all caps,
        Third "subkey-name" has to end in a slash (delimiter).

        http://search.cpan.org/~adamk/Win32-TieRegistry-0.26/lib/Win32/TieRegistry.pm
        "Hash key string ends with a delimiter
        If the key string ends with a delimiter, then it is treated as a subkey name or path of subkey names separated by delimiters."

        what i think should work for you is below the __END__, i cant test it 'cuz i dont have SPS

Re: Open an application from windows registry
by Lotus1 (Vicar) on Feb 26, 2017 at 16:51 UTC

    According to the Examples section at the documentation for Win32::TieRegistry you can use or die() statements when fetching the key values. It can give you good clues to what has happened and where the program has failed. Also, printing out the value of $mykeyval2 before the system() function attempts to run it is a good idea for debugging.

    $diskKey= $Registry->{"LMachine/System/Disk/"} or die "Can't read LMachine/System/Disk key: $^E\n"; $data= $key->{"/Information"} or die "Can't read LMachine/System/Disk//Information value: $^E\n +";