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

Oh mighty perl monks please let me ask a simple(?) question. I use perl for a while now but i cant sort that out myself and obviously i dont find the correct search terms in google. I'm havin several modules that i include dynamicly if the core modules need them like that :
eval( "use sy::$c->{id}" ); print Dumper ( %{sy::c1::capability} );
off course the above works but what would be the correct syntax if i would want to do something like that with a variable package name like below :
print Dumper ( %{sy::$packname::capability }
Even a simple link would do if i could find the info there Many Thx for the help ...

Update:Thank you everyone for your help

Replies are listed 'Best First'.
Re: Dynamic package name (?!)
by moritz (Cardinal) on Feb 07, 2011 at 15:05 UTC
      I had a look on perlmod allready but unfortunatly i could not find something that even pointed in the right direction. I dont have problems with variable "Variable" names (even if u should not use them i know ;) but that variable package name syntax makes me barf :-P

      P.S.: %::{"sy::"}{$packagename . '::}

      just spits out tons of errors ending with :

      syntax error at ./sy.x line 699, near "}{" syntax error at ./sy.x line 702, near "}" syntax error at ./sy.x line 714, near "}" syntax error at ./sy.x line 730, near "}" Execution of ./sy.x aborted due to compilation errors.
        Sorry, the main symbol table is %::, but when accessing elements it needs to become a $. And of course the final quote needs to be finished too, so more like:
        $::{"sy::"}{$packagename . "::"}
Re: Dynamic package name (?!)
by chromatic (Archbishop) on Feb 07, 2011 at 17:00 UTC

    If these modules were classes, you could load them with Class::Load and then call static methods on them instead of accessing package globals directly.

    (You can load them with Class::Load even if they aren't classes.)

Re: Dynamic package name (?!)
by Anonymous Monk on Feb 07, 2011 at 15:31 UTC
      Many thx that did it ... well not very clean with "no strict 'refs'" but it works
Re: Dynamic package name (?!)
by Anonymous Monk on Feb 07, 2011 at 15:15 UTC
Re: Dynamic package name (?!)
by pemungkah (Priest) on Feb 09, 2011 at 23:58 UTC
    A future suggestion: please don't delete the question! Leaving the question allows someone else to search on it.

    Also, I might have wanted to add something, but I really can't add anything to "thanks" - and puzzling out the original question from the replies will take too much time.

Re: Dynamic package name (?!)
by CountZero (Bishop) on Feb 07, 2011 at 14:57 UTC
    What happened when you tried it?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      print Dumper( %{ sy::$ch_id::caps } ) => Bad name after sy:: at ./sy.x line 699
        I tried this and it worked:
        package Testing; $in_package = 'In package'; package main; use Data::Dumper; $package = 'Testing'; print Dumper(${$package . '::in_package'});
        Output:
        $VAR1 = 'In package';
        Of course it will not run under the strictures.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James