📄 ch19.htm
字号:
<BR>
7 PHILIPPINES Peso
24.80 1000.00 $ 40.32
<BR>
8 PAKISTAN Rupee 38.00 1200.00 $ 31.58
<BR>
9 BAHRAIN Dinar
0.38 45.00 $
118.42<BR>
10 IRAQ Dinar
0.60 10.00 $ 16.67
<BR>
11 JORDAN Dinar
0.70 100.00 $
142.86<BR>
12 SAUDIARABIA Riyal
3.75 1000.00 $
266.67<BR>
</FONT></TT>
</BLOCKQUOTE>
<P>
Notice how the value of the investment is calculated in the format
specification itself. Look at line 15 in Listing 19.6 to see how
the output fields are set up for use with the format specifiers.
Note also that in line 14 the dollar sign is printed verbatim
in the output. You can print anything you want as long as it's
not misinterpreted as a specifier.
<P>
You can even call subroutines that return values in place of variables
in the format specification. Listing 19.7 defines a function that
tells you if your funds are running too high. The function returns
a message indicating that your investment in a foreign currency
is too low or too high based on a certain criteria. The value
returned from the function is a string and therefore will be printed
as left-justified output using <TT><FONT FACE="Courier">@<<<<<<<<</FONT></TT>.
<HR>
<BLOCKQUOTE>
<B>Listing 19.7. Using subroutines in formats.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2 while(<>) {<BR>
3 ($country, $code, $currency, $value, $amount)
= split(',');<BR>
4 write;<BR>
5 }<BR>
6 format STDOUT_TOP =<BR>
7 Id Country Currency Rate Amount
Value in $<BR>
8 ============================================================
<BR>
9 .<BR>
10 format STDOUT =<BR>
11 @<<<<<<<<<<<< @<<<<<<
@#####.## @####.## $ @>>>>>>>>>>>>>>
<BR>
12 $country,$currency,$value,$amount,&checkAmount($amount,$value)
<BR>
13 .<BR>
14 sub checkAmount {<BR>
15 my ($num, $val) = @_;<BR>
16 my $dollars;<BR>
17 my $ret;<BR>
18 $dollars = $num / $val;<BR>
19 $ret = sprintf "%6.2f
", $dollars;<BR>
20 if ($dollars < 10)<BR>
{ $ret =
sprintf "%6.2f low ", $dollars; }<BR>
21 if ($dollars > 200)<BR>
{ $ret =
sprintf "%6.2f high", $dollars; }<BR>
22 $ret;<BR>
23 }</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Note the format string to the <TT><FONT FACE="Courier">sprintf</FONT></TT>
function call in line numbers 19, 20, and 21 in the code shown
in Listing 19.7. The <TT><FONT FACE="Courier">sprintf</FONT></TT>
statement is designed to return the same number of characters
regardless of the value of <TT><FONT FACE="Courier">$dollar</FONT></TT>.
The length of the <TT><FONT FACE="Courier">$ret</FONT></TT> variable
in the <TT><FONT FACE="Courier">checkAmount</FONT></TT> subroutine
is a constant. If you leave the length of <TT><FONT FACE="Courier">$ret</FONT></TT>
dependent on <TT><FONT FACE="Courier">sprintf</FONT></TT>, there
is no guarantee that the output will be assigned on a decimal
point. It's important that you return a string back from the <TT><FONT FACE="Courier">checkAmount</FONT></TT>
function and not an integer. If you return an integer value, it
won't be printed.
<P>
Here's the output from Listing 19.7.
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Id Country Currency Rate Amount
Value in $<BR>
============================================================<BR>
UK Pound
1.85 100.00 $ 54.05
<BR>
BELGIUM Franc 32.00
200.00 $ 6.25 low
<BR>
DENMARK Krone
6.00 2000.00 $
333.33 high<BR>
FINLAND Markka 4.69 1000.00 $
213.22 high<BR>
FRAncE Franc
5.28 50.00 $
9.47 low<BR>
ELSALVADOR Colon
8.74 340.00 $ 38.90
<BR>
PHILIPPINES Peso
24.80 1000.00 $ 40.32
<BR>
PAKISTAN Rupee 38.00 1200.00 $ 31.58
<BR>
BAHRAIN Dinar
0.38 45.00 $
118.42<BR>
IRAQ Dinar
0.60 10.00 $ 16.67
<BR>
JORDAN Dinar
0.70 100.00 $
142.86<BR>
SAUDIARABIA Riyal
3.75 1000.00 $
266.67 high</FONT></TT>
</BLOCKQUOTE>
<H2><A NAME="UsingMoreThanOneFormat"><FONT SIZE=5 COLOR=#FF0000>Using
More Than One Format</FONT></A></H2>
<P>
There is a standard header that uses the <TT><FONT FACE="Courier">_TOP</FONT></TT>
suffix; however, there is no <TT><FONT FACE="Courier">_END</FONT></TT>
or <TT><FONT FACE="Courier">_BOTTOM</FONT></TT> suffix to print
out when you are done. This is obvious because Perl does not know
when you want to stop. All is not lost, though, because you can
specify a different format to print out results at the end.
<P>
Listing 19.8 defines a new format name, called <TT><FONT FACE="Courier">ENDING</FONT></TT>.
First, it selects the format by setting the internal Perl variable
<TT><FONT FACE="Courier">$~</FONT></TT> to the name. Next, it
uses the format <TT><FONT FACE="Courier">ENDING</FONT></TT> to
print out the total value of the foreign currency portfolio.
<HR>
<BLOCKQUOTE>
<B>Listing 19.8. Using a different format.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2 while(<>) {<BR>
3 ($country, $code, $currency, $rate,
$amount)<BR>
4 =
split(',');<BR>
5 $sum += ($amount/$rate) ;<BR>
6 write;<BR>
7 }<BR>
8 $~ = "ENDING";<BR>
9 write;<BR>
10 format STDOUT_TOP =<BR>
11 Id Country Currency Rate Amount
Value<BR>
12 ============================================================
<BR>
13 .<BR>
14 format STDOUT =<BR>
15 @<<<<<<<<<<<< @<<<<<<
@#####.## @####.## @#####.##
<BR>
16 $country,$currency,$rate,$amount,$amount/$rate<BR>
17 .<BR>
18 format ENDING =<BR>
19 ========================================================<BR>
20 Total
Value = $@#######.###<BR>
21 $sum<BR>
22 ========================================================<BR>
23 .</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Here is the output of Listing 19.8.
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Id Country
Currency Rate Amount
Value<BR>
========================================================<BR>
UK Pound
1.85 100.00 54.05
<BR>
BELGIUM Franc 32.00
200.00 6.25
<BR>
DENMARK Krone
6.00 2000.00
333.33<BR>
FINLAND Markka 4.69 1000.00
213.22<BR>
FRAncE Franc
5.28 50.00 9.47
<BR>
ELSALVADOR Colon
8.74 340.00 38.90
<BR>
PHILIPPINES Peso
24.80 1000.00 40.32
<BR>
PAKISTAN Rupee 38.00 1200.00 31.58
<BR>
BAHRAIN Dinar
0.38 45.00
118.42<BR>
IRAQ Dinar
0.60 10.00 16.67
<BR>
JORDAN Dinar
0.70 100.00
142.86<BR>
SAUDIARABIA Riyal
3.75 1000.00
266.67<BR>
========================================================<BR>
Total
Value = $ 1271.741
<BR>
========================================================</FONT></TT>
</BLOCKQUOTE>
<H2><A NAME="ControllingtheFormat"><FONT SIZE=5 COLOR=#FF0000>Controlling
the Format</FONT></A></H2>
<P>
There are some very important internal Perl variables you must
be aware of when working with formats. By setting the values in
the following variables in a Perl script, you can control the
output of where to write to, the number of lines per page, what
to write on top of every new page and how to write every entry
and so on:<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=100><CENTER><I>Variable</I></CENTER></TD><TD WIDTH=513><CENTER><I>Description</I></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><CENTER><TT><FONT FACE="Courier">$~</FONT></TT></CENTER>
</TD><TD WIDTH=513>This variable contains the name of the default format to use. The default is <TT><FONT FACE="Courier">STDOUT</FONT></TT> if you are writing to <TT><FONT FACE="Courier">STDOUT</FONT></TT>. This is used in Listing 19.8.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><CENTER><TT><FONT FACE="Courier">$^</FONT></TT></CENTER>
</TD><TD WIDTH=513>This variable contains the default top-of-page format. Just like the <TT><FONT FACE="Courier">$~</FONT></TT> variable, you can set <TT><FONT FACE="Courier">$^</FONT></TT> to a different top-of-page format than the default. Set this value
to <TT><FONT FACE="Courier">NULL</FONT></TT> if you don't want a header printed every <TT><FONT FACE="Courier">$=</FONT></TT> lines.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><CENTER><TT><FONT FACE="Courier">$=</FONT></TT></CENTER>
</TD><TD WIDTH=513>This variable contains the number of lines to write before writing the top-of-page header. The default value is 60 lines per page.
</TD></TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -