in reply to Use with variable

Others have answered (the eval route is the way I'd go); but I have a question. Was your query merely curiosity, or have you actually found a use for this technique?

I've never encountered a problem where this was the solution, and I'm just curious from a "hey, that's really unusual" point-of-view. Would you mind sharing more about what you're doing?

--
$me = rand($hacker{perl});

Replies are listed 'Best First'.
Re^2: Use with variable
by edan (Curate) on Sep 08, 2004 at 15:09 UTC

    It's easy to think of applications for this technique. Any sort of "plug-in" or "load-on-request" situation. Or "drivers", for instance. Ever use DBI? Notice how you only have to "use DBI;" and then the appropriate "DBD::*" module is loaded according to the datasource in your DBI->connect() call? That magic is done by dynamically loading a module that is contained in, yes, you guessed it, a variable! Actually, the eval method is used there:

    # --- load the code my $driver_class = "DBD::$driver"; eval qq{package # hide from PAUSE DBI::_firesafe; # just in case require $driver_class; # load the driver }; if ($@) { #....
    --
    edan

      Hey, neat. I've never written anything that could benefit from plugins, so I never encountered the need for that before.

      I definitely need to file that one away for later. Thanks!

      --
      $me = rand($hacker{perl});