Jenda Krynicky wrote:
From: Rob Dixon <[EMAIL PROTECTED]>
Jenda Krynicky wrote:
It's just a matter of one map():
#!perl
use XML::Generator;
$ref = {
'dermot' => '10',
'joe' => '17',
'rose' => '11',
'phil' => '13',
'brian' => '20',
'andy' => '15',
};
my $gen = XML::Generator->new(':pretty');
my $xml = $gen->users(
map { $gen->user({id => $ref->{$_}}, $_) } keys %{$ref}
);
print $xml;
Well yes, of course that's possible. But you're advocating abandoning
strictures and writing unintelligible code by proposing it.
Beg your pardon? use strict doesn't have any problems with that code.
What I meant was that a code fragment without 'use strict' is acceptable,
but since your program starts with
#!perl
use XML::Generator;
you are clearly posting the entire code and thereby encouraging the
omission of strictures. It's hard enough as it is to get people to use
them as a matter of course without a regular poster setting a precedent
like this.
And if you find map{} uninteligible it's your problem, not the code's.
What exactly does that mean?
If you insist on not using map{} you could of course write the code
like this:
#!perl
use strict;
use warnings;
use XML::Generator;
my $ref = {
'dermot' => '10',
'joe' => '17',
'rose' => '11',
'phil' => '13',
'brian' => '20',
'andy' => '15',
};
my $gen = XML::Generator->new(':pretty');
my @users;
foreach (keys %{$ref}) {
push @users, $gen->user({id => $ref->{$_}}, $_)
}
my $xml = $gen->users(
@users
);
print $xml;
__END__
but I do not find it any easier to read. Just the opposite.
map{} just transforms a list, what's so hard about it?
Nothing Jenda. But just imagine coming across the line
map { $gen->user({id => $ref->{$_}}, $_) } keys %{$ref}
in code you'd never seen before. Are you telling me its intent
would leap out at you instantly? I think most people would take
at least several seconds, and even then would want to reassure
themselves that their understanding was correct.
(By the way, I made the original error in replacing the OP's
<username> elements with <user>. The method name in both of our posts
needs to be changed accordingly.)
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/