From jarich at perltraining.com.au Tue Mar 9 18:42:53 2010 From: jarich at perltraining.com.au (Jacinta Richardson) Date: Wed, 10 Mar 2010 13:42:53 +1100 Subject: [Canberra-pm] Programming Perl course in Canberra 19th - 23rd April Message-ID: <4B97072D.9070401@perltraining.com.au> G'day folk, Paul or I will be coming to Canberra in April to run our Programming Perl course. This course has recently been expanded to cover documentation, testing, starting modules with module-starter and a few other more advanced ideas. If you or a colleague would like to book on this course, we'd be very happy to give all Canberra Perl Mongers a 5% ($150) discount OR a free Perl book of your choice. Book before Friday this week and you could therefore get *2* free books (or the discount and 1 free book). Convince your colleague to say that you referred them (when booking by Friday), and you could get a free book in addition to the specials they receive. Early Bird: Friday 12th March (this Friday) Course Dates: 19th - 23rd April http://perltraining.com.au/bookings/Canberra.html This 5 day course covers Perl's fundamentals to give an existing programmer sufficient knowledge of Perl to work on most projects. Specifically it covers: * Perl's variable types: scalars, arrays, hashes * Operators and functions * Conditional and looping structures * Subroutines * Advanced argument passing * Regular expressions * Writing and using modules (libraries) * module-starter * Testing your module * Perl objects * References and complex data structures * System interaction and security issues We would love to attend a Canberra Perl Monger meeting during this week as well. Do you have a preferred evening you want to suggest? All the best, J -- ("`-''-/").___..--''"`-._ | Jacinta Richardson | `6_ 6 ) `-. ( ).`-.__.`) | Perl Training Australia | (_Y_.)' ._ ) `._ `. ``-..-' | +61 3 9354 6001 | _..`--'_..-_/ /--'_.' ,' | contact at perltraining.com.au | (il),-'' (li),' ((!.-' | www.perltraining.com.au | From John.Hockaday at ga.gov.au Wed Mar 17 18:56:58 2010 From: John.Hockaday at ga.gov.au (John.Hockaday at ga.gov.au) Date: Thu, 18 Mar 2010 12:56:58 +1100 Subject: [Canberra-pm] Parsing an XML document using XML::Twig and using it on an Template [SEC=UNCLASSIFIED] In-Reply-To: <4B97072D.9070401@perltraining.com.au> References: <4B97072D.9070401@perltraining.com.au> Message-ID: <8B2245497B7F9348B262E7DF858E0B72337F4AA009@EXCCR01.agso.gov.au> Hi All, I am trying to read an XML document into a hash handler that can be used on the Template package. I thought that XML::Twig would be good for this but I have no idea how to create a hash/array "thingy" that can be passed to the Template module. Can someone help with this? I don't understand the hash/array structure created by the XML::Twig and for the input to the Template. Here is some idea of the code: use XML::Twig; use Template; my ($inputFile) = 'bob.xml'; my ($templatePath) = '/public/asddadm/test'; my ($templateFile) = 'temp.xml'; my ($output) = 'output.xml'; my $twig= new XML::Twig; $twig->parsefile( $inputFile); # build the twig my $root = $twig->root; $root->print => 'indented', # output will be nicely form my $temp = Template->new({'INCLUDE_PATH' => "$templatePath", 'TAG_STYLE' => 'html', 'PRE_CHOMP' => '1', 'ABSOLUTE' => '1',}) || die $Template::ERROR, "\n"; # process the template object no strict "refs"; $temp->process($templateFile, $root, $output) || die $temp->error(); exit (0); Thanks. John From suter at zwitterion.org Thu Mar 18 00:58:03 2010 From: suter at zwitterion.org (Mark Suter) Date: Thu, 18 Mar 2010 17:58:03 +1000 Subject: [Canberra-pm] Parsing an XML document using XML::Twig and using it on an Template In-Reply-To: <8B2245497B7F9348B262E7DF858E0B72337F4AA009@EXCCR01.agso.gov.au> References: <4B97072D.9070401@perltraining.com.au> <8B2245497B7F9348B262E7DF858E0B72337F4AA009@EXCCR01.agso.gov.au> Message-ID: <20100318075803.GO30634@zwitterion.humbug.org.au> John, > I am trying to read an XML document into a hash handler that can be > used on the Template package. I thought that XML::Twig would be good > for this but I have no idea how to create a hash/array "thingy" that > can be passed to the Template module. Can someone help with this? I > don't understand the hash/array structure created by the XML::Twig and > for the input to the Template. The code below works for me. I've run this email using perl -x and it produces this output: Dumper: $VAR1 = {'thing' => [{'name' => 'Perl','rating' => 'Awesome'},{'name' => 'Python','rating' => 'Awesome'}]}; Template: Perl is Awesome. Python is Awesome. I wouldn't recommend this coding style normally and it lacks a lot of the normal error checking; however, it looks cool ;) -- Mark Suter http://zwitterion.org/ | I have often regretted my | speech, never my silence. mobile 0411 262 316 - gpg 2C71D63D | Xenocrates (396-314 B.C.) ----------------------------- perl -x ----------------------------- #!/usr/bin/perl # [MJS 18 Mar 2010] XML::Twig -> Template demo use strict; use warnings; use XML::Twig; use Template; use Data::Dumper; ## Contrived sample XML ;) my $xml = <<'EOF' ; PerlAwesome PythonAwesome EOF ## Contrived sample template ;) my $template = <<'EOF' ; Dumper: [% dump %] Template: [% FOREACH thing IN thing -%] [% thing.name %] is [% thing.rating %]. [% END -%] EOF ## Parse the XML into a simple data structure my $data = XML::Twig->parse($xml)->simplify(); ## Add a text dump of that structure to itself $data->{dump} = Data::Dumper->new( [$data] )->Indent(0)->Dump(); ## Template it Template->new()->process( \$template, $data ); __END__ From John.Hockaday at ga.gov.au Mon Mar 22 16:03:45 2010 From: John.Hockaday at ga.gov.au (John.Hockaday at ga.gov.au) Date: Tue, 23 Mar 2010 10:03:45 +1100 Subject: [Canberra-pm] Parsing an XML document using XML::Twig and using it on an Template [SEC=UNCLASSIFIED] In-Reply-To: <20100318075803.GO30634@zwitterion.humbug.org.au> References: <4B97072D.9070401@perltraining.com.au> <8B2245497B7F9348B262E7DF858E0B72337F4AA009@EXCCR01.agso.gov.au> <20100318075803.GO30634@zwitterion.humbug.org.au> Message-ID: <8B2245497B7F9348B262E7DF858E0B72337F4AA019@EXCCR01.agso.gov.au> Many thanks to those who replied to me both on and off the list. I eventually came up with a solution. The problem was, as suggested, that Twig uses objects and Template uses complex hash arrays and arrays for the data. I couldn't find a method that converts the Twig object into an array so I had to write a sub routine that did that and call it from the twig_roots method. Its not pretty but it does what I want. Here is the relevant code: ... my (%hash, @names); my $twig = XML::Twig->new ( twig_roots => { 'title' => \&pushOntoArray, 'identifier' => \&pushOntoArray, 'node-description' => \&pushOntoArray, 'hostname' => \&pushOntoArray, 'port' => \&pushOntoArray, 'repository-name' => \&pushOntoArray, ... 'contact/contact-fax' => \&pushOntoArray, } ); $twig->parsefile( $inputFile ); push @names, $rec; my ($arrayRef) = \@names; %hash = ('servers' => $arrayRef); $hashRef = \%hash; my $temp = Template->new({'INCLUDE_PATH' => "$templatePath", 'TAG_STYLE' => 'html', 'PRE_CHOMP' => '1', 'ABSOLUTE' => '1',}) || die $Template::ERROR, "\n"; # process the template object no strict "refs"; $temp->process($templateFile, $hashRef, $output) || die $temp->error(); ################################ sub pushOntoArray { my ($t, $name) = @_; my ($value) = $name->name; $value =~ s{-}{}gxm; my ($content) = $name->text; $$rec{$value} = $content; $t->purge; print "$value, $content\n"; } Thanks again for all your help. John > -----Original Message----- > From: Mark Suter [mailto:suter at zwitterion.org] > Sent: Thursday, 18 March 2010 6:58 PM > To: Canberra Perl Mongers > Subject: Re: Parsing an XML document using XML::Twig and > using it on an Template > > John, > > > I am trying to read an XML document into a hash handler that can be > > used on the Template package. I thought that XML::Twig > would be good > > for this but I have no idea how to create a hash/array "thingy" that > > can be passed to the Template module. Can someone help with this? I > > don't understand the hash/array structure created by the > XML::Twig and > > for the input to the Template. > > The code below works for me. I've run this email using perl -x > and it produces this output: > > Dumper: > $VAR1 = {'thing' => [{'name' => 'Perl','rating' => > 'Awesome'},{'name' => 'Python','rating' => 'Awesome'}]}; > Template: > Perl is Awesome. > Python is Awesome. > > I wouldn't recommend this coding style normally and it lacks a > lot of the normal error checking; however, it looks cool ;) > > -- > Mark Suter http://zwitterion.org/ | I have often regretted my > | speech, never my silence. > mobile 0411 262 316 - gpg 2C71D63D | Xenocrates (396-314 B.C.) > > ----------------------------- perl -x ----------------------------- > #!/usr/bin/perl > # [MJS 18 Mar 2010] XML::Twig -> Template demo > > use strict; > use warnings; > use XML::Twig; > use Template; > use Data::Dumper; > > ## Contrived sample XML ;) > my $xml = <<'EOF' ; > > > PerlAwesome > PythonAwesome > > EOF > > ## Contrived sample template ;) > my $template = <<'EOF' ; > Dumper: > [% dump %] > Template: > [% FOREACH thing IN thing -%] > [% thing.name %] is [% thing.rating %]. > [% END -%] > EOF > > ## Parse the XML into a simple data structure > my $data = XML::Twig->parse($xml)->simplify(); > > ## Add a text dump of that structure to itself > $data->{dump} = Data::Dumper->new( [$data] )->Indent(0)->Dump(); > > ## Template it > Template->new()->process( \$template, $data ); > > __END__ > From John.Hockaday at ga.gov.au Mon Mar 22 20:14:02 2010 From: John.Hockaday at ga.gov.au (John.Hockaday at ga.gov.au) Date: Tue, 23 Mar 2010 14:14:02 +1100 Subject: [Canberra-pm] Parsing an XML document using XML::Twig and using it on an Template [SEC=UNCLASSIFIED] In-Reply-To: <8B2245497B7F9348B262E7DF858E0B72337F4AA019@EXCCR01.agso.gov.au> References: <4B97072D.9070401@perltraining.com.au> <8B2245497B7F9348B262E7DF858E0B72337F4AA009@EXCCR01.agso.gov.au> <20100318075803.GO30634@zwitterion.humbug.org.au> <8B2245497B7F9348B262E7DF858E0B72337F4AA019@EXCCR01.agso.gov.au> Message-ID: <8B2245497B7F9348B262E7DF858E0B72337F4AA01C@EXCCR01.agso.gov.au> Hi List, I'm still caught. For three days I have been trying to get the content of an XML element into a variable using XML::Twig. All the tutorials and examples don't show how to do this. All I want to do is: my ($uuid); my ($input) = 'bob.xml'; my ($t) = new XML::Twig(twig_roots => {'gmd:fileIdentifier/gco:CharacterString' => '1'},); $uuid = $t->parsefile($input); print "uuid = ", $uuid, "\n"; All this gives me is: print out to STD of the content that I want and "uuid = XML::Twig=HASH(0x67d2e8)" Can anyone please tell me the simple way to get the content of an XML element into a variable? Thanks. John