in reply to Confused Contexts and wantarray

Thanks for the comments. I'll grant that the desired operation of link is somewhat ill-conceived. And I'll probably change it. But once I discovered the contextual issues, I couldn't just let them go. Perhaps the most readable approach would be to add a size routine:
my $n = size($obj->link('next')); sub size {scalar @_}
The routine doesn't do a thing except force a list context on link and scalarize its return value. But having it there also makes clear what's intended.

<heretical rant>
In my mind size ought to have been a built-in function, rather than relying solely on implied semantics and contextual hocus-pocus to do its job. The number of newbies who try to use length for this purpose is, by itself, testament to an unmet need.
</heretical rant>

And just think of all the questions in SoPW that might've been eliminated!

Replies are listed 'Best First'.
Re^2: Confused Contexts and wantarray
by ysth (Canon) on Jan 18, 2005 at 07:34 UTC
Re^2: Confused Contexts and wantarray
by duff (Parson) on Jan 18, 2005 at 06:00 UTC
    In my mind size ought to have been a built-in function, rather than relying solely on implied semantics and contextual hocus-pocus to do its job.

    You'll get your wish in perl6 as there will be a method you can use on array objects to tell you how many items are in the array. Last I looked it was called nelems but that may have changed.

    As an aside, length is going the way of the dinosaur too. It'll be replaced by routines that are more specific and will give you the number of bytes, characters, graphemes, etc. that are contained within a scalar.