Fixing warnings 'n other stuff

Ovid on 2003-11-04T00:57:37

:(

sub convert_to_list {
    my $self = shift;
    my @args = split / -/, $self->arguments;

    # Special case, since map does x for each element and we don't want to add a - to the first element (because of split).
    my $first_arg = shift @args;
    
    @args = map { '-' . $_ } @args;
    unshift @args, $first_arg;

    return @args;
}

:)

sub convert_to_list {
    my $self = shift;
    return () unless $self->arguments;
    return split / (?=-)/ $self->arguments;
}

I love Perl :)


:-D

Theory on 2003-11-04T01:13:49

sub convert_to_list {
    my $self = shift;
    # Return knows if it's a scalar or array context!
    my $args = $self->arguments or return;
    return split / (?=-)/, $args;
}

Re::-/

Ovid on 2003-11-04T04:20:45

In theory, that looks good and if I were calling the shots, it would be. However, consider the following:

some_func(other_func1(), other_func2());

If one of the "other funcs" has a bare return, some_func() will be passed a list with only one element! You can get around this by prepending the function calls with scalar, but that is not done in the shop I work at, yet function calls are often embedded in other function calls. Thus, we have explicit return values. If only one value is being returned, we return undef (to my dismay).

That means we get tripped up going the other direction:

sub some_func {
    return undef unless ($some_condition);
}

my @results = some_func($value);
if (@results) { # wrong!

That fails because we have a array with one undef element, which in scalar context evaluates as true. In this respect, it's a backwards coding style they have here, but I'm trying to follow it.

zero

bart on 2003-11-04T16:42:30

So $self->arguments returning "0" needs to be ignored?

Re:zero

Ovid on 2003-11-04T17:17:08

Um, err, (shuffles feet and looks away nervously).

Nice catch :)