Vraceni odkazu v perlu

Honza Pazdziora adelton na informatics.muni.cz
Úterý Březen 20 10:20:42 MET 2001


On Tue, Mar 20, 2001 at 09:05:41AM +0100, Pavel Šmerk wrote:
> 
> asi neco nechapu. Ve velbloudi knize je psano, ze my deklaruje promenne
> pouze pro nejvnitrnejsi uzavirajici blok. Nejak se mi nepodarilo najit,
> jak funguje vraceni odkazu na takto deklarovane privatni promenne v
> podprogramech. Nevim proc, ale myslel jsem si, ze bych v takovem pripade
> 
> vracel odkaz na neco, co mi behem navratu prestane existovat.
> Nasledujici prikaz nicmene vypise jednicku
> 
> perl -e 'sub a {my $a = 1; return \$a} my $a = a; print $$a'
> 
> potreboval bych vedet, jestli muzu spolehat na to, ze vraceny odkaz
> pujde vzdy dereferencovat na puvodni hodnotu privatni promenne, nebo
> jestli napriklad misto \@pole je lepsi vracet [@pole] atd. (vzdy
> samozrejme za predpokladu my @pole).

perldoc perlref:

     1.  By using the backslash operator on a variable,
         subroutine, or value.  (This works much like the &
         (address-of) operator in C.)  Note that this typically
         creates ANOTHER reference to a variable, because there's
         already a reference to the variable in the symbol table.
         But the symbol table reference might go away, and you'll
         still have the reference that the backslash returned.

perldoc perldsc:

     Surprisingly, the following dangerous-looking construct will
     actually work out fine:

         for $i (1..10) {
             my @list = somefunc($i);
             $LoL[$i] = \@list;
         }

     That's because my() is more of a run-time statement than it
     is a compile-time declaration per se.  This means that the
     my() variable is remade afresh each time through the loop.
     So even though it looks as though you stored the same
     variable reference each time, you actually did not!  This is
     a subtle distinction that can produce more efficient code at
     the risk of misleading all but the most experienced of
     programmers.  So I usually advise against teaching it to
     beginners.  In fact, except for passing arguments to
     functions, I seldom like to see the gimme-a-reference
     operator (backslash) used much at all in code.  Instead, I
     advise beginners that they (and most of the rest of us)
     should try to use the much more easily understood
     constructors [] and {} instead of relying upon lexical (or
     dynamic) scoping and hidden reference-counting to do the
     right thing behind the scenes.

Ano, je mozno to takhle pouzivat.

Pokud si nejsi jisty, proc to funguje a zda to funguje, tak to
nepouzivej a vytvarej anonymni pole/hashe. ;-)

> P.S. Vsechno, co jsem zkousel, nasvedcuje tomu, ze lze takto vracet
> odkazy a pujdou vzdy rozumne dereferencovat. V takovem pripade by me
> zajimalo, jak dlouho perl 'chrani' obsahy promennych deklarovanych
> pomoci my - jestli je to treba jako v unixu se soubory, ze by se pamet
> uvolnila, az by se na ni nikdo neodkazoval.

Perl ma ke kazdemu mistu v pameti pocet referenci, ktere se na neho
odkazuji. Pokud tento pocet klesne na nulu, tak je misto uvolneno.
Drive ne. Cili ano, Perl zajisti, ze referencovany skalar ci pole
budou stale na svem miste, pokud je nekdo referencuje.

-- 
------------------------------------------------------------------------
 Honza Pazdziora | adelton na fi.muni.cz | http://www.fi.muni.cz/~adelton/
   .project: Perl, DBI, Oracle, MySQL, auth. WWW servers, DBD::XBase.
------------------------------------------------------------------------


Další informace o konferenci Perl