Front page | perl.perl6.language |
Postings from September 2002
Re: Regex query
From: Luke Palmer
September 21, 2002 14:03
Re: Regex query
Message ID: Pine.LNX.firstname.lastname@example.org
On 21 Sep 2002, Smylers wrote:
> Luke Palmer wrote:
> > my @v = $( &func() );
> > Would provide scalar context. But then assign it to a list...
> In the course of reading that I developed a concern about memory usage
> when trying to find the size of arrays. As I understand it the Perl 5
> syntax for discovering the number of elements in a list will still work:
> $num = @massive;
> C<$num> becomes a reference to C<@massive>, but in a numeric context it
> will evaluate to the number of elements in that array.
No. C<$num> becomes a reference to C<@massive>. If it was I<assigned> in
numeric context, it will hold the number of elements.
$num = @massive; # $num is a reference
$num = +@massive; # $num is 10_000
I think C<$num> does evaluate to the number of elements if you use it in
numeric context (in the first one), but in the second, it doesn't hold
that reference. This solves your problem.
> Suppose C<@massive> had 10_000 strings in it, and it goes out of scope
> but C<$num> stays in scope or its value is returned from a function. If
> C<$num> is just going to be used as a number then only the 10_000 needs
> to be stored in it, and the data in C<@massive> can be freed.
> But because C<$num> _might_ be used as an array ref, the data has to be
> kept around, which is wasteful.
The programmer should know whether it would or wouldn't, so he could put +
> Does that matter? This example is fairly contrived, and anybody
> actually concerned about this can always use:
> $num = @massive.length;
> So perhaps this isn't a problem.