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

my @prereq = qw(DBI DBIx::AnyDBD); for my $prereq (@prereq) { eval "use $prereq" ; die "\nPlease install $prereq before installing DBSchema::Sample" +if ($@) ; my $v = "$prereq::VERSION"; # LINE 115 my $v2 = eval $v; print "Found $prereq version $v2\n" ; # LINE 117 } ====== OUTPUT: Use of uninitialized value in string at /home/tbone/perl-modules/lib/s +ite_perl/5.8.0/DBSchema/Sample.pm line 115. Use of uninitialized value in concatenation (.) or string at /home/tbo +ne/perl-modules/lib/site_perl/5.8.0/DBSchema/Sample.pm line 117. Found DBI version Use of uninitialized value in string at /home/tbone/perl-modules/lib/s +ite_perl/5.8.0/DBSchema/Sample.pm line 115. Use of uninitialized value in concatenation (.) or string at /home/tbo +ne/perl-modules/lib/site_perl/5.8.0/DBSchema/Sample.pm line 117. Found DBIx::AnyDBD version Found the following DBD drivers:

Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality.

Replies are listed 'Best First'.
Re: eliminating warnings with conditional uses
by dragonchild (Archbishop) on Sep 29, 2003 at 17:27 UTC
    Either do no warnings 'unitialized'; in the block or do the following:
    my @prereqs = qw(DBI DBIx::AnyDBD); foreach my $prereq (@prereqs) { eval "use $prereq;"; die "Blah\n" if $@; my $v = $prereq->VERSION; print "Found $prereq version $v\n"; }

    You could use symbolic references ($v = ${"${prereq}::VERSION"};), but it's smarter to use the methods handed to you. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: eliminating warnings with conditional uses
by CombatSquirrel (Hermit) on Sep 29, 2003 at 17:31 UTC
    I came up with the following:
    my @prereq = qw(DBI DBIx::AnyDBD); for my $prereq (@prereq) { eval "use $prereq" ; die "\nPlease install $prereq before installing DBSchema::Sample" +if ($@) ; my $v = "\$${prereq}::VERSION"; # LINE 115 my $v2 = eval $v; print "Found $prereq version $v2\n" ; # LINE 117 }
    The problem is that Perl tries to interpolate the variable VERSION in package prereq in line 115 of your code. As long as there is no prereq package, $v will be empty; you'll get another warning on line 117, because your eval in line 116 also returns an empty string.
    Hope this helped.
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
Re: eliminating warnings with conditional uses
by bart (Canon) on Sep 29, 2003 at 19:18 UTC
    You made two errors. You'd immediately see that if you tried to print the contents of $v.
    • You forgot the '$' in front of the variable's name
    • You need to separate the $prereq from the two colons, or Perl will treat them as the name of the variable $VERSION from the package "prereq".

    This fixes it:

    my $v = "\$$prereq\::VERSION"; # LINE 115
    At least, now I see the printed result:
      Found DBI version 1.15
    
    without any warning. (I don't have the other module.)