Thread Array in sub belegen, wie geht das kürzer? (8 answers)
Opened by Crian at 2005-01-28 16:49

betterworld
 2005-01-28 22:56
#51406 #51406
User since
2003-08-21
2613 Artikel
ModeratorIn

user image
[quote=Crian,28.01.2005, 16:54]Also die Variante eine Referenz auf eine lokale Variable zurückzugeben werde ich nie nie nie wählen! Das ist unter Sprachen wie C++ eine Kathastrophe, weil es Code erzeugt, der meistens funktioniert (weil der Speicher noch nicht abgesammelt wurde) und manchmal eben nicht. Sowas ist einfach ein NO NO NO. Wie das unter Perl gehandhabt wird weiß ich nicht[/quote]
Unter Perl wird das anders gehandhabt als in C. Es ist explizit erlaubt, my-Variablen per Referenz aus ihrem Scope herauszukatapultieren. Ich verwende das auch oft. Im Prinzip machen die beliebten Closures ja auch nichts anderes.

Hier ist ein Auszug aus perlsub:
Quote
Unlike local variables in C or C++, Perl's lexical variables don't nec-
essarily get recycled just because their scope has exited. If some-
thing more permanent is still aware of the lexical, it will stick
around. So long as something else references a lexical, that lexical
won't be freed--which is as it should be. You wouldn't want memory
being free until you were done using it, or kept around once you were
done. Automatic garbage collection takes care of this for you.

This means that you can pass back or save away references to lexical
variables, whereas to return a pointer to a C auto is a grave error.


Der Grund, dass es nicht fatal ist wie in C, ist imho, dass in Perl die Skalare selbst nicht im Stack liegen, sondern nur ihre Namen\n\n

<!--EDIT|betterworld|1106945882-->

View full thread Array in sub belegen, wie geht das kürzer?