tieregistry.html
来自「perl教程」· HTML 代码 · 共 1,015 行 · 第 1/5 页
HTML
1,015 行
<dd>
<p>If the hash is tied to the virtual root of the registry [or the
virtual root of a remote machine's registry], then we treat hash
key strings which start with the delimiter character specially.</p>
</dd>
<dd>
<p>If the hash key string starts with two delimiters in a row, then
those should be immediately followed by the name of a remote
machine whose registry we wish to connect to. That can be
followed by a delimiter and more subkey names, etc. If the
machine name is not following by anything, then a virtual root
for the remote machine's registry is created, a hash is tied to
it, and a reference to that hash it is returned.</p>
</dd>
</li>
<dt><strong><a name="item_hash_key_string_starts_with_the_delimiter">Hash key string starts with the delimiter</a></strong>
<dd>
<p>If the hash is tied to a virtual root key, then the leading
delimiter is ignored. It should be followed by a valid Registry
root key name [either a short-hand name like <code>"LMachine"</code>, an
<em>HKEY_*</em> value, or a numeric value]. This alternate notation is
allowed in order to be more consistant with the <a href="#item_open"><code>Open()</code></a> method
function.</p>
</dd>
<dd>
<p>For all other Registry keys, the leading delimiter indicates
that the rest of the string is a value name. The leading
delimiter is stripped and the rest of the string [which can
be empty and can contain more delimiters] is used as a value
name with no further parsing.</p>
</dd>
</li>
<dt><strong><a name="item_exact_match_with_direct_subkey_name_followed_by_de">Exact match with direct subkey name followed by delimiter</a></strong>
<dd>
<p>If you have already called the Perl <a href="../../lib/Pod/perlfunc.html#item_keys"><code>keys</code></a> function on the tied
hash [or have already called <a href="#item_membernames"><code>MemberNames</code></a> on the object] and the
hash key string exactly matches one of the strings returned, then
no further parsing is done. In other words, if the key string
exactly matches the name of a direct subkey with a delimiter
appended, then a reference to a hash tied to that subkey is
returned [but only if <a href="../../lib/Pod/perlfunc.html#item_keys"><code>keys</code></a> or <a href="#item_membernames"><code>MemberNames</code></a> has already
been called for that tied hash].</p>
</dd>
<dd>
<p>This is only important if you have selected a delimiter other than
the system default delimiter and one of the subkey names contains
the delimiter you have chosen. This rule allows you to deal with
subkeys which contain your chosen delimiter in their name as long
as you only traverse subkeys one level at a time and always
enumerate the list of members before doing so.</p>
</dd>
<dd>
<p>The main advantage of this is that Perl code which recursively
traverses a hash will work on hashes tied to Registry keys even if
a non-default delimiter has been selected.</p>
</dd>
</li>
<dt><strong><a name="item_hash_key_string_contains_two_delimiters_in_a_row">Hash key string contains two delimiters in a row</a></strong>
<dd>
<p>If the hash key string contains two [or more] delimiters in a row,
then the string is split between the first pair of delimiters.
The first part is interpreted as a subkey name or a path of subkey
names separated by delimiters and with a trailing delimiter. The
second part is interpreted as a value name with one leading
delimiter [any extra delimiters are considered part of the value
name].</p>
</dd>
</li>
<dt><strong><a name="item_hash_key_string_ends_with_a_delimiter">Hash key string ends with a delimiter</a></strong>
<dd>
<p>If the key string ends with a delimiter, then it is treated
as a subkey name or path of subkey names separated by delimiters.</p>
</dd>
</li>
<dt><strong><a name="item_hash_key_string_contains_a_delimiter">Hash key string contains a delimiter</a></strong>
<dd>
<p>If the key string contains a delimiter, then it is split after
the last delimiter. The first part is treated as a subkey name or
path of subkey names separated by delimiters. The second part
is ambiguous and is treated as outlined in the next item.</p>
</dd>
</li>
<dt><strong><a name="item_hash_key_string_contains_no_delimiters">Hash key string contains no delimiters</a></strong>
<dd>
<p>If the hash key string contains no delimiters, then it is ambiguous.</p>
</dd>
<dd>
<p>If you are reading from the hash [fetching], then we first use the
key string as a value name. If there is a value with a matching
name in the Registry key which the hash is tied to, then the value
data string [and possibly the value data type] is returned.
Otherwise, we retry by using the hash key string as a subkey name.
If there is a subkey with a matching name, then we return a
reference to a hash tied to that subkey. Otherwise we return
<a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>.</p>
</dd>
<dd>
<p>If you are writing to the hash [storing], then we use the key
string as a subkey name only if the value you are storing is a
reference to a hash value. Otherwise we use the key string as
a value name.</p>
</dd>
</li>
</dl>
<p>
</p>
<h3><a name="examples">Examples</a></h3>
<p>Here are some examples showing different ways of accessing Registry
information using references to tied hashes:</p>
<dl>
<dt><strong><a name="item_canonical_value_fetch">Canonical value fetch</a></strong>
<dd>
<pre>
<span class="variable">$tip18</span><span class="operator">=</span> <span class="variable">$Registry</span><span class="operator">-></span><span class="operator">{</span><span class="string">"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\"</span>
<span class="operator">.</span> <span class="string">'Windows\\CurrentVersion\\Explorer\\Tips\\\\18'</span><span class="operator">}</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Should return the text of important tip number 18. Note that two
backslashes, <code>"\\"</code>, are required to get a single backslash into
a Perl double-quoted or single-qouted string. Note that <code>"\\"</code>
is appended to each key name [<code>"HKEY_LOCAL_MACHINE"</code> through
<code>"Tips"</code>] and <code>"\\"</code> is prepended to the value name, <code>"18"</code>.</p>
</dd>
<dt><strong><a name="item_changing_your_delimiter">Changing your delimiter</a></strong>
<dd>
<pre>
<span class="variable">$Registry</span><span class="operator">-></span><span class="variable">Delimiter</span><span class="operator">(</span><span class="string">"/"</span><span class="operator">);</span>
<span class="variable">$tip18</span><span class="operator">=</span> <span class="variable">$Registry</span><span class="operator">-></span><span class="operator">{</span><span class="string">"HKEY_LOCAL_MACHINE/Software/Microsoft/"</span>
<span class="operator">.</span> <span class="string">'Windows/CurrentVersion/Explorer/Tips//18'</span><span class="operator">}</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>This usually makes things easier to read when working in Perl.
All remaining examples will assume the delimiter has been changed
as above.</p>
</dd>
<dt><strong><a name="item_using_intermediate_keys">Using intermediate keys</a></strong>
<dd>
<pre>
<span class="variable">$ms</span><span class="operator">=</span> <span class="variable">$Registry</span><span class="operator">-></span><span class="operator">{</span><span class="string">"LMachine/Software/Microsoft/"</span><span class="operator">}</span><span class="operator">;</span>
<span class="variable">$tips</span><span class="operator">=</span> <span class="variable">$ms</span><span class="operator">-></span><span class="operator">{</span><span class="string">"Windows/CurrentVersion/Explorer/Tips/"</span><span class="operator">}</span><span class="operator">;</span>
<span class="variable">$tip18</span><span class="operator">=</span> <span class="variable">$winlogon</span><span class="operator">-></span><span class="operator">{</span><span class="string">"/18"</span><span class="operator">}</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Same as above but opens more keys into the Registry which lets you
efficiently re-access those intermediate keys. This is slightly
less efficient if you never reuse those intermediate keys.</p>
</dd>
<dt><strong><a name="item_chaining_in_a_single_statement">Chaining in a single statement</a></strong>
<dd>
<pre>
<span class="variable">$tip18</span><span class="operator">=</span> <span class="variable">$Registry</span><span class="operator">-></span><span class="operator">{</span><span class="string">"LMachine/Software/Microsoft/"</span><span class="operator">}</span><span class="operator">-></span>
<span class="operator">{</span><span class="string">"Windows/CurrentVersion/Explorer/Tips/"</span><span class="operator">}</span><span class="operator">-></span><span class="operator">{</span><span class="string">"/18"</span><span class="operator">}</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Like above, this creates intermediate key objects then uses
them to access other data. Once this statement finishes, the
intermediate key objects are destroyed. Several handles into
the Registry are opened and closed by this statement so it is
less efficient but there are times when this will be useful.</p>
</dd>
<dt><strong><a name="item_even_less_efficient_example_of_chaining">Even less efficient example of chaining</a></strong>
<dd>
<pre>
<span class="variable">$tip18</span><span class="operator">=</span> <span class="variable">$Registry</span><span class="operator">-></span><span class="operator">{</span><span class="string">"LMachine/Software/Microsoft"</span><span class="operator">}</span><span class="operator">-></span>
<span class="operator">{</span><span class="string">"Windows/CurrentVersion/Explorer/Tips"</span><span class="operator">}</span><span class="operator">-></span><span class="operator">{</span><span class="string">"/18"</span><span class="operator">}</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Because we left off the trailing delimiters, <em>Win32::TieRegistry</em>
doesn't know whether final names, <code>"Microsoft"</code> and <code>"Tips"</code>,
are subkey names or value names. So this statement ends up
executing the same code as the next one.</p>
</dd>
<dt><strong><a name="item_what_the_above_really_does">What the above really does</a></strong>
<dd>
<pre>
<span class="variable">$tip18</span><span class="operator">=</span> <span class="variable">$Registry</span><span class="operator">-></span><span class="operator">{</span><span class="string">"LMachine/Software/"</span><span class="operator">}</span><span class="operator">-></span><span class="operator">{</span><span class="string">"Microsoft"</span><span class="operator">}</span><span class="operator">-></span>
<span class="operator">{</span><span class="string">"Windows/CurrentVersion/Explorer/"</span><span class="operator">}</span><span class="operator">-></span><span class="operator">{</span><span class="string">"Tips"</span><span class="operator">}</span><span class="operator">-></span><span class="operator">{</span><span class="string">"/18"</span><span class="operator">}</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>With more chains to go through, more temporary objects are created
and later destroyed than in our first chaining example. Also,
when <code>"Microsoft"</code> is looked up, <em>Win32::TieRegistry</em> first
tries to open it as a value and fails then tries it as a subkey.
The same is true for when it looks up <code>"Tips"</code>.</p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?