Generating XML Documents from Text Files

   



Generating a Simple SVG Document with Perl

Consider the red rectangle displayed in Figure 17.1.


Figure 17.1: A red rectangle.

The Perl script  simplePerl1.pl in Listing 17.1 generates an SVG document that renders a rectangle.

Listing 17.1 simplePerl1.pl

start example
print "<svg>\n"; print "  <g transform=\"translate(50,50)\">\n"; print "  <rect x=\"0\" y=\"50\"  width=\"20\"                 height=\"50\"\n"; print "        style=\"fill:red\"/>\n"; print "  </g>\n"; print "</svg>\n";
end example

The SVG document script in Listing 17.2 is the output of the Perl script in Listing 17.1.

Listing 17.2 simplePerl1.svg

start example
<svg>  <g transform="translate(50,50)">    <rect x="0" y="50"  width="20" height="50"          style="fill:red"/>  </g> </svg>
end example

Remarks

After you have added the directory containing the Perl executable to your PATH variable, you can generate the SVG document displayed in Listing 17.2 by invoking Perl from the command line as follows:

perl -w simplePerl1.pl >simplePerl1.svg

The Perl script in Listing 17.2 contains of a set of print statements that print each line of SVG code. While this is acceptable for very small SVG documents, this technique becomes cumbersome with SVG documents that are more than a page in length. A slightly better approach is demonstrated in the next example.



   



Fundamentals of SVG Programming. Concepts to Source Code
Fundamentals of SVG Programming: Concepts to Source Code (Graphics Series)
ISBN: 1584502983
EAN: 2147483647
Year: 2003
Pages: 362

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net