vendredi 31 juillet 2015

Conditionally instantiate a new Perl array

I've got the following code:

my @departments;
if( $opts->{'d'} )
{
    @departments = @{$opts->{'d'} };
}

$opts is just a hash reference that could possibly have an array ref as the value of a key.

I'd like to do something like the following:

my @departments = $opts->{'d'} ? @{$opts->{'d'}} : undef;

But obviously that will just put one element into @departments with value undef.

The reason I'm performing this action is because I later want to be able to check

if( @departments )
{
    my $department_string = join( q{,}, @departments );
    $big_string . $department_string;
}

to dynamically add to a string.

Aucun commentaire:

Enregistrer un commentaire