Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Syntax question when using Class::DBI

by freddo411 (Chaplain)
on Dec 09, 2004 at 00:51 UTC ( [id://413389]=perlquestion: print w/replies, xml ) Need Help??

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

Monks,

I'm stuck on a syntax problem. I'm using Class::DBI which lets me get at values of fields within a DB by treating each row as an object. So normally I'd refer to my "id" as:

print $rowobj->id;
OK so far, but now I need to be more dynamic, like so:
my $columnname = 'id'; print $rowobj->$columnname;
but this gives me an syntax error when I perl -c my code.

How do I use a variable value in this case?

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

Replies are listed 'Best First'.
Re: Syntax question when using Class::DBI
by fglock (Vicar) on Dec 09, 2004 at 01:47 UTC

    This syntax does work in 5.005_03 - but you may have to add parenthesis to make it clear that you are calling a method:

    my $columnname = 'id'; print $rowobj->$columnname();

      Thank you. I had this problem at work last week where I don't get to pick the perl version :) and resorted to no strict 'refs'; &{etc...

      Just for reference - The ever-growing reference table of backwards 5.005_03 compatibility: 289351

Re: Syntax question when using Class::DBI
by broquaint (Abbot) on Dec 09, 2004 at 01:13 UTC
    There is no problem with using a simple scalar variable when dynamically calling a method, as Fletch illustrated. However if you're using an aggregate variable or an expression you can simply use the can method e.g
    print $rowobj->can( $cols{id} )->();
    Or if you wish to call it directly you need to be a little indirect and take advantage of scalar dereferencing e.g
    print $rowobj->${ \$cols{id} };
    Addendum: On a more practical note you can always access the primary key by using the id accessor method, regardless of what is the name of the actual row; as documented.
    HTH

    _________
    broquaint

Re: Syntax question when using Class::DBI
by perrin (Chancellor) on Dec 09, 2004 at 01:26 UTC
    That should work, but you can also use the get() accessor:
    $rowobj->get($columnname)
Re: Syntax question when using Class::DBI
by Fletch (Bishop) on Dec 09, 2004 at 00:58 UTC

    That's perfectly valid syntax (although more paranoid code would check with UNIVERSAL::can before calling arbitrary methods).

    freebie:~ 973> cat fooble + 19:55:47 my $columnname = 'id'; print $rowobj->$columnname; freebie:~ 974> perl -wc fooble + 19:55:48 Name "main::rowobj" used only once: possible typo at fooble line 2. fooble syntax OK

    Your problem's elsewhere.

      fooble syntax OK Your problem's elsewhere.
      Scratches head ... Really, I'm not crazy:

      gonzo:: more foo.pl #!/usr/local/bin/perl my $columnname = 'id'; print $rowobj->$columnname; gonzo:: perl -c foo.pl syntax error at foo.pl line 4, near "$columnname;" foo.pl had compilation errors.
      OK, how about that fact that I'm running an ancient perl 5.005. That would explain our different results.

      And the solution for 5.005 is staring at me in another NOOBs question:

      my $columnname = 'id'; print $rowobj->{$columnname};
      Cheers

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

        Be aware that Class::DBI lazy-loads the columns, so as soon as you start using the Essential feature for columns, you might find $rowobj->{$columnname} empty even though there is a value in the column. Using the get method should make sure that your object gets filled with data.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://413389]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-03-29 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found