Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: I've read perlref

by maa (Pilgrim)
on Mar 11, 2004 at 11:27 UTC ( [id://335765]=note: print w/replies, xml ) Need Help??


in reply to I've read perlref

and why you can only return one value from a subroutine?

You can return as many values as you like from a sub in list context. How do you think split etc work?

why perl uses references to pass arrays?

Because it's easier... and more efficient. It's also the best way to return multple arrays from a sub... return \@a1, \@a2;

UPDATE: just noticed that you have not QUOTED your array values... and values is a perl keyword!

Replies are listed 'Best First'.
Re: I've read perlref
by Abigail-II (Bishop) on Mar 11, 2004 at 11:34 UTC
    why perl uses references to pass arrays?
    Because it's easier... and more efficient.
    Well, it's certainly not easier as you need to explicitely put a reference in (and later dereference the reference). I also doubt this would be more efficient if direct passing of arrays was available in Perl.

    The real reason is that there's no way in Perl to directly pass an array - even with a @ prototype, you're still passing a reference. Perl flattens an array into a list when used in list context. That's the reason we use references (or globs, but that's very perl4-ish) to pass arrays.

    Abigail

      yesyes, I understand the efficiency argument (copying data rather than pointing to it), but it certainly is not easier, or quicker to develop.

      And yes, you can only pass one value from a subroutine, even if that value is a list of other values. Joy. Rather than being able to directly return a int, a list, a different list I have to stuff all that into onebig list and start playing with references all over again!

      Why they want me to do this stuff in Perl and not Python I dont know....

        Did you ever try to return multiple values from a subroutine?

        sub multi { return 1,2,'a','b',3,4; }; print $_ for (multi);

        It's not as nice as Pythons way, where you can return multiple arrays without having to think about references, but it's certainly transparently possible.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://335765]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-18 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found