in reply to is split optimized?

Is split optimized? Yes. Most of the Perl internals are very efficient.

Does split only find the first chunk when you only ask for the first chunk? No. Since you are calling split in a list context, it generates a list of all elements and assigns that to your list.

You were very careful to call split in list context, BTW. In your example, $y does not have to be in parentheses, because your right-side construct puts split in list context. Both <nobr>($y) = split()</nobr> and <nobr>$y = (split())[0]</nobr> call split in list context. You don't need both, but it certainly works as you have it.

BTW, Seekers of Perl Wisdom is the right place to ask this kind of question.

Russ
Brainbench 'Most Valuable Professional' for Perl

Replies are listed 'Best First'.
Calling split in a scalar context
by gryng (Hermit) on Jul 14, 2000 at 06:27 UTC
    If you try to call split in a scalar context you will recieve: "Use of implicit split to @_ is deprecated". The code will run, and will be slightly faster:
    Five: 13 wallclock secs (13.39 usr + 0.00 sys = 13.39 CPU) @ +5.15/s (n=69) One: 13 wallclock secs (12.92 usr + 0.01 sys = 12.93 CPU) @ 4 +.87/s (n=63)
    (relevant code:)
    One => sub { my ($y) = (split(/\s+/,$testlarge))[0]; }, Five => sub { my $y = split(/\s+/,$testlarge) }
    While a tad faster, using something besides split (such as a regex) show that split doesn't optimize away the other entries. See my other post on this thread for more benchmark results.

    Ciao,
    Gryn