in reply to simple symbolic reference Q

I seem to be forgetting basic symbolic reference usage.
That's good. You should not have learnt it in the first place.

Replies are listed 'Best First'.
Re^2: simple symbolic reference Q
by perl-diddler (Chaplain) on Oct 04, 2010 at 22:35 UTC
    Um...I just wrote twisted routine to handle a bunch of symbolic references. This is a code snippet where I pass in the name of a variable, and use it to construct 3 different symbolic variable names that seem to all work (the variables are 'package' variables declared using 'our'. How is this different?
    AH... just verified: Our vs. my.
    sub _common_setvar { my $XX = shift; # XX=(db|cache) no strict 'refs'; my $XX_prefix = $XX . '_prefix'; debug("path_ops","_XX_prefix=%s",$XX_prefix); if (@_) { $$XX_prefix = $_[0]; my $_XX_prefix_set = "_" . $XX_prefix . "_set"; debug("path_ops","_XX_prefix_set=%s",$_XX_prefix_set); $$_XX_prefix_set = True; my $_save_XX_prefix = '_save' . $$XX_prefix; if ($_storage_dir_set) { my $tmp = $$_save_XX_prefix = _path_cat($storage_dir,$$XX_prefix); debugn("path_ops", "cmn_sv: sd=%s, ddXX_p=%s, ddsave_XX_p=%s, ", $storage_dir, $tmp, $$XX_prefix); _dir_check($$_save_XX_prefix); } } $$XX_prefix; } sub db_prefix { my $this=shift; _common_setvar('db',@_); debug("path_ops","db=%s, sdb=%s",$db_prefix,$_save_db_prefix); } sub cache_prefix { my $this=shift; _common_setvar('cache',@_); debug("path_ops","cache=%s, scache=%s",$cache_prefix,$_save_cache_ +prefix); }

      Nasty.

      You should have simply used a hash. Drop all the toxic $$XX stuff out. Use simple keys instead of complex variable names with symbolic refs on top.

      $cache{'storage directory'}; for example.

      Using hashes would prune your tree of subroutines, prevent them from having action at a distance, make it clear what variables are changing, and greatly improve your code readability.