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

Dear monks,

Im struggling with references. Tye's References quick reference is a great for information about dereferencing, and chapter 8 of programming perl has a lot about references. Even with this information however I have still come across a problem.

The following code is part of an object orientated package, hence the self reference (otherwise I could understand manage it) and is trying to use Getopt::Long
sub readoptions { my $self = shift; # usually I would create a hash %hash = ( foo => \$bar ); # and reference %hash. unless i'm mistaken this creates # an anoymous hash and stores it in $self->{CONFHASH} $self->{CONFHASH} = { foo => \$self->{BAR} }; # first argument to GetOptions woudl usually be \%hash GetOptions( $self->{CONFHASH}, 'foo|f' ) or die "option not found"; }
Yes, this is completely wrong. The result is trying to be something where I can do $self->{BAR} to find an option. Would a fellow monk care to enlighten me?

Update: oops, i stated $self->{CONFHASH}->{foo} when I meant $self->{BAR}, although both ways are good
TIA

Replies are listed 'Best First'.
Re: creating references
by Zaxo (Archbishop) on Jan 25, 2004 at 23:33 UTC

    A set of options derived from GetOpt::Long is generally global in some sense. They could be attached to a package variable, but it's usually not necessary to have each class instance carry its own reference to the options. The exception would be if an instance might modify an option for its own use. For that, local may be of more use than carrying the reference.

    I don't see anything wrong with your dereferencing that won't be cleared up with a little refinement of your design. It's often worth while to call GetOptions() in a BEGIN block to make the options visible at compile time.

    After Compline,
    Zaxo

      It's often worth while to call GetOptions() in a BEGIN block to make the options visible at compile time
      In OO? Huuuuh?