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

I have two files: main.pl and Database.pm . In main.pl I have this line:
$info = $db_info->get_info($ID);

in which $db_info has been blessed in Database. $ID is an integer. In Database.pm (which contains a package named Database) I have a subroutine that starts like this:
sub get_info { my ($class,%db_info,$ID) = @_;
However, that doesn't work. So, I changed it to this to troubleshoot my problem:
sub get_info { my ($stuff,$ID) = @_; print "\nstuff = ".$stuff."\nID =".$ID."\n\n";
$stuff prints out "Database=HASH(0x804b620)". $ID prints out "1" (like it should). It seems that both the $class and %db_info are getting put together. From what I've read, and the examples I've seen, that doesn't make sense.

Why is this happening, and what can I do to handle it properly? As always, pointers to relevant docs are very welcome.

melguin.

Replies are listed 'Best First'.
Re: strange args format in module sub
by Chmrr (Vicar) on Sep 16, 2001 at 07:02 UTC

    The line my ($class,%db_info,$ID) = @_, specifically the %db_info part, is the problem. The hash gobbles up all of the values that it can from the parameter list -- even if the number of parameters is odd. Thus, %db_info is getting one key ("1") set, and $ID is left with nothing to read from @_.

    The solution is to move %db_info to after $ID, though it's unclear from what you wrote what %db_info is supposed to do or is used for. If you're thinking that it's the Database object -- it's not. The $class variable is a reference to the Database object in question.

    By the way, the phrase "doesn't work" should be stricken from the English language, in my opinion. Being a little more specific always helps. ;>

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^+*`^ ve^#$&V"+@( NO CARRIER'

Re: strange args format in module sub
by wog (Curate) on Sep 16, 2001 at 06:58 UTC
    It seems that both the $class and %db_info are getting put together.

    This is exactly what happens. When you bless an object, you take a reference (in this case to a hash), and attach a classname to it (which can be easily retrieved with ref.) The reference with the attached classname is is the object. When you call a method on an object the object itself is passed as the first argument.

    You may want to look closer at those examples you have seen, or look for better examples. (You might want to try perltoot or perlobj, if you haven't already.)

Re: strange args format in module sub
by seesik (Initiate) on Sep 16, 2001 at 07:03 UTC
    when your get_info method is invoked with the infix notion, your $db_info object will implicitly be passed as the first argument... no need for your %db_info hash. you'll commonly see
    sub get_info { my $self = shift; my $ID = shift; # or @_ #whatever.. }
    which gives you a $self handle to your db_info instance (the this reference in some other languages). why don't you check out this section in perlobj.

    cheers

Re: strange args format in module sub
by geektron (Curate) on Sep 16, 2001 at 07:00 UTC
    the first arg in this snippet (  my ( $stuff, $ID ) = @_ ) is your object. it will usually be a hashref, since most perl objects are blessed hashes.