Schrift
[thread]5756[/thread]

prototypes (try/catch)



<< >> 4 Einträge, 1 Seite
betterworld
 2003-09-24 23:39
#55989 #55989
User since
2003-08-21
2613 Artikel
ModeratorIn

user image
Hallo,
ich habe da in perlsub gelesen, wie man try-catch implementieren koennte. Ich finde das aber etwas komisch insofern, als dass da im Prototyp &@ steht, obwohl doch ueberhaupt kein Array als zweites Argument uebergeben wird. Vielmehr scheint er sich die catch-Anweisung daherzuholen... peil ich nicht.
Danke fuer Erklaerung

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
           sub try (&@) {
my($try,$catch) = @_;
eval { &$try };
if ($@) {
local $_ = $@;
&$catch;
}
}
sub catch (&) { $_[0] }

try {
die "phooey";
} catch {
/phooey/ and print "unphooey\n";
};
macMeck
 2003-09-26 06:54
#55990 #55990
User since
2003-08-04
162 Artikel
BenutzerIn
[default_avatar]
Geht's dir um das Verstaendnis des Codes oder um die Implementierung von try/catch?
Das erste ist mir im Moment auch zu hoch. Bei letzterem hilft dir vielleicht das CPAN-Modul Error weiter...

macMeck
It all works, as long as it's documented!
coax
 2003-09-26 07:37
#55991 #55991
User since
2003-08-11
457 Artikel
BenutzerIn
[default_avatar]
[quote=betterworld,24.09.2003, 21:39]Ich finde das aber etwas komisch insofern, als dass da im Prototyp &@ steht, obwohl doch ueberhaupt kein Array als zweites Argument uebergeben wird.[/quote]
Das @-Zeichen in der Prototypen-Definition steht auch nicht fuer ein Array sondern fuer eine Liste.
Eine Subroutine mit Codereferenz- und Array-Prototyp wuerde so aussehen:
Code: (dl )
1
2
3
sub (&\@) {
# ....
}


Hier mal ein Versuch einer Definition
Code (perl): (dl )
sub catch (&) { $_[0] }


Erstes Argument == Rueckgabewert, d.h. die anonyme Subroutine die catch beim Aufruf erhaelt
Code: (dl )
{ /phooey/ and print "unphooey\n"; }

wird auch gleich wieder von catch zurueck gegeben.
Code (perl): (dl )
1
2
3
4
5
6
7
8
sub try (&@) {
     my($try,$catch) = @_;
     eval { &$try };
     if ($@) {
        local $_ = $@;
        &$catch;
     }
 }

try nimmt auch als erstes Argument eine anonyme Subroutine entgegen gefolgt von einer Liste.
Die anonymous Sub ist:
Code (perl): (dl )
{ die "phooey"; }

Die Liste die try entgegen nimmt enthaelt nur den Rueckgabewert von catch (Codereferenz bzw. anonymous Subroutine).
,,Das perlt aber heute wieder...'' -- Dittsche
betterworld
 2003-09-26 16:28
#55992 #55992
User since
2003-08-21
2613 Artikel
ModeratorIn

user image
macMeck: ersteres ;)
coax: Danke, ich glaube, jetzt hab ich es verstanden.
<< >> 4 Einträge, 1 Seite



View all threads created 2003-09-24 23:39.