Returning multiple items from a sub?

Discussion in 'Perl Misc' started by robb@acm.org, Nov 19, 2006.

  1. Guest

    Hi,

    I want to write a sub that returns a list of items so that the syntax
    would like this:

    my ($thing_one, $thing_two, $thing_three) = MyPackage->my_sub();

    Is it possible to write a sub that can be used in this way? I haven't
    figured it out.

    Thanks.
     
    , Nov 19, 2006
    #1
    1. Advertising

  2. Sisyphus Guest

    <> wrote in message
    news:...
    > Hi,
    >
    > I want to write a sub that returns a list of items so that the syntax
    > would like this:
    >
    > my ($thing_one, $thing_two, $thing_three) = MyPackage->my_sub();
    >
    > Is it possible to write a sub that can be used in this way? I haven't
    > figured it out.
    >


    package MyPackage;
    use warnings;
    use strict;

    my ($thing_one, $thing_two, $thing_three) = MyPackage->my_sub();

    print $thing_one, " ", $thing_two, " ",$thing_three, "\n";

    sub my_sub {
    my @ret = (5,7,9);
    return @ret;
    }

    # or just:
    #sub my_sub{ return (6, 8, 10)}

    __END__

    Cheers,
    Rob
     
    Sisyphus, Nov 19, 2006
    #2
    1. Advertising

  3. <> wrote:


    > I want to write a sub that returns a list of items so that the syntax
    > would like this:
    >
    > my ($thing_one, $thing_two, $thing_three) = MyPackage->my_sub();



    So you want it to return a single flat list of scalars.


    > Is it possible to write a sub that can be used in this way?



    Errr, yes. All subroutines/methods in Perl return lists (when called in
    list context).


    > I haven't
    > figured it out.



    There is no "figuring" needed, there is only "reading the first
    couple paragraphs" of perlsub.pod, which is where you go to
    learn things related to subroutines in Perl.


    all functions likewise return to their caller one single flat
    list of scalars.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 19, 2006
    #3
  4. Joe Smith Guest

    wrote:

    > Is it possible to write a sub that can be used in this way? I haven't
    > figured it out.


    If you want a function/method to return several things, then
    you simple return several things. Period.

    sub return_three_things {
    ...
    return ($first,$second,$third};
    }

    What's so hard about that?
     
    Joe Smith, Nov 20, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. THY
    Replies:
    1
    Views:
    408
    Steve C. Orr, MCSD
    Aug 19, 2003
  2. Replies:
    3
    Views:
    1,022
    Kent Johnson
    Apr 22, 2005
  3. Ben
    Replies:
    2
    Views:
    938
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,132
    Lawrence D'Oliveiro
    May 20, 2011
  5. ela
    Replies:
    12
    Views:
    373
    Uri Guttman
    Apr 6, 2009
Loading...

Share This Page