Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Re: Reference to a sub return / Config::General

by Oberon (Monk)
on Jan 22, 2004 at 22:33 UTC ( #323343=note: print w/replies, xml ) Need Help??


in reply to Re: Reference to a sub return / Config::General
in thread Reference to a sub return / Config::General

> Don't worry about size unless you have to, and it sounds like you really don't.

I know, I know: don't microoptimize. But it's the principle dammit! <s>

No, I shan't worry about it. But I thought maybe there was some way around it that would teach me something new. <he said wistfully ...>

  • Comment on Re: Re: Reference to a sub return / Config::General

Replies are listed 'Best First'.
Re: Re: Re: Reference to a sub return / Config::General
by revdiablo (Prior) on Jan 22, 2004 at 22:58 UTC
    But I thought maybe there was some way around it

    If the subroutine returns a list instead of a hashref, there's not much you can do about it.

    Just for a little bit more explanation: when you say return %hash, the hash gets flattened into a plain ol' list. When you subsequently say my %hash = foo(), that shoehorns the list into a new hash. Again, there's nothing you can do about this without modifying the subroutine's return statement.

      > when you say return %hash, the hash gets flattened into a plain ol' list. When you subsequently say my %hash = foo(), that shoehorns the list into a new hash.

      Ah, I hadn't thought of it that way. So I'm not only running into the difference between a value and a variable, but also the difference between a list and an array.

      Although, come to think of it, those are basically analogous ...

        So I'm not only running into the difference between a value and a variable, but also the difference between a list and an array.

        Right. The only way you can really return an array itself (or, as previously discussed, a hash) is with a reference. Just like with return %hash, when you write return @array, the array is flattened. The elements are sucked out and put into a list. The list is returned. The caller can then grab that list and do what it wants with it; be it iterate through it, suck it into a new array, or even suck it into a hash.

        It may seem complex at first, but really it's very simple. Your data structures get dismantled unless you use references.

        Before you dismay at what happens to multi-level datastructures, keep in mind that these are only achievable in Perl via the use of references. They're perfectly safe, so things like @array = ([qw/one two/], [qw/three four/]); return @array will not blow up into tiny pieces. 8^)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2019-11-21 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (104 votes). Check out past polls.

    Notices?