Hi c,

In my opinion your comment that this "is being done for a rather lengthy script" says to me that you want to avoid globals at all cost. The comments and snippits of advice of our fellow monks in this case are excellent and will work perfectly fine for you. But just in case you were looking for yet another way to do it...

Another way to approach the problem that may add too much overhead for you is to build an object around your process. I'm a quasi-newbie when it comes to OO design and implimentation, but there are many cases where OO design lets you concentrate on the business rule you are coding and avoid thinking about your data and the methods of accessing it.

Don't judge me by the quality of my code here because, like I said, I'm not the most experienced OO programmer in the world. However I hope the concepts that I'm talking about here are helpful and make sense. I also hope to receive some constructive criticism on what I've tossed down here.

Here we create a package called Nule. It has a few internal scalars called A, B and C. It also has a few access methods (the subs) which print the value of A (print_a), lets you change or simply return the value of B (access_b) and accepts some random stuff to print along with C (print_stuff_c). The scalars in the object act as globals according to any methods of the object your create, but avoid polluting your actual "main" working space.

#! /usr/local/bin/perl -w use strict; { package Nule; sub new { my $self = {}; shift; # Has package name $self->{A} = shift; $self->{B} = shift; $self->{C} = shift; bless ($self); return $self; } sub print_a { my $self = shift; print $self->{A}."\n"; } sub access_b { my $self = shift; if (@_) { $self->{B} = shift; } return $self->{B}; } sub print_stuff_c { my $self = shift; my $stuff = shift; print "$stuff".$self->{C}."\n"; } } # end of package # Main portion of program. # Create object my $thing = new Nule(1, 2, 3); # Print A $thing->print_a(); # Change B, then print it $thing->access_b(4); print $thing->access_b()."\n"; # Print C with some stuff $thing->print_stuff_c("Some stuff "); exit;

I hope this is helpful to someone (writing it was a good exercise for me!). If you like the idea I urge you to check out merlyn's perlboot tutorial as well as perltoot and perlobj for more information.

Good luck!
{NULE}

--
http://www.nule.org

In reply to Re: Re: Re: Understanding why strict prevents a use of local by {NULE}
in thread Understanding why strict prevents a use of local by c

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.