develooper Front page | perl.perl5.porters | Postings from December 2000

Can we have left side info of subs, as 'split' does? (fwd)

Thread Next
Jeff Pinyan
December 11, 2000 21:28
Can we have left side info of subs, as 'split' does? (fwd)
Message ID:
Check out this thought-provoking clpmisc post.

Perhaps wantarray should return the number of elements expected to be
returned (-1 for an array or hash).

Jeff "japhy" Pinyan
CPAN - #1 Perl Resource  (my id:  PINYAN)
PerlMonks - An Online Perl Community
The Perl Archive - Articles, Forums, etc.

---------- Forwarded message ----------
Date: Tue, 12 Dec 2000 09:50:53 +0800
From: John Lin <>
Newsgroups: comp.lang.perl.misc
Subject: Can we have left side info of subs, as 'split' does?

Dear all,

When I consulted perldoc -f split, I saw:


    When assigning to a list, if LIMIT is omitted, Perl supplies a
    LIMIT one larger than the number of variables in the list, to
    avoid unnecessary work.

    In time critical applications it behooves you not
    to split into more fields than you really need.

It means 'split' has the information of the left side of sub calling.

    ($one,$two) = split;  # 'split' knows the left side has 2 elements

Can we also do that?
The first thought that came to my mind was 'wantarray'.

    sub foo { print wantarray }
    $zero = foo;
    ($one) = foo;
    ($one,$two) = foo;
    ($one,$two,$three) = foo;
    @infinite = foo;

1111    # not 0123-1

So 'wantarray' is not the answer.

I wonder what kind of information does 'split' use.
If it is available to users, it would be great.

Is it the only way to pass the number as an argument?

    $zero = foo(0);
    ($one) = foo(1);
    @infinite = foo(-1);

Or there is other facility available?

Thank you.

John Lin

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About