Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Get the $main value when use local

by gopalr (Priest)
on Aug 04, 2005 at 09:03 UTC ( #480728=perlquestion: print w/replies, xml ) Need Help??
gopalr has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

$a='main'; if (1) { local $a='sub'; print "\n$a"; print "\n$main::a"; } print "\n$a";


sub sub main

Needed OUTPUT:

sub main main

But, When I use my instead of local, I get the correct output.

How can i get the main value when use local.


Replies are listed 'Best First'.
Re: Get the $main value when use local
by polettix (Vicar) on Aug 04, 2005 at 09:10 UTC
    The goal of local is exactly that of not allowing you to do that. According to the relevant docs:
    This operator works by saving the current values of those variables in its argument list on a hidden stack and restoring them upon exiting the block, subroutine, or eval. This means that called subroutines can also reference the local variable, but not the global one.
    Even if you succeed in touching the hidden stack, this is something you shouldn't really rely on for anything serious.

    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Get the $main value when use local
by Joost (Canon) on Aug 04, 2005 at 09:21 UTC
    local and my are just completely different things. local $something in the main package creates a temporary replacement for the global $main::something. my $something creates a lexical variable with the same name as $something - but $main::something always refers to the global, since lexicals are independent of package.

    I would recommend you just use lexicals. The cases where you want to use local() are very limited (generally, local is used for temporarily overriding special globals like $/, $_ etc).

    If you really need to use globals, just do this:

    $a='main'; if (1) { my $olda = $a local $a = 'sub'; print "$a\n"; print "$olda\n"; } print "$a\n";
Re: Get the $main value when use local
by blazar (Canon) on Aug 04, 2005 at 09:11 UTC
    AFAIK, you can't. That's the whole point about local. Any good reason why you need to use local instead of my? You may try giving a peek into Coping with Scoping and Seven Useful Uses of local for insightful info.

    As a side note, read perldoc perlvar to find out why as a general rule $a should not be used as a general purpose variable.

    Update: all in all your test code may have been written like thus:

    #!/usr/bin/perl -l $c='main'; { local $c='sub'; print $c; print $main::c; } print $c; __END__
Re: Get the $main value when use local
by jbrugger (Parson) on Aug 04, 2005 at 09:14 UTC
    You can't:
    local and my are different
    local does not work the same way as my. In particular, it doesn't create private variables. Variables declared local remain global. Instead, Perl assigns a temporary value to the variable, and then restores the old value when the variable goes out of scope.

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://480728]
Approved by polettix
Front-paged by broquaint
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2017-05-27 06:40 GMT
Find Nodes?
    Voting Booth?