Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 168935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:32:28+00:00 2026-05-11T12:32:28+00:00

I often find it useful to be able to schedule code to be executed

  • 0

I often find it useful to be able to schedule code to be executed upon leaving the current scope. In my previous life in TCL, a friend created a function we called defer.

It enabled code like: set fp [open ‘x’] defer(‘close $fp’);

which was invoked when the current scope exited. The main benefit is that it’s always invoked no matter how/where I leave scope.

So I implemented something similar in Perl but it seems there’d be an easier way. Comments critiques welcome.

The way I did it in Perl:

  • create a global, tied variable which holds an array of subs to be executed.
  • whenever I want to schedule a fn to be invoked on exit, I use local to change the array. when I leave the current scope, Perl changes the global to the previous value because the global is tied, I know when this value change happens and can invoke the subs in the list.

The actual code is below.

Is there a better way to do this? Seems this would be a commonly needed capability.

use strict;  package tiescalar;  sub TIESCALAR {     my $class = shift;      my $self = {};     bless $self, $class;     return $self; }  sub FETCH {     my $self = shift;     return $self->{VAL}; }  sub STORE {     my $self = shift;     my $value = shift;      if (defined($self->{VAL}) && defined($value)) {     foreach my $s (@{$self->{VAL}}) { &$s; }     }     $self->{VAL} = $value; }  1;  package main;  our $h; tie($h, 'tiescalar'); $h = []; printf '1\n'; printf '2\n';  sub main {      printf '3\n';     local $h = [sub{printf '9\n'}];     push(@$h, sub {printf '10\n';});     printf '4\n';     {      local $h = [sub {printf '8\n'; }];     mysub();     printf '7\n';     return;     } }  sub mysub {     local $h = [sub {printf '6\n'; }];     print '5\n'; }  main();  printf '11\n'; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:32:29+00:00Added an answer on May 11, 2026 at 12:32 pm

    Well, your specific case is already handled if you use lexical filehandles (as opposed to the old style bareword filehandles). For other cases, you could always use the DESTROY method of an object guaranteed to go to zero references when it goes out of scope:

    #!/usr/bin/perl  use strict; use warnings;  for my $i (1 .. 5) {     my $defer = Defer::Sub->new(sub { print 'end\n' });     print 'start\n$i\n'; }  package Defer::Sub;  use Carp;  sub new {     my $class = shift;     croak '$class requires a function to call\n' unless @_;     my $self  = {         func => shift,     };     return bless $self, $class; }  sub DESTROY {      my $self = shift;     $self->{func}(); } 

    ETA: I like brian’s name better, Scope::OnExit is a much more descriptive name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 112k
  • Answers 112k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Nope. In Java that static variable only gets initialized once.… May 11, 2026 at 9:54 pm
  • Editorial Team
    Editorial Team added an answer $(document).ready(function(){. $(".alphabet a").each(function(i){ if ($(".longlist ul li a[title^="+$(this).text()+"]").length < 1){… May 11, 2026 at 9:54 pm
  • Editorial Team
    Editorial Team added an answer Basically, a surface is something that you can render to.… May 11, 2026 at 9:54 pm

Related Questions

I need a user interface that allows users to sort a table according to
How do you effectively test code that you wrote? I find that it is
I've found myself increasingly unsatisfied with the DataSet/DataTable/DataRow paradigm in .Net, mostly because it's
I'm slightly worried that this may be a duplicate, but I have searched the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.