in reply to 'finally' block in Perl?

The statically scoped method is usually best. But another technique involving tied variables and local scoping is explored here: Abigail shows how to tie a variable to the cwd, then use
tie $cwd => 'main'; # the example ties in package main { local $cwd = '/some/other/directory'; # execute in /some/other/directory } # resume in original working directory

This technique will obviously work for other ties too; it is probably more appropriate when initialisation and finalisations are such paired opposites.

Replies are listed 'Best First'.
Re: Re: 'finally' block in Perl?
by John M. Dlugosz (Monsignor) on Jul 22, 2001 at 11:13 UTC
    Hmm, that means that the "poping" of the local going out of scope actually assigns the saved value back to the tied variable. I would have thought that the whole symbol is pushed somewhere and restored by changing its linkage.