in reply to Re: Re: Understanding why strict prevents a use of local
in thread Understanding why strict prevents a use of local
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}
|
|---|