[tpm] Using a variable in a 'qw()'

Michael Graham magog at the-wire.com
Thu Jun 12 11:10:33 PDT 2008



when you say: 

   my $foo="org.tle_bu.Clients";
   use Net::DBus::Exporter ($foo);

It is really the equivalent of:

    my $foo="org.tle_bu.Clients";
    BEGIN {
        require Net::DBus::Exporter;
        Net::DBus::Exporter->import($foo);
    }
   
And the BEGIN block gets run before you assign to $foo.  Which is not what you
want.

Instead, do this:

    my $foo;
    BEGIN {
        $foo="org.tle_bu.Clients";
    }
    use Net::DBus::Exporter ($foo);

Or:

    my $foo="org.tle_bu.Clients";
    require Net::DBus::Exporter;
    Net::DBus::Exporter->import($foo);


Michael


On Thu, 12 Jun 2008 13:55:52 -0400
Madison Kelly <linux at alteeve.com> wrote:

> Mike Stok wrote:
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> > 
> > On 12-Jun-08, at 10:25 AM, Madison Kelly wrote:
> > 
> >> Hi all,
> >>
> >>  I want to use a variable when calling a module that uses the
> >> 'qw()' syntax. Specifically:
> >>
> >> use Net::DBus::Exporter qw(org.tle_bu.Clients);
> >>
> >>  I would like to be able to say:
> >>
> >> my $foo="org.tle_bu.Clients";
> >> use Net::DBus::Exporter qw($foo);
> >>
> >>  But '$foo' is passed in directly, rather than it's value. Is there
> >> an
> >> alternative to 'qw()'? Honestly, I use that syntax all the time but
> >> I've
> >> never really understood how it works. Perhaps I should be asking
> >> for a pointer to a doc that explains this syntax better?
> >>
> >> Thanks, as always!
> > 
> > In addition to the things other people have mentioned, you need to
> > be aware of when variables get set.  use is a BEGIN time thing, so
> > 
> > #!/usr/bin/env perl
> > 
> > use strict;
> > use warnings;
> > 
> > my $var = ':standard';
> > use CGI ($var);
> > print header;
> > 
> > doesn't do what I intended, and I need to do this to make sure $var
> > is set before the use happens (or something similar depending on
> > what you want the scoping of $var to be):
> > 
> > #!/usr/bin/env perl
> > use strict;
> > use warnings;
> > 
> > my $var;
> > BEGIN { $var = ':standard'; }
> > use CGI ($var);
> > print header;
> > 
> > Hope this helps,
> > 
> > Mike
> 
> Thanks Mike, Jim and Mark!
> 
>    I'm still struggling to get this working, and I think it might be 
> what Mike is getting at... My variables are set by reading a config 
> file, so sticking the variable into a 'BEGIN {}' won't work as too
> many things have to have happened before I can call the SR that reads
> the file and populates my config hash. I tried wrapping the module
> call in 'END {}', but that also didn't work.
> 
>    Specifically:
> 
> print "dbus::interface::clients:
> [$$conf{dbus}{interface}{clients}]\n";
> 
>    Prints out:
> 
> dbus::interface::clients: [org.tle_bu.Clients]
> 
>    But when I call:
> 
> use Net::DBus::Exporter qq($$conf{dbus}{interface}{clients});
> 
>    I get:
> 
> Use of uninitialized value in pattern match (m//) at 
> /usr/lib/perl5/Net/DBus/Exporter.pm line 263.
> Use of uninitialized value in concatenation (.) or string at 
> /usr/lib/perl5/Net/DBus/Exporter.pm line 263.
> interface name '' is not valid.Names must consist of tokens using the 
> characters a-z, A-Z, 0-9, _, with at least two tokens, separated by
> '.' BEGIN failed--compilation aborted at ./dbus_server line 74.
> 
>    If I call:
> 
> use Net::DBus::Exporter qq($$conf{dbus}{interface}{clients});
> 
>    Or:
> 
> END { use Net::DBus::Exporter qq($$conf{dbus}{interface}{clients}); }
> 
>    I get:
> 
> Use of uninitialized value in string at ./dbus_server line 74.
> interface name '' is not valid.Names must consist of tokens using the 
> characters a-z, A-Z, 0-9, _, with at least two tokens, separated by
> '.' BEGIN failed--compilation aborted at ./dbus_server line 74.
> 
>    Also, just to be safe, I tried sticking the value is a simple 
> variable called '$foo', in case being in a hash was part of the
> problem, but that didn't fix anything.
> 
> Thanks!
> 
> Madi
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm


-- 
Michael Graham <magog at the-wire.com>


More information about the toronto-pm mailing list