http://www.perlmonks.org?node_id=660363


in reply to Re: symbolic variables
in thread symbolic variables

yikes, i have to resort to them whenever i want to have "generic" logi +c that deals with a multitude of routines. i use symbolic references for : object methods dispatch tables and in most unit test scripts If you have a better alternative; i'll consider it!

object methods : i use anonmyous routines for the methods that deal with scalar attributes.

package <>; our $scalarMethods; @{ $scalarMethods } = qw( name ); foreach my $attributeScalar ( @{ $scalarMethods } ) { no strict "refs"; *$attributeScalar = sub { my $subName = $package . "::$attributeScalar"; if ($VRB) { print " $subName \n"; } my ($self) = shift; if (@_) { $self->{$attributeScalar} = shift; } return $self->{$attributeScalar}; } }

dispatch tables : a generic manner to call routines in a package

for ( $ndx=0; $ndx<=0; $ndx++ ) { no strict 'refs'; ($kgDATA,$pmDATA) = &{ "cktTmplt_utils::$SUB" }($ndx );

unit test scripts : within the <>.t script

# are package variables accessible ? no strict "refs"; foreach my $pkg ( @{ $filePackages } ) { for ( my $i=0; $i<=1; $i++ ) { ${ $pkg . "::VRB" } = $i; is( $i, ${ $pkg . "::VRB" }, "GLBL VAR : VRB" ); ${ $pkg . "::TST" } = $i; is( $i, ${ $pkg . "::TST" }, "GLBL VAR : TST" ); } ${ $pkg . "::VRB" } = 1; # no STDOUT durings tests ${ $pkg . "::TST" } = 1; # return data and don't execute } use strict "refs";