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

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

Thread Next
From:
Jeff Pinyan
Date:
December 11, 2000 21:28
Subject:
Can we have left side info of subs, as 'split' does? (fwd)
Message ID:
Pine.GSO.4.21.0012112241330.19179-100000@crusoe.crusoe.net
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     japhy@pobox.com    http://www.pobox.com/~japhy/
CPAN - #1 Perl Resource  (my id:  PINYAN)       http://search.cpan.org/
PerlMonks - An Online Perl Community          http://www.perlmonks.com/
The Perl Archive - Articles, Forums, etc.   http://www.perlarchive.com/

---------- Forwarded message ----------
Date: Tue, 12 Dec 2000 09:50:53 +0800
From: John Lin <johnlin@chttl.com.tw>
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:

    split /PATTERN/,EXPR,LIMIT

    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.

Great!!!
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;

__END__
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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About