Help with test program

Discussion in 'Perl Misc' started by joubertb@gmail.com, Oct 8, 2006.

  1. Guest

    I have been working on this for the past few days and still can't
    figure out what I am doing wrong.

    Below the my test program. Can someone point out to me what I am doing
    wong?

    Thanks.

    --joubert

    ===========================< cut here >===============================

    package foobar;

    sub new {
    my $class = shift;
    my $self = {};

    return bless $self, $class;
    }

    sub func {
    my $self = shift;
    my $param = shift;
    print "inside function\n";

    print "param=$param\n";

    return "zzzzz";
    }


    $f = new foobar;

    my %myarray = (
    foo => { func => $f->func, }
    );
    #
    # When I do the above assignment, it calls the method $f->func. What I
    want it to do is assign the
    # function pointer to func so that below I can call it.
    #

    #$myarray{foo}{func};
    $xxx = $myarray{foo}{func}("abc123");
    #
    # When the function above is called, it says that "zzzzz" is does not
    exist. It looks like the return
    # value from $f->func is assigned to $myarray{foo}{func}.
    #
    # What am I doing wrong ?
     
    , Oct 8, 2006
    #1
    1. Advertising

  2. On 2006-10-08 20:47, <> wrote:
    > I have been working on this for the past few days and still can't
    > figure out what I am doing wrong.
    >===========================< cut here >===============================
    >
    > package foobar;

    [...]
    > sub func {
    > my $self = shift;
    > my $param = shift;
    > print "inside function\n";
    >
    > print "param=$param\n";
    >
    > return "zzzzz";
    > }
    >
    >
    > $f = new foobar;
    >
    > my %myarray = (
    > foo => { func => $f->func, }
    > );
    > #
    > # When I do the above assignment, it calls the method $f->func.


    Yes.

    > # What I
    > # want it to do is assign the
    > # function pointer to func so that below I can call it.
    > #
    >
    > #$myarray{foo}{func};
    > $xxx = $myarray{foo}{func}("abc123");


    If you want to call it like this (i.e., you don't want to call func on
    any object, but always on $f), the best way is probably:

    my %myarray = (
    foo => { func => sub { $f->func(@_) }, }
    );

    if you want to be able to call func on arbitrary objects, I'd just store
    the name and let perl find the right method at call time:

    my %myarray = (
    foo => { func => 'func', }
    );

    my $f2 = new foobar;
    my $func = $myarray{foo}{func};
    $xxx = $f2->$func("abc123");

    There are other ways ...

    hp


    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
     
    Peter J. Holzer, Oct 8, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sting
    Replies:
    5
    Views:
    1,634
    Natty Gur
    Jun 8, 2004
  2. Robert Allan Schwartz
    Replies:
    7
    Views:
    378
    Marco Manfredini
    Aug 13, 2004
  3. Chris
    Replies:
    1
    Views:
    671
    Chris
    May 11, 2006
  4. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    714
    Skybuck Flying
    Oct 4, 2009
  5. Tomasz Wegrzanowski
    Replies:
    2
    Views:
    428
    Mauricio Fernandez
    Aug 23, 2006
Loading...

Share This Page