in reply to YAOOM

Instead of using AUTOLOAD, why not just use can()?
my $recs = $dbh->selectall_arrayref( $sql ); my @people; foreach my $rec ( @$recs ) { my $i = 0; my $person = Person->new(); foreach my $meth ( map { $person->can($meth) } qw( name hight weig +ht age gender dob ) ) { $person->$meth( $rec->[$i++] ) if $meth; } push @people, $person; }

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

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Replies are listed 'Best First'.
Re: Re: YAOOM
by petral (Curate) on Dec 08, 2001 at 01:31 UTC
      Instead of using AUTOLOAD, why not just use can()?

    Because AUTOLOAD leaves the test in the package, whereas can requires the test everywhere the package is used in the application?

      p
Re: Re: YAOOM
by lachoy (Parson) on Dec 08, 2001 at 00:08 UTC

    Just to be picky: you either want a grep instead of a map or you need to rewrite the foreach since can() returns a coderef:

    foreach my $meth ( grep { $person->can( $_ ) } ...

    Chris
    M-x auto-bs-mode

      The code, as written, works just fine. can() will either return a coderef or undef. Hence, the if $meth check. I used map to allow for warnings, if desired. If no warnings are desired, grep is the better way to go.

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

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Excuse me, just being a dingdong. Nothing to see here. Must test code before opening yap :-)

        Chris
        M-x auto-bs-mode

Re: Re: YAOOM
by mortis (Pilgrim) on Dec 07, 2001 at 22:54 UTC
    Actualy, the technique is less for actual dynamic method calling, and more for actual method typos in the code.