Hi People

I'm trying to get a grip on passing @_ to subroutines. Or rather NOT passing it.

I have a package:

sub HLOM::Links::new {
        my ($class, %arg) = @_;
        my $userid = 0;
        if ($arg{userid}) {$userid = int $arg{userid};}
        bless {
                _UserID => $userid,
                _LinkID => 0,
                _longmessage => "",
                _shortmessage => "",
                _error => ""
        }, $class;
}

and several subroutines.

These subroutines are called externally ie HLOM::Links->FindLink(LinkID => $id);

but also internally ie &FindLink(LinkID => $id);

I read in Perlsub that doing &FindLink will pass the current @_ to the subroutine. Great.

But I thought that doing &FindLink(LinkID => $id); would not pass the current @_ which is what I want. ie I only want to pass the param LinkID not all the other stuff that was passed to the subroutine that called the subroutine....

here's a sample of the subroutine. Really simplified as it's quite long.

sub AddLink {
        my ($class, %arg) = @_;
        my $userid = $_[0]->{_UserID};
        my $LinkUrl = $arg{LinkUrl};
        my $LinkTitle = $arg{LinkTitle};
        my $newlinkid = 0;
        my $fc = 0;     
        # a load of stuff snipped.
        # adds link and sets $newlinkid to the id of link
        if ($newlinkid > 0) {$fc = &FindLink(LinkID => $LinkID);}
        if ($fc > 0) {
                $_[0]->{_error} = "";
                $_[0]->{_shortmessage} = "Link added\n";
        }
        return $newlinkid;
}

what's happening is the current @_ is being passed to the subroutine FindLink even though I'm adding arguments. All my subroutines have "my ($class, %arg) = @_ ;" at the start of them. but the error log says the following:

Odd number of elements in hash assignment

looking through the hash the error is right. It's like the hash has become mangled. It appears as if the first element of the array (Ie the hash key LinkID) has been stripped.

here's the output when traversing the hash:

3123 =>
LinkNotes =>
LinkTitle => perlsub
LinkID => 3123
LinkUrl => http://www.perldoc.com/perl5.6.1/pod/perlsub.html

The first line should be LinkID => 3123

And the rest of it should NOT be being passed. I just don't get it.

I thought that maybe I need to pass $class explicitly when doing this. That fixed it in the past, but then I thought maybe I should ask someone who actually knows. I do want to pass the class and the args explicitly added but not the %arg that was in the calling subroutine.

Anyone?

TIA

Angie


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to