首先安装XML::Writer,XML::Parser等模块

1.执行/usr/bin/perl -MCPAN -e 'install "YAML"' 安装YAML模块

2.进入CPAN模式,进行下载模块操作 $ sudo perl -MCPAN -e shell

3.在CPAN模式下进行安装,nolock_cpan> install XML::Writer XML::Parser

4.Ctrl+Z退出安装

5.写入代码

$ cat writer.pl

#!/usr/bin/perl
use warnings;
use strict;

use XML::Writer;
my $writer;
$writer=new XML::Writer(DATA_MODE=>1,DATA_INDENT=>2);
$writer->xmlDecl('UTF-8','yes');
$writer->startTag('recipe','name'=>'Kulfi','banner'=>'Kulfi.png');
$writer->emptyTag('serves','number'=>'6-8');
$writer->startTag('ingredients');
item ($writer,[
    [300,'ml','Condensed Milk'],
    [360,'ml','Evaporated Milk'],
    [450,'ml','Whipping Cream'],
    [1,'tsp','Ground Almonds'],
    [1,'tsp','Pistachio Power'],
    ['1/8','tsp','Saffron']
]);
$writer->endTag('ingredients');

$writer->startTag('procedure');
step ($writer,[
    'Mix all the ingredients in a blender for 2-3 minutes.',
    'Pour the mixture into a container ,and freeze for 6-8 hoursPour the mixture into a container ,and freeze for 6-8 hours.',
    'Enjoy!']);
$writer->endTag('procedure');
$writer->endTag('recipe');
$writer->end;

exit(0);

sub item {
    my ($writer,$list)=@_;
    my ($data,$value,$unit,$item);
   
    foreach $data (@$list)    {
        $writer->startTag('item','value'=>$value,'unit'=>$unit);
        $writer->characters($item);
        $writer->endTag('item');               
    }   
}

sub step {
    my ($writer,$list)=@_;
    my $step;
   
    foreach $step (@$list) {
        $writer->dataElement('step',$step);
    }
}

6.执行 $ perl writer.pl

perl XML创建XML文件_perl