⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch8.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Initialize scalar, array, and hash variables.<BR>Pass the variables to the </I><TT><I>printRef()</I></TT><I>fuNCtion. These are non-refereNCes so the undefined value shouldbe returned.<BR>Pass variable refereNCes to the </I><TT><I>printRef()</I></TT><I>fuNCtion. This is accomplished by prefixing the variable nameswith a backslash.<BR>Pass a fuNCtion refereNCe and a refereNCe to a refereNCe to the</I><TT><I>printRef()</I></TT><I>fuNCtion.<BR>Define the </I><TT><I>printRef()</I></TT><I>fuNCtion.<BR>Iterate over the parameter array.<BR>Assign the refereNCe type to </I><TT><I>$refType</I></TT><I>.<BR>If the current parameter is a refereNCe, then print its refereNCetype, otherwise, print that it's a non-refereNCe.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 8.3&nbsp;&nbsp;08LST03.PL-Using the <FONT FACE="BI Helvetica BoldOblique">ref()</FONT>FuNCtion to Determine the RefereNCe Type of a Parameter<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>$scalar = 10;@array  = (1, 2);%hash   = ( &quot;1&quot; =&gt; &quot;Davy Jones&quot; );# I added extra spaces around the parameter list# so that the backslashes are easier to see.printRef( $scalar, @array, %hash );printRef( \$scalar, \@array, \%hash );printRef( \&amp;printRef, \\$scalar );# print the refereNCe type of every parameter.sub printRef {    foreach (@_) {        $refType = ref($_);        defined($refType) ? print &quot;$refType &quot; : print(&quot;Non-refereNCe &quot;);    }    print(&quot;\n&quot;);}</PRE></BLOCKQUOTE><HR><P>This program displays:<BLOCKQUOTE><PRE>Non-refereNCe Non-refereNCe Non-refereNCeSCALAR ARRAY HASHCODE REF</PRE></BLOCKQUOTE><P>By using the <TT>ref()</TT> fuNCtionyou can protect program code that derefereNCes variables fromproducing errors when the wrong type of refereNCe is used.<H3><A NAME="ExampleCreatingaDataRecord">Example: Creating a Data Record</A></H3><P>Perl's associative arrays (hashes) are extremely useful when itcomes to storing information in a way that facilitates easy retrieval.For example, you could store customer information like this:<BLOCKQUOTE><PRE>%record = ( &quot;Name&quot;    =&gt; &quot;Jane Hathaway&quot;,            &quot;Address&quot; =&gt; &quot;123 Anylane Rd.&quot;,            &quot;Town&quot;    =&gt; &quot;AnyTown&quot;,            &quot;State&quot;   =&gt; &quot;AnyState&quot;,            &quot;Zip&quot;     =&gt; &quot;12345-1234&quot;);</PRE></BLOCKQUOTE><P>The <TT>%record</TT> associative arrayalso can be considered a <I>data record</I> with five <I>members</I>.Each member is a single item of information. The data record isa group of members that relates to a single topic. In this case,that topic is a customer address. And, a <I>database</I> is oneor more data records.<P>Each member is accessed in the record by using its name as thekey. For example, you can access the state member by saying <TT>$record{&quot;State&quot;}</TT>.In a similar manner, all of the members can be accessed.<P>Of course, a database with only one record is not very useful.By using refereNCes, you can build a multiple record array. Listing8.4 shows two records and how to initialize a database array.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Declare a data record called </I><TT><I>%recordOne</I></TT><I>as an associative array.<BR>Declare a data record called </I><TT><I>%recordTwo</I></TT><I>as an associative array.<BR>Declare an array called </I><TT><I>@database</I></TT><I>with refereNCes to the associative arrays as elements.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 8.4&nbsp;&nbsp;08LST04.PL-A Database with Two Records<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>%recordOne = ( &quot;Name&quot;    =&gt; &quot;Jane Hathaway&quot;,               &quot;Address&quot; =&gt; &quot;123 Anylane Rd.&quot;,               &quot;Town&quot;    =&gt; &quot;AnyTown&quot;,               &quot;State&quot;   =&gt; &quot;AnyState&quot;,               &quot;Zip&quot;     =&gt; &quot;12345-1234&quot;);%recordTwo = ( &quot;Name&quot;    =&gt; &quot;Kevin Hughes&quot;,               &quot;Address&quot; =&gt; &quot;123 Allways Dr.&quot;,               &quot;Town&quot;    =&gt; &quot;AnyTown&quot;,               &quot;State&quot;   =&gt; &quot;AnyState&quot;,               &quot;Zip&quot;     =&gt; &quot;12345-1234&quot;);@database = ( \%recordOne, \%recordTwo );</PRE></BLOCKQUOTE><HR><P>You can print the address member of the first record like this:<BLOCKQUOTE><PRE>print( %{$database[0]}-&gt;{&quot;Address&quot;} . &quot;\n&quot;);</PRE></BLOCKQUOTE><P>which displays:<BLOCKQUOTE><PRE>123 Anylane Rd.</PRE></BLOCKQUOTE><P>Let's dissect the derefereNCing expression in this print statement.Remember to work left to right and always evaluate brackets andparentheses first. Ignoring the <TT>print()</TT>fuNCtion and the newline, you can evaluate this line of code inthe following way:<UL><LI>The inner most bracket is <TT>[0],</TT>which means that we'll be looking at the first element of an array.<LI>The square bracket operators have a left to right associativity,so we look left for the name of the array. The name of the arrayis database.<LI>Next come the curly brackets, which tell Perl to derefereNCe.Curly brackets also have a left to right associativity, so welook left to see the refereNCe type. In this case we see a <TT>%</TT>,which means an associative array.<LI>The -&gt; is the infix derefereNCe operator. It tells Perlthat the thing being derefereNCed on the left (the database refereNCein this case) is connected to something on the right.<LI>The 'thing' on the right is the key value or &quot;Address.&quot;Notice that it is inside curly braces exactly as if a regularhash key were being used.</UL><P>The variable declaration in the above example uses three variablesto define the data's structure. We can condense the declarationdown to one variable as shown in Listing 8.5.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Declare an array called </I><TT><I>@database</I></TT><I>with two associative arrays as elements. Because the associativearrays are not being assigned directly to a variable, they areconsidered anonymous.<BR>Print the value associated with the &quot;Name&quot; key for thefirst element of the </I><TT><I>@database</I></TT><I>array.<BR>Print the value associated with the &quot;Name&quot; key for thesecond element of the </I><TT><I>@database</I></TT><I>array.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 8.5&nbsp;&nbsp;08LST05.PL-Declaring the Database Structurein One Shot <BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>@database = (<I> </I>       { &quot;Name&quot;    =&gt; &quot;Jane Hathaway&quot;,      &quot;Address&quot; =&gt; &quot;123 Anylane Rd.&quot;,      &quot;Town&quot;    =&gt; &quot;AnyTown&quot;,      &quot;State&quot;   =&gt; &quot;AnyState&quot;,      &quot;Zip&quot;     =&gt; &quot;12345-1234&quot;    },    { &quot;Name&quot;    =&gt; &quot;Kevin Hughes&quot;,      &quot;Address&quot; =&gt; &quot;123 Allways Dr.&quot;,      &quot;Town&quot;    =&gt; &quot;AnyTown&quot;,      &quot;State&quot;   =&gt; &quot;AnyState&quot;,      &quot;Zip&quot;     =&gt; &quot;12345-1234&quot;    });print(%{$database[0]}-&gt;{&quot;Name&quot;} . &quot;\n&quot;);print(%{$database[1]}-&gt;{&quot;Name&quot;} . &quot;\n&quot;);</PRE></BLOCKQUOTE><HR><P>This program displays:<BLOCKQUOTE><PRE>Jane HathawayKevin Hughes</PRE></BLOCKQUOTE><BLOCKQUOTE>Let's analyze the derefereNCing code in the first print line.</BLOCKQUOTE><UL><LI>The innermost bracket is [0], which means that we'll be lookingat the first element of an array.<LI>The square bracket operators have a left to right associativity,so we look left for the name of the array. The name of the arrayis <TT>database</TT>.<LI>Next comes the curly brackets, which tell Perl to derefereNCe.Curly brackets also have a left to right associativity, so welook left to see the refereNCe type. In this case we see a <TT>%,</TT>which means an associative array.<LI>The -&gt; is the infix derefereNCe operator. It tells Perlthat the thing being derefereNCed on the left (the <TT>database</TT>refereNCe in this case) is connected to something on the right.<LI>The 'thing' on the right is the key value or &quot;Name.&quot;Notice that it is inside curly braces exactly as if a regularhash key were being used.</UL><P>Even though the structure declarations in the last two exampleslook different, they are equivalent. You can confirm this becausethe structures are derefereNCed the same way. What's happeninghere? Perl is creating <I>anonymous </I>associative array refereNCesthat become elements of the <TT>@database</TT>array.<P>In the previous example, each hash had a name-<TT>%recordOne</TT>and <TT>%recordTwo</TT>. In the currentexample, there is no variable name directly associated with thehashes. If you use an anonymous variable in your programs, Perlautomatically will provide a refereNCe to it.<P>We can explore the coNCepts of data records a bit further usingthis basic example. So far, we've used hash refereNCes as elementsof an array. When one data type is stored inside of another datatype, this is called <I>nesting </I>data types. You can nest datatypes as often and as deeply as you would like.<P>At this stage of the example, <TT>%{$database[0]}-&gt;{&quot;Name&quot;}</TT>was used to derefereNCe the &quot;Name&quot; member of the firstrecord. This type of derefereNCing uses an array subscript totell Perl which record to look at. However, you could use an associativearray to hold the records. With an associative array, you couldlook at the records using a customer number or other id value.Listing 8.6 shows how this can be done.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Declare a hash called %</I><TT><I>database</I></TT><I>with two keys, MRD-100 and MRD-250. Each key has a refereNCe toan anonymous hash as its value.<BR>Find the refereNCe to the hash associated with the key &quot;MRD-100.&quot;Then print the value associated with the key &quot;Name&quot;inside the first hash.<BR>Find the refereNCe to the hash associated with the key &quot;MRD-250.&quot;Then print the value associated with the key &quot;Name&quot;inside the first hash.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 8.6&nbsp;&nbsp;08LST06.PL-Using an Associative Arrayto Hold the Records<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE> %database = (    &quot;MRD-100&quot; =&gt; { &quot;Name&quot;    =&gt; &quot;Jane Hathaway&quot;,                   &quot;Address&quot; =&gt; &quot;123 Anylane Rd.&quot;,                   &quot;Town&quot;    =&gt; &quot;AnyTown&quot;,                   &quot;State&quot;   =&gt; &quot;AnyState&quot;,                   &quot;Zip&quot;     =&gt; &quot;12345-1234&quot;                 },    &quot;MRD-250&quot; =&gt; { &quot;Name&quot;    =&gt; &quot;Kevin Hughes&quot;,                   &quot;Address&quot; =&gt; &quot;123 Allways Dr.&quot;,                   &quot;Town&quot;    =&gt; &quot;AnyTown&quot;,                   &quot;State&quot;   =&gt; &quot;AnyState&quot;,                   &quot;Zip&quot;     =&gt; &quot;12345-1234&quot;                 });print(%{$database{&quot;MRD-100&quot;}}-&gt;{&quot;Name&quot;} . &quot;\n&quot;);print(%{$database{&quot;MRD-250&quot;}}-&gt;{&quot;Name&quot;} . &quot;\n&quot;);</PRE></BLOCKQUOTE><HR><P>This program displays:<BLOCKQUOTE><PRE>Jane HathawayKevin Hughes</PRE></BLOCKQUOTE><P>You should be able to follow the same steps that we used previouslyto decipher the print statement in this listing. The key is thatthe associative array index is surrounded by the curly bracketsinstead of the square brackets used previously.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -