Returning reference to an array

Discussion in 'Perl' started by Andrew Fleet, Oct 25, 2003.

  1. Andrew Fleet

    Andrew Fleet Guest

    Hi,
    I'm looking at returning a reference to an array I create within a
    subroutine.

    I could do this...

    sub foo {
    my @theArray;

    <snip>

    return \@theArray;
    }

    This works however I'm concerned I'm returning a reference to stack data. I
    heard if I used 'local' rather than 'my' the array will always exist (it's
    global), but using 'my' I'm creating a temporary stack based
    array that will then be cleaned up when the subroutine returns, hence my
    reference will be invalid.

    So...my or local?

    Thanks

    Andy
    Andrew Fleet, Oct 25, 2003
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    "Andrew Fleet" <> wrote in
    news:Yyimb.734$:

    > I'm looking at returning a reference to an array I create within a
    > subroutine.
    >
    > I could do this...
    >
    > sub foo {
    > my @theArray;
    >
    > <snip>
    >
    > return \@theArray;
    > }
    >
    > This works however I'm concerned I'm returning a reference to stack
    > data. I heard if I used 'local' rather than 'my' the array will always
    > exist (it's global), but using 'my' I'm creating a temporary stack
    > based array that will then be cleaned up when the subroutine returns,
    > hence my reference will be invalid.
    >
    > So...my or local?


    Use 'my'. You heard wrong. Perl data structures are cleaned up when
    there are no more remaining references to them. When you declare
    @theArray with 'my', a new data structure is created. When you return
    \@theArray and presumably store that return value into a variable, there
    is still an extant reference to the array. When all references to the
    array go out of scope or are reassigned, then the array will be garbage-
    collected.

    For your future reference, comp.lang.perl is a defunct newsgroup. Please
    post general perl questions to comp.lang.perl.misc; you'll get a better
    response there.

    - --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBP5u1mWPeouIeTNHoEQIm8ACePbY4ajAWypokHBdPDyAamPdz+NcAoJSX
    kYDhlG/aEBKkK9BYsmP08671
    =6UqS
    -----END PGP SIGNATURE-----
    Eric J. Roode, Oct 26, 2003
    #2
    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. Ken
    Replies:
    3
    Views:
    9,818
    John C. Bollinger
    Jun 1, 2004
  2. PGR
    Replies:
    6
    Views:
    388
    Stefan Ram
    Jan 4, 2006
  3. Alexander Stippler
    Replies:
    2
    Views:
    611
    Alexander Stippler
    Jul 4, 2003
  4. Replies:
    11
    Views:
    650
    Christos Georgiou
    May 2, 2006
  5. JustMe
    Replies:
    1
    Views:
    165
    Tassilo v. Parseval
    Aug 29, 2003
Loading...

Share This Page