nmerriweather has asked for the wisdom of the Perl Monks concerning the following question:

i'm looking for a way to have a block of code that is used to set some value
sub block { my $setting= 0; { $setting= 1; COMMAND; $setting= 2; } return $setting; } $result= block();
in the above example, $result should equal 1. everything that i thought would work either does nothing, is invalid, or exits the block() function

i just want to exit the enclosing braces. i could likely solve this using another function, but i'd like to avoid that.

Replies are listed 'Best First'.
Re: 'block' idiom needed
by shigetsu (Hermit) on Jan 06, 2007 at 23:29 UTC
    You want to use last:
    #!/usr/bin/perl use strict; use warnings; sub block { my $var = 0; { $var = 1; last; $var = 2; } return $var; } print block();
      thats it! thank you!
Re: 'block' idiom needed
by diotalevi (Canon) on Jan 07, 2007 at 00:05 UTC

    You've just asked for 'return', haven't you?

    sub block { my $setting = 0; { return $setting = 1; } }

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      no, return will exit the sub. i just wanted to exit the braces.

        Well yes... but then you just said return $setting outside of the braces. It'd be better for locality to not escape the braces just to get to the return and instead just do the return directly.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: 'block' idiom needed
by Lod (Initiate) on Jan 07, 2007 at 18:39 UTC
    Why do you want to do this?