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 3974178
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:32:16+00:00 2026-05-20T04:32:16+00:00

If I were to have a simple tied scalar class that increments every time

  • 0

If I were to have a simple tied scalar class that increments every time it is read I could do that like this:

package Counter;

use strict;
use warnings;

sub TIESCALAR {
  my $class = shift;
  my $value = 0;

  bless \$value, $class;

  return \$value;
}

sub FETCH {
  my $self = shift;

  my $value = $$self;

  $$self++;

  return $value;
}

sub STORE {
  my $self = shift;
  $$self = shift;
}

1;

However to create a counter variable I have to use tie. I could create one counter and export it. But what I really want to do is make it look OO. It seems that I could create a new method like this:

sub new {
  my $class = shift;
  my $counter;

  tie $counter, $class;

  return $counter;
}

then in my main script get two counters by doing:

my $counter1 = Counter->new();
my $counter2 = Counter->new();

I am assuming this doesn’t work because a tie doesn’t survive a copy (I read that in the docs somewhere), is there simply no way to do this?

NB. I know it is only a matter of style, but it would LOOK more correct to the eye.

  • 1 1 Answer
  • 1 View
  • 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-20T04:32:17+00:00Added an answer on May 20, 2026 at 4:32 am

    Tie magic is not carried across assignment because it applies to the variable itself, not the value it contains. You have a few options:

    Returning a reference:

    sub new {tie my $ret, ...; \$ret}
    
    my $counter = Counter->new;
    
    say $$counter;
    

    Assigning to a glob:

    our ($counter);
    
    *counter = Counter->new; # same new as above
    
    say $counter;
    

    Or you could pass the variable into the constructor:

    sub new {my $class = shift; tie $_[0], $class}
    
    Counter->new(my $counter);
    
    say $counter;
    

    You can even make a constructor that works with both methods:

    sub new {
        my $class = shift;
        tie $_[0] => $class;
        \$_[0]
    }
    
    our $glob; *glob = Counter->new;
    
    Counter->new(my $lexical);
    

    In the last two examples, tie is passed $_[0] directly. The reason for this is that the elements of @_ are aliases to the argument list, so it works as if you had typed the my $counter in the tie line.


    And finally, while your example is very clear and follows best practices, in the spirit of TIMTOWTDI, you could write your entire class like this:

    {package Counter;
        sub TIESCALAR {bless [0]}
        sub FETCH {$_[0][0]++}
        sub STORE {$_[0][0] = $_[1]}
        sub new {tie $_[1] => $_[0]; \$_[1]}
    }
    

    One last thing to mention. While your question is about tied variables, you can also use overloading to achieve this:

    {package Counter;
        use overload fallback => 1, '""' => sub {$_[0][0]++};
        sub new {bless [0]}
    }
    
    my $counter = Counter->new;  # overloading survives the assignment
    
    say $counter;
    

    But you loose the ability to reset the counter via assignment. You could add a sub set {$_[0][0] = $_[1]} method to Counter.

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

Sidebar

Related Questions

I have a simple Django model like: class Person(models.Model): referrer = models.ForeignKey('self', null=True) ...
I have simple win service, that executes few tasks periodically. How should I pass
I have simple SSIS package which reads data from flat file and insert into
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script
I have simple SSIS package in which On Error event handler I have created
I have simple SSIS package where I import data from flat file into SQL
I have simple WinForms application where modifying Windows Registry. The problem is that in
I have a delete button that is tied to some comments on a page
I have a EF entity that is tied to a SQL table that contains
I've got a simple table that is storing unique IDs tied to a download.

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.