For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2006 > Trying to get this to run right









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Trying to get this to run right
Jockser

2006-06-25, 6:58 pm

Hello, I'm new to this as you will be able to tell from reading this. I'm
running WinXP. I've instaled Perl and also the Template Toolkit. I'm
trying to take an xml file and process it via the Template Toolkit. The
instructions for the tool kit are:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Example 10-1. Stock control data
<inventory>
<product id="0050">
<name>Basic Widget</name>
<price>49.99</price>
<stock>2500</stock>
</product>
<product id="0051">
<name>Cheap Widget</name>
<price>29.99</price>
<stock>5000</stock>
</product>
<product id="0101">
<name>Super Widget</name>
<price>99.99</price>
<stock>1000</stock>
</product>
<product id="0102">
<name>Ultra Widget</name>
<price>149.99</price>
<stock>500</stock>
</product>
</inventory>
Suppose that we want to produce a report based on this data and also want to
include the value of the stock. We can use the XML.Simple plugin to do this.
Example 10-2 shows one way that we might do it.
Example 10-2. Template to create a stock report
[% USE inventory = XML.Simple('products.xml') -%]
[% FOREACH product = inventory.product.keys.sort;
current = inventory.product.$product -%]
[% current.id %] [% product %]
[%- current.stock | format('%5d') %] units @
[%- current.price | format('%6.2f') -%] =
[%- current.stock * current.price | format('%10.2f') %]
[%- total = total + current.stock * current.price %]
[% END -%]
Total value: [% total | format('%10.2f')%]
XML.Simple is given the name of an XML document and it builds a data
structure that contains all of the data from that document. The USE
directive returns a reference to this data structure, which we can then
access using standard Template Toolkit techniques. In this case, the data
structure it builds is a multilevel hash.
At the top level, the hash has only one key, product (representing the
<product> tags from the original document). The value is a reference to
another hash. The keys in this second hash are the names of the products,
and the values are references to other hashes containing the details of the
product. We can therefore use the expression inventory.product.keys.sort to
get a list of the product names in alphabetical order.
To cut down on typing, we create a temporary variable, current, which
contains the hash representing the current product. We can then access
various parts of that hash to get the data that we want. Notice that we
calculate the value of the current stock in each product and also keep a
running total (in total) that we can display in the end. We also make use of
the format filter to ensure that all of the numbers line up neatly.
The output generated by Example 10-2 is shown in Example 10-3.

Example 10-3. Generated stock report
0050 Basic Widget 2500 units @ 49.99 = 124975.00
0051 Cheap Widget 5000 units @ 29.99 = 149950.00
0101 Super Widget 1000 units @ 99.99 = 99990.00
0102 Ultra Widget 500 units @ 149.99 = 74995.00
Total value: 449910.00

For many tasks, XML.Simple is a perfectly adequate approach, however there
will certainly be times when you need something that is a little more
sophisticated. We'll look at XML.DOM and XML.XPath later in this chapter,
but first we'll take a short detour to look at how we might create XML
documents using the Template Toolkit.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Now the silly question is how do I run this part of the code:

[% USE inventory = XML.Simple('products.xml') -%]
[% FOREACH product = inventory.product.keys.sort;
current = inventory.product.$product -%]
[% current.id %] [% product %]
[%- current.stock | format('%5d') %] units @
[%- current.price | format('%6.2f') -%] =
[%- current.stock * current.price | format('%10.2f') %]
[%- total = total + current.stock * current.price %]
[% END -%]
Total value: [% total | format('%10.2f')%]

For those familure with Template Toolkit I have tried using tpage to execute
it,
however I recieve an error that says XML.Simple plugin not found. I have
check in PPM and found I do
have XML-Simple installed. I have changed it to XML-Simple when running it
through tpage but it says:


Jockser

2006-06-25, 6:58 pm

continuation from previous post at the bottom


"Jockser" <qbert@comcast.net> wrote in message
news:QI-dnWNkb_h_QAPZnZ2dnUVZ_umdnZ2d@comcast.com...
> Hello, I'm new to this as you will be able to tell from reading this.
> I'm running WinXP. I've instaled Perl and also the Template Toolkit. I'm
> trying to take an xml file and process it via the Template Toolkit. The
> instructions for the tool kit are:
>
> /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
> Example 10-1. Stock control data
> <inventory>
> <product id="0050">
> <name>Basic Widget</name>
> <price>49.99</price>
> <stock>2500</stock>
> </product>
> <product id="0051">
> <name>Cheap Widget</name>
> <price>29.99</price>
> <stock>5000</stock>
> </product>
> <product id="0101">
> <name>Super Widget</name>
> <price>99.99</price>
> <stock>1000</stock>
> </product>
> <product id="0102">
> <name>Ultra Widget</name>
> <price>149.99</price>
> <stock>500</stock>
> </product>
> </inventory>
> Suppose that we want to produce a report based on this data and also want
> to include the value of the stock. We can use the XML.Simple plugin to do
> this. Example 10-2 shows one way that we might do it.
> Example 10-2. Template to create a stock report
> [% USE inventory = XML.Simple('products.xml') -%]
> [% FOREACH product = inventory.product.keys.sort;
> current = inventory.product.$product -%]
> [% current.id %] [% product %]
> [%- current.stock | format('%5d') %] units @
> [%- current.price | format('%6.2f') -%] =
> [%- current.stock * current.price | format('%10.2f') %]
> [%- total = total + current.stock * current.price %]
> [% END -%]
> Total value: [% total | format('%10.2f')%]
> XML.Simple is given the name of an XML document and it builds a data
> structure that contains all of the data from that document. The USE
> directive returns a reference to this data structure, which we can then
> access using standard Template Toolkit techniques. In this case, the data
> structure it builds is a multilevel hash.
> At the top level, the hash has only one key, product (representing the
> <product> tags from the original document). The value is a reference to
> another hash. The keys in this second hash are the names of the products,
> and the values are references to other hashes containing the details of
> the product. We can therefore use the expression
> inventory.product.keys.sort to get a list of the product names in
> alphabetical order.
> To cut down on typing, we create a temporary variable, current, which
> contains the hash representing the current product. We can then access
> various parts of that hash to get the data that we want. Notice that we
> calculate the value of the current stock in each product and also keep a
> running total (in total) that we can display in the end. We also make use
> of the format filter to ensure that all of the numbers line up neatly.
> The output generated by Example 10-2 is shown in Example 10-3.
>
> Example 10-3. Generated stock report
> 0050 Basic Widget 2500 units @ 49.99 = 124975.00
> 0051 Cheap Widget 5000 units @ 29.99 = 149950.00
> 0101 Super Widget 1000 units @ 99.99 = 99990.00
> 0102 Ultra Widget 500 units @ 149.99 = 74995.00
> Total value: 449910.00
>
> For many tasks, XML.Simple is a perfectly adequate approach, however there
> will certainly be times when you need something that is a little more
> sophisticated. We'll look at XML.DOM and XML.XPath later in this chapter,
> but first we'll take a short detour to look at how we might create XML
> documents using the Template Toolkit.
> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
>
> Now the silly question is how do I run this part of the code:
>
> [% USE inventory = XML.Simple('products.xml') -%]
> [% FOREACH product = inventory.product.keys.sort;
> current = inventory.product.$product -%]
> [% current.id %] [% product %]
> [%- current.stock | format('%5d') %] units @
> [%- current.price | format('%6.2f') -%] =
> [%- current.stock * current.price | format('%10.2f') %]
> [%- total = total + current.stock * current.price %]
> [% END -%]
> Total value: [% total | format('%10.2f')%]
>
> For those familure with Template Toolkit I have tried using tpage to
> execute it,
> however I recieve an error that says XML.Simple plugin not found. I have
> check in PPM and found I do
> have XML-Simple installed. I have changed it to XML-Simple when running
> it through tpage but it says:


file error - parse error - report.tt line 1: unexpected token (-)
[% USE inventory = XML-Simple('products.xml') %]

any help would be great, hopefully I'm not off my rocker on this.


THANKS


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com