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

  • SEARCH
  • Home
  • 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 7705647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:54:55+00:00 2026-05-31T23:54:55+00:00

Suppose I have a function that returns a closure: sub generator { my $resource

  • 0

Suppose I have a function that returns a closure:

sub generator
{

   my $resource = get_resource();
   my $do_thing = sub
   {
     $resource->do_something();
   }

  return $do_thing;
}

# new lexical scope
{
   my $make_something_happen = generator();

   &$make_something_happen();
}

I would like to be able to ensure that when $make_something_happen is removed from scope, I am able to call some $resource->cleanup();

Of course, if I had a class, I could do this with a destructor, but that seems a bit heavyweight for what I want to do. This isn’t really an “object” in the sense of modelling an object, it’s just a functiony thing that needs to execute some code on startup and immediately prior to death.

How would I do this in Perl( and, out of curiosity, does any language support this idea)?

  • 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. Editorial Team
    Editorial Team
    2026-05-31T23:54:58+00:00Added an answer on May 31, 2026 at 11:54 pm

    I’d just use a class for this. Bless the subroutine reference and still use it like you are. The get_resource then uses this class. Since I don’t know what that looks like, I’ll leave it up to you to integrate it:

     package Gozer {
          sub new {
             my( $class, $subref );
             bless $subref, $class;
             }
    
          sub DESTROY {
             ...; #cleanup
             }
          }
    

    If every thing can have it’s own cleanup, I’d use the class to group two code refs:

     package Gozer {
          sub new {
             my( $class, $subref, $cleanup );
             bless { run => $subref, cleanup => $cleanup }, $class;
             }
    
          sub DESTROY {
             $_[0]{cleanup}();
             }
          }
    

    In Perl, I don’t think this is heavyweight. The object system simply attaches labels to references. Not every object needs to model something, so this is a perfectly fine sort of object.

    It would be nice to have some sort of finalizers on ordinary variables, but I think those would end up being the same thing, topologically. You could do it with Perl as a tie, but that’s just an object again.

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

Sidebar

Related Questions

Suppose I have a SQL function that returns a table and I get the
Suppose I have a SQL function that returns a table and I get the
Suppose I have a function f that takes a vector v and returns a
Suppose i have a function that returns an important result and several unimportant results.
Suppose I have a function like this: def getNeighbors(vertex) which returns a list of
Suppose I have a PHP script that returns a json response. It looks something
I have C function that takes string pointer as a parameter. This functions returns
Suppose I have a function, say: >>> def foo(a): return a+1 I want to
I have a function in C that accepts and returns a double (and uses
Suppose I have a function which consults some external stateful service and returns a

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.