Objects instead of global variables in Perl
Posted
by
Gaurav Dadhania
on Stack Overflow
See other posts from Stack Overflow
or by Gaurav Dadhania
Published on 2011-01-17T04:24:17Z
Indexed on
2011/01/17
10:53 UTC
Read the original article
Hit count: 223
I don't know if this is the right thing to do. But I'm lookig for tutorials/articles on using objects instead of global variables to store state. For eg.
package something
# some code here...
# that generates errors and uses
# something::errors to track errors.
package something::errors
sub new {
my ($this) = @_;
bless $this;
return $this;
}
sub setErrors{
my ($this, @errors) = @_;
$this->{errors} = \@errors;
}
sub getErrors{
my ($this) = @_;
return $this->{errors};
}
Is this better than using global varibles? Any down-sides to this? Any approach which might be better?
Thanks.
© Stack Overflow or respective owner