15.03.2020 Views

perl-language-es

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Consumo de XML:

use strict;

use warnings;

use utf8;

package Library;

use feature qw(say);

use Carp;

use autodie;

say "Showing data information";

my $bookstore = Bookstore->new( file => './sample.xml' );

foreach my $book( @{$bookstore->books} ) {

say "ID: " . $book->bookid;

say "Title: " . $book->title;

say "Author: " . $book->author, "\n";

}

Notas:

Por favor tenga cuidado con lo siguiente:

1. La primera clase debe ser XML::Rabbit::Root . Te colocará dentro de la etiqueta principal del

documento XML. En nuestro caso nos colocará dentro de <catalog>

2. Clases anidadas que son opcionales. Es necesario acceder a esas clases a través de un

bloque try / catch (o eval / $@ check). Los campos opcionales simplemente devolverán null

. Por ejemplo, para purchase_data el bucle sería:

foreach my $book( @{$bookstore->books} ) {

say "ID: " . $book->bookid;

say "Title: " . $book->title;

say "Author: " . $book->author;

try {

say "Purchase price: ". $book->purchase_data->price, "\n";

} catch {

say "No purchase price available\n";

}

}

sample.xml

<?xml version="1.0"?>

<catalog>

<book id="bk101">

<author>Gambardella, Matthew</author>

<title>XML Developer's Guide</title>

<genre>Computer</genre>

<price>44.95</price>

<publish_date>2000-10-01</publish_date>

<description>An in-depth look at creating applications

with XML.</description>

</book>

<book id="bk102">

https://riptutorial.com/es/home 9

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!