# So, how to get $Foo::Str without soft ref?
Moreover, how to know whether an arbitrary package has a specific global

1. various ways.
a) Any symbol table your current symbol table knows about - is in the current symbol table. So:

use strict; package Foo; use vars qw($str); # $Foo::str created $str = 'foo str'; package main; my $pack = "Foo::"; my $var = "str"; print ${*{${$main::{$pack}}{$var}}{SCALAR}}; __END__ foo str

b) You could also use string eval - but that's as deprecated as symbolic references hereabouts ;-)

use strict; $Foo::str = 'foo str'; my $class = 'Foo'; my $value = eval "\$$class\::str"; print $value,$/;

2. Look into the symbol table:

package Foo; use vars qw($str); $str = "foo str"; package main; my $pack = "Foo::"; my $var = "str"; print "$_: ",(defined ${$main::{$pack}}{$_} ? 'yup' : 'nope'),$/ for qw(str bar); __END__ str: yup bar: nope

update: Caveats about autovivification apply. You might want to break up the lookup into steps, operating with defined. Consider:

$\=$/; use strict; use warnings; package Foo; use vars qw($str); $str = "foo str"; package main; my $pack = "Bar::"; my $var = "str"; print "yup" if defined ${$main::{$pack}}{$var}; /::$/ and print for keys %main:: __END__ version:: Tie:: utf8:: re:: CORE:: DynaLoader:: mro:: strict:: attributes:: Bar:: <----here Regexp:: vars:: UNIVERSAL:: Foo:: main:: Carp:: PerlIO:: IO:: Internals:: warnings:: DB::

Symbol tables are subject to autovivification, too.


In reply to Re: How to access globals in arbitrary classes, without using soft ref? by shmem
in thread How to access globals in arbitrary classes, without using soft ref? by llancet

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.