in reply to Re^2: Short invocation for per-object methods
in thread Short invocation for per-object methods

The way I did it I don't think the global scope of that global variable will ever contain an object, so I wasn't too worried about that part

You know, I think you're right about that, which makes me think this never would have worked anyway. The $self referred to by your debug method is the our $self, which never has anything in except in a local scope.

Using my obviously wouldn't work since debug wouldn't be able to get to it, and I think just omitting the local would cause re-entrancy issues. So local seemed like the only solution.

That's kind of what I was getting at: when local is the answer, it usually means you're asking the wrong question. It is very useful in a couple of specific cases though, so I can't say it shouldn't ever be used.

  • Comment on Re^3: Short invocation for per-object methods

Replies are listed 'Best First'.
Re^4: Short invocation for per-object methods
by ikegami (Patriarch) on Jan 04, 2007 at 16:37 UTC

    my provides static binding.
    local our provides dynamic binding.

    Both have their uses. The latter would allow me to set $Carp::Verbose for just one call to carp, for example.

    { local $Carp::Verbose = 1; carp(...); }

    (Mind you, not a good example, since I could just have called croak.)

    And just to show that it works:

    sub bar { print our $var, "\n"; # Prints 2 } sub foo { local our $var = 2; bar(); } our $var = 1; foo();
      I use it for file slurping (local $/) and for DBI (local $dbh->{'AutoCommit'}). The latter looks strange, but works because of the way DBI is implemented, and makes AutoCommit automatically snap back to its old value when I leave the current scope.