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

Monks,

I'm using Class::DBI. I have the following syntax which won't work (in Perl 5.005) and I'm wondering how it can be done:

use strict; use warnings; ...snip... my $nn = "admin_contact_fk" ; my $foo = $record->$nn; perl -c CDNR.pm syntax error at CDNR.pm line 293, near "$nn;" BEGIN not safe after errors--compilation aborted at CDNR.pm line 466.

Doesn't work... but...

my $foo = $record->admin_contact_fk;

Does work.

Can someone explain how to use a variable with the "->" operator?

UPDATE: changed subject to include perl version

Please don't tell me to upgrade, as I don't control the production environment at my $firm.

-------------------------------------
Nothing is too wonderful to be true
-- Michael Faraday

Replies are listed 'Best First'.
Re: Syntax question (Perl 5.005)...
by chromatic (Archbishop) on Nov 07, 2003 at 22:10 UTC

    Does adding an empty argument list help distinguish this as a soft method call?

    my $foo = $record->$nn();
Re: Syntax question...
by hardburn (Abbot) on Nov 07, 2003 at 21:34 UTC

    This code works for me:

    use strict; package foo; sub bar { print "Works\n" } package main; my $var = "bar"; foo->$var;

    It's possible that your error is somewhere else. Perl's error messages are often, um, less then perfect at pointing to the real place the error is at.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      Interesting. I copied your code and tried to run on my machine and got an error. Perhaps this is due to the fact I'm running on Perl 5.005.

      gonzo:/export/home/fredk/docs/test: more foo.pl #!/usr/local/bin/perl use strict; package foo; sub bar { print "Works\n"; } package main; my $var = "bar"; foo->$var; gonzo:/export/home/fredk/docs/test: ./foo.pl syntax error at ./foo.pl line 10, near "$var;" Execution of ./foo.pl aborted due to compilation errors.
      UPDATE

      This works if I change foo->$var to foo->$var();

      Can someone confirm this is a 5.005 thing?

      -------------------------------------
      Nothing is too wonderful to be true
      -- Michael Faraday