in reply to How to access globals in arbitrary classes, without using soft ref?

The symbol table is a hash, so you can check existence with exists. See perlmod for a description of symbol tables.
use strict; use warnings; use 5.010; { package Foo; our $bar; } { if (exists $::{'Foo::'}{bar}) { say "There's a Foo::bar with some sigil"; } if (exists $::{'Foo::'}{baz}) { say "There's a Foo::baz with some sigil"; } }

(Update: added code)

  • Comment on Re: How to access globals in arbitrary classes, without using soft ref?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to access globals in arbitrary classes, without using soft ref?
by llancet (Friar) on Feb 22, 2010 at 09:14 UTC
    thanks a lot!

      They are called symbolic refs.

      I must disagree with moritz. Using %:: instead of symbolic refs does not help at all. All the same problems and disadvantages exist because it does exactly the same thing. Using %:: has the additional disadvantage that it's not caught by use strict 'refs';. In fact, it's generally less readable and harder to code using %:: instead of symbolic refs, so it's usually a step backwards to use %::.

      If you want to improve your approach, use accessors.

      use strict; { package Foo; my $str = 'foo str'; sub str { my $class = shift; $str = $_[0] if @_; return $str; } } { package Bar; my $str = 'bar str'; sub str { my $class = shift; $str = $_[0] if @_; return $str; } } my $class = 'Foo'; my $value = $class->str();