Hi Francisco,
of course you could just exchange the order of the two arguments:
Always put the scalar arguments (in this case, and in particular,
the CODE reference) first and the multi-component arguments (in
this case, the hash) last. But that's perhaps ugly for your purpose.
If you *really* want to pass a code reference after other arguments,
why don't you just make the other arguments references, too?
When passing multi-component arguments in the middle of an argument
list, using HASH or ARRAY references is of a robust, standard
solution, like so:
----- 8< ----- schnipp ----- >8 ----- 8< ----- schnapp ----- >8 -----
#!/usr/bin/perl
use warnings;
use strict;
sub apply_to_hash_values ($&) {
my ($attr, $func) = @_;
die "hash required" unless ref $attr eq 'HASH';
print "$_ => ", $func->($attr->{$_}), "\n" foreach keys %$attr;
}
sub sqr {
my $x = shift;
return $x ** 2;
}
print apply_to_hash_values { two => 2, three => 3 }, \&sqr;
----- 8< ----- schnipp ----- >8 ----- 8< ----- schnapp ----- >8 -----
But for your purpose, i doubt that's the right approach.
I see no point in using code references at all. Consider:
----- 8< ----- schnipp ----- >8 ----- 8< ----- schnapp ----- >8 -----
#!/usr/bin/perl
use warnings;
use strict;
sub T($;@) {
my $tag = shift || die 'tag required';
my $attr = ref $_[0] eq 'HASH' ? shift : {};
my $text = join '', @_;
return "<$tag" . (join '', (map " $_=\"$attr->{$_}\"", keys %$attr)) .
">$text</$tag>\n";
}
print
T 'HTML', (
T 'HEAD', (
T 'TITLE', 'Greeting'
),
T 'BODY', (
T 'H1', { align => 'center' }, 'Hello World!'
),
);
----- 8< ----- schnipp ----- >8 ----- 8< ----- schnapp ----- >8 -----
Some of the other suggestions look more complicated than required.
Have fun,
Ingo
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs