|
|
The Perl script simplePerl2.pl in Listing 17.3 uses Perl 4A-style invocation of Perl functions in order to generate an SVG document that renders a rectangle. (If you're using Perl 5.x, you can remove the ampersands.)
Listing 17.3 simplePerl2.pl
&generateHeader(); &generateRectangle(0, 288, 20, 112,"fill:red"); &generateFooter(); sub generateHeader() { print "<svg>\n"; } sub generateRectangle($$$$) { my($x,$y,$width,$height,$style) = @_; print " <g transform=\"translate(50,50)\">\n"; print " <rect x=\"$x\" y=\"$y\" width=\"$width\"height=\"$height\"\n"; print " style=\"$style\"/>\n"; print " </g>\n"; } sub generateFooter() { print "</svg>\n"; }
The SVG document in Listing 17.4 is the output of the Perl script in Listing 17.3.
Listing 17.4 simplePerl2.svg
<svg> <g transform="translate(50,50)"> <rect x="0" y="288" width="20" height="112" style="fill:red"/> </g> </svg>
After you have added the directory containing the Perl executable to your PATH variable, you can generate the SVG document displayed in Listing 17.4 by invoking Perl from the command line as follows:
perl -w simplePerl2.pl >simplePerl2.svg
The Perl script in Listing 17.3 is an improvement over Listing 17.1 because it uses Perl functions to render the initial <svg> tag, the SVG rect element, and the closing SVG </svg> tag. You can obviously add more function calls in order to generate additional rectangles. For example, you could modify the Perl script in Listing 17.3 so that it adds two more rectangles as follows:
&generateHeader(); &generateRectangle(0, 288, 20, 112,"fill:red"); &generateRectangle(40, 288, 20, 112,"fill:green"); &generateRectangle(80, 288, 20, 112,"fill:blue"); &generateFooter();
The modified Perl script will generate the following output from the command line:
<svg> <g transform="translate(50,50)"> <rect x="0" y="288" width="20" height="112" style="fill:red"/> </g> <g transform="translate(50,50)"> <rect x="40" y="288" width="20" height="112" style="fill:green"/> </g> <g transform="translate(50,50)"> <rect x="80" y="288" width="20" height="112" style="fill:blue"/> </g> </svg>
|
|