in reply to Sharing a variable between subroutines

Another possibility is to use local with global variables. This use is generally not recommended as global variables are generally a Bad Thing. However, you might at times find this useful. See tilly's Why I Like Functional Programming.

use strict; use warnings; use vars '$foo'; somesub(); print "\$foo is $foo\n"; sub somesub { local $foo = "Ovid"; anothersub(); } sub anothersub { print "\$foo is $foo\n"; }

This is mentioned primarily for the sake of completeness. Sometimes rules need to be broken.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re (tilly) 2: Sharing a variable between subroutines
by tilly (Archbishop) on Jan 05, 2002 at 19:37 UTC
    Indeed.

    As I like to say, every good thing has costs. You need to learn to balance the benefits with the costs and decide what works best in your particular situation.

    The reverse is not quite true, not every bad thing has good points. But it is true often enough that I am uncomfortable saying categorically, Thou shalt not...