develooper Front page | perl.perl5.porters | Postings from July 2020

try/catch and CLEANUP/FINALLY/&c.

Thread Next
From:
Felipe Gasper
Date:
July 16, 2020 12:48
Subject:
try/catch and CLEANUP/FINALLY/&c.
Message ID:
D64B9DEC-11F1-4BA5-8A3C-C73E2DE95989@felipegasper.com
My understanding is that CLEANUP (“FINALLY”?) blocks essentially build into the language the functionality of modules like Scope::Guard, but without the Perl-level guard object.

The well-known try/catch/finally pattern would then look thus:

{
	CLEANUP { print 3 }

	try {
		print 1;
		die 'oh no';
	}
	catch {
		print 2;
	}
}

To me--accustomed as I am to JS, Try::Tiny, &c.--this seems rather clunky. The extra code block is ugly, and the execution path doesn’t match the code layout. Compare to:

	try {
		print 1;
		die 'oh no';
	}
	catch {
		print 2;
	}
	finally {
		print 3;
	}

Even for the more general case of “cleanup” logic, I’m not sure I find this:

	CLEANUP { ... }

… more natural than the Scope::Guard pattern, where I can easily control both when the callback fires and whether to cancel it.

How devoted are folks to the idea that a CLEANUP { .. } block will suffice as the means to implement try/catch/finally? For what it’s worth, I respectfully hope not overly so.

cheers,
-Felipe
Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About