develooper Front page | perl.perl5.porters | Postings from March 2008

Re: local $@ has an unwanted side effect

Thread Previous | Thread Next
From:
Tim Bunce
Date:
March 24, 2008 10:46
Subject:
Re: local $@ has an unwanted side effect
Message ID:
20080324131740.GB4448@timac.local
On Fri, Mar 21, 2008 at 12:56:12PM -0500, David Nicol wrote:
> Ths issue is that in the first case, the assignment to $@, which is
> supposed to happen by the eval, is affected by the local within
> localdie, which should have been completed before eval sets $@.

> How can this be documented?
> 
> $ diff -u  /usr/lib/perl5/5.8/pods/perlfunc.pod.orig
> /usr/lib/perl5/5.8/pods/perlfunc.pod
> --- /usr/lib/perl5/5.8/pods/perlfunc.pod.orig   2008-03-21 12:36:39.263371300 -0500
> +++ /usr/lib/perl5/5.8/pods/perlfunc.pod        2008-03-21 12:53:19.812144700 -0500
> @@ -1568,6 +1568,22 @@
>  particular situation, you can just use symbolic references instead, as
>  in case 6.
> 
> +The assignment to C<$@> occurs before restoration of localised variables,
> +which means a temporary is required to mask some but not all errors:
> +
> +    # alter $@ on nefarious repugnancy only
> +    {
> +       my $e;
> +       {
> +          local $@;  # hide outer $@
> +          eval { test_repugnancy() };
> +          # $@ =~ /nefarious/ and die $@; # DOES NOT WORK
> +          $@ =~ /nefarious/ and $e = $@;
> +       }
> +       die $e if defined $e
> +    }
> +
> +
>  C<eval BLOCK> does I<not> count as a loop, so the loop control statements
>  C<next>, C<last>, or C<redo> cannot be used to leave or restart the block.

Nobody commented on this, so I'll add a +1

Tim.

p.s. I'd also make a couple of minor changes:
Change "is required to mask" to "is required if you want to mask", and
change "# hide outer $@" to "# protect existing $@".

Thread Previous | 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