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

I'm trying to add a value to an existing subkey. I tried what was posted when I searched but did not work. Is there something that I overlooked in my code.
#!perl -w # DESCRIPTION # perl script: Finds the repo version number from the *snapshot.snp fi +le. # # --- # main( ) # { #use Win32::TieRegistry; use Win32::TieRegistry ( Delimiter=>"/" ); $PVFDRepo = "c:\\content\\PV\\FD\\PVFDRepoSnapshot.snp"; $FCFDRepo = "c:\\content\\FC\\FD\\FCFDRepoSnapshot.snp"; $RepoFile = "c:\\content\\RepoFile.txt"; $keyFed = $Registry->{"HKEY_LOCAL_MACHINE/SOFTWARE/AACTS/2001/Cont +ent Build/Federal Compliance"}; $keyGlobal = $Registry->{"HKEY_LOCAL_MACHINE/SOFTWARE/AACTS/2001/C +ontent Build/Global Provision"}; for( $count=1; $count<=2; $count++ ) { $found = 0; if( $count eq 1 ) { $Filename = $PVFDRepo; $TagName = "Global Provision"; $key = $keyGlobal; } else { $Filename = $FCFDRepo; $TagName = "Federal Compliance"; $key = $keyFed; } open( FILE, "<$Filename" ) || die "can't open file $Filename: +$!\n"; @filelines = <FILE>; foreach $fileline( @filelines ) { chop( $fileline ); if( $fileline =~ "<repo_version>(.*)</repo_version>" && $ +found eq 0 ) { $key{'/Repo Version'} = "$1"; $found = 1; } } close( FILE ); } # }

Replies are listed 'Best First'.
Re: Adding values to registry
by graff (Chancellor) on Aug 04, 2002 at 02:29 UTC
    The previous reply contained good advice. I would add the following observation, about the variable "$key" in your code:

    It is initially assigned as a scalar variable:

    ... if( $count eq 1 ) { ... $key = $keyGlobal; } else { ... $key = $keyFed; } ...

    Then, later on, the same variable name ("key") is used as a hash:

    if( $fileline =~ "..." && $found eq 0 ) { $key{'/Repo Version'} = "$1"; $found = 1; }

    If the initial assignment to "$key" was intended to be a reference to a hash, then assigning a value to an element of the hash requires the use of a de-reference operator, like this:

    $key->{'/Repo Version'} = $1;
    Since your original code did not use de-reference, your assignment of "$1" to "$key{'/Repo Version'} caused a new hash array to be declared at that point -- note that when the same name is used for a scalar and a hash (and/or an array), Perl treats the scalar and the hash (and/or the array) as different storage locations, whose values are completely distinct and unrelated.
Re: Adding values to registry
by Courage (Parson) on Aug 02, 2002 at 19:19 UTC
    your code should work, as works mine in such situations

    I can advice you following:

    • It is suspicious to "chop" each line, much better to "chomp" it, because, if last line of a file does not contain newline, your code will damage data.
    • Just right before assigning into registry by "$key{...}=..." perform debugging printout of key and value information and post it here again, I think this will help solving your problem
    • (this will not affect script behaviour here, but anyways, this could affect somewhere else) why do you assign a numeric value to a variable but then check it for string equality by "eq" operator?

    Courage, the Cowardly Dog.