http://qs321.pair.com?node_id=180148

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

Is it possible to alter variables in a caller()'s namespace?

I'm trying to write a common subroutine that will check authentication data (from %fdat) and control redirects (via %http_headers_out) This is a HTML::Embperl page, btw. I'd like to be able to just put in code similar to:

[- $user = Common->checkauth() -]

Where checkauth will check the paramaters in %fdat, and if they match up, return a new logged-in user object. if they don't match up, send a location header, and exit() (which is handled specially by embperl

There's no reason why I coulden't just pass in %fdat & %http_headers_out, but I was just curious as if this way was possible solution

Thanks in advance

Replies are listed 'Best First'.
Re: Caller's Variables
by Abigail-II (Bishop) on Jul 08, 2002 at 12:29 UTC
    There are two types of variables you can modify in your callers namespace:
    • All package variables (well, technically, package variables are the only variables with a "namespace").
    • All lexical variables that share the same scope as your subroutine.
    That is, lexical variables that don't share a scope are unreachable. And that's a good thing, that's why you have lexical variables in the first place.

    Abigail

      That is, lexical variables that don't share a scope are unreachable.
      Not strictly true since you can modify lexical variables with the help of PadWalker. Here's some code from the docs
      sub increment_my_x { my $h = peek_my (1); ${$h->{'$x'}}++; } my $x=5; increment_my_x; print $x; # prints 6
      Although you'd have to be quite a disturbed individual to use this sort of thing in production any code ;-)
      HTH

      _________
      broquaint

        I could see one or two valid uses. The one place I've used it for production was a tied hash package. We wanted to be able to alias package vars in the hash values with vars from the caller. Retrofitting this to older code was a pain as they HAD to be package variables so we changed the alias sub to look at caller's lexicals and then the package vars.

        -Lee

        "To be civilized is to deny one's nature."
        Update
        While on the subject, I should mention a bug/feature of PadWalker. It will NOT see all lexicals visable where the subroutine is called if you have a bare block
        #!/usr/bin/perl -w use strict; use PadWalker qw(peek_my); my ($oa,$ob) = ("Outer One", "Outer Two"); { my $inb = "Bare Block"; # Will not be seen by peek_my peeper(); } sub peeper { my $c = peek_my(1); print "Lexicals:\n", (map { "Found $_ with value $c->{$_}\n" } sor +t keys %$c ),"\n"; }