in reply to Re: Subroutine question on use of uninitialized value.
in thread Subroutine question on use of uninitialized value.

Would be possible to have it without a loop, I need to make a small xml with these values like:
my $value = "<results> <name>$args->{name}</name> <address>$args->{address}</address> ... </results> ";
I was thinking about checking right at this first line:
my ($args) = @_;

Thanks!

Replies are listed 'Best First'.
Re^3: Subroutine question on use of uninitialized value.
by toolic (Bishop) on Jun 01, 2012 at 13:58 UTC
    Would be possible to have it without a loop
    What's wrong with loops? They're low in fat, high in fiber... oh, nevermind.
    use warnings; use strict; my $info = send_values({ name => 5, address => 6, id => 7, pic => 8, }); print $info; sub send_values { my ($args) = @_; my @params = qw( name address pic id zip phone ); my $values = "<results>\n"; for (@params) { $values .= " <$_>$args->{$_}</$_>\n" if exists $args->{$_ +}; } $values .= "</results>\n"; return $values; } __END__ <results> <name>5</name> <address>6</address> <pic>8</pic> <id>7</id> </results>

    You could do the above without a loop, but it wouldn't be as scalable or as easy to maintain. For example, you would have to type 'address' 4 times instead of once:

    $values .= " <address>$args->{address}</address>\n" if ex +ists $args->{address};