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

There may be MTOWTDI, but what criteria might cause me to pick one over t'other?
{ my $num = 0; sub count_it { $num++; } sub print_it { print $num, "\n"; } }
vs.
sub count_it { our $num ||= 0; $num++; } sub print_it { our $num ||= 0; print $num, "\n"; }
Which is "better", under what circumstances?
-- 
Jeff Boes                            vox 616.226.9550 ext 24
Database Engineer                           fax 616.349.9076
Nexcerpt, Inc.                       http://www.nexcerpt.com
        ...Nexcerpt...Connecting People With Expertise

Replies are listed 'Best First'.
Re: Pros/cons of my/our sought
by broquaint (Abbot) on May 14, 2002 at 18:31 UTC
    our()
    pros
    • creates package variables with ease
    • visible across the scope of the file
    cons
    • clutters package namespace
    • visible across the scope of the file
    • globals don't scale
    my()
    pros
    • creates lexical variables which are safer and don't dirty a package's namespace
    • my()ed variables behave well and will destroy/untie/evaporate when they fall out of scope
    • you can do funky things like closures with my()ed variables
    cons
    • my()ed vars don't stick around long enough to pick up the cheque ;-)
    Basically our() is designed for getting around using vars, whereas the use of my() encourages safer programming and goes hand in hand with strict which is a much praised practice in the monastery.
    HTH

    _________
    broquaint

Re: Pros/cons of my/our sought
by cjf (Parson) on May 14, 2002 at 18:12 UTC
Re: Pros/cons of my/our sought
by blakem (Monsignor) on May 15, 2002 at 08:00 UTC
    my is probably what you want, since it creates a lexical variable that can't be tampered with outside of the block its delcared in. Take a look at this code:
    #!/usr/bin/perl -wT use strict; # use an our-variable.... sub count_our { our $num ||= 0; $num++; } sub print_our { our $num ||= 0; print $num, "\n"; } # use a my-variable.... { my $num = 0; sub count_my { $num++; } sub print_my { print $num, "\n"; } } # thousand lines of code here # $num is common enough that we use it somewhere else as well my $num = 10; our $num = 10; # thousand lines of code here # did we mess up the 'my' variable? count_my(); print "My count: "; print_my(); # how about the 'our' variable? count_our(); print "Our count: "; print_our();
    Notice that the our-variable gets fubared somewhere in the middle of this script. The my-variable is truly encapsulated and can't be modified except by count_my() and print_my().

    -Blake

(jeffa) Re: Pros/cons of my/our sought
by jeffa (Bishop) on May 14, 2002 at 19:58 UTC
Re: Pros/cons of my/our sought
by Anonymous Monk on May 14, 2002 at 18:31 UTC
    In your second usage consider what might happen as your program becomes more complex and you or someone else adds more subroutines. Without carefully checking the entire codebase for existing our globals you may easily stomp on the global $num in another routine or block.