in reply to Re: Temp variable performace vs Inline behavior
in thread Temp variable performace vs Inline behavior

BrowserUk’s solution:

my @devices = grep{ defined( $_ = parse_node( $_ ) ) } @nodes;

is compact and efficient, but has one side-effect: the assignment to $_ clobbers the contents of @nodes.

In cases where this is undesirable, the following seems to do the same without the side-effect:

my @devices = grep { defined } map { parse_node( $_ ) } @nodes;

Or is there a simpler way?

Athanasius <°(((><contra mundum

Replies are listed 'Best First'.
Re^3: Temp variable performace vs Inline behavior
by tobyink (Canon) on Jun 27, 2012 at 16:44 UTC

    You could use the same technique but clobber a temporary array instead...

    my @devices = grep { defined($_ = parse_node($_)) } my @tmp = @nodes;

    Or you could use map:

    my @devices = map { my $x; defined($x=parse_node($_)) ? $x : () } @nodes;

    If parse_node was written to return the empty list in cases of failure (rather than returning the scalar undef), then it would just be:

    my @devices = map { parse_node($_) } @nodes;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'