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

Hello again

This post is a result of Passing a required script arguments

Below is my code I'm using, and I simply want to print the argument passed to the Foo module:

#####Foo.pm###### package Foo; sub test{ my $name = shift; return $name; } 1; ##### bar.pl ##### #!/perl/bin/perl use warnings; use strict; use Foo; my $name=Foo->test("TEST"); print "NAME: $name\n";
The module seems to work but it's returning "NAME: Foo", and not "TEST".
Can someone tell me what I'm doing wrong?

Thanks in advance

Replies are listed 'Best First'.
Re: First Perl Module!!
by hardburn (Abbot) on Sep 26, 2003 at 16:18 UTC

    The class name noted before the -> operator is actually passed as the first argument to your subroutine. Other languages hide this fact from you (such as Java's this), but Perl doesn't.

    To fix it, you need to change the subroutine to something like this:

    sub test{ my $class = shift; my $name = shift; return $name; }

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: First Perl Module!!
by Ovid (Cardinal) on Sep 26, 2003 at 16:49 UTC

    As has been pointed out, using the arrow operator passes the class name (or object instance, if you have one) as the first argument to the method. However, you may not want to dive into OO Perl just yet. In that case, call the subroutine as a function, not as a method. That means that you change the '->' to '::'.

    use Foo; my $name = Foo::test("TEST"); print "Name: $name\n";

    Once you get that down, you can start learning about Exporter and learn to export the selected functions you need:

    package Foo; use strict; use warnings; use base 'Exporter'; our @EXPORT_OK = ( 'test' ); sub test { my $thing = shift; return $thing; }

    With that code anything that can be accessed through your package (such as package variables and subroutines) can be exported to your class by listing the item in your "import list":

    use Foo qw(test); my $name = test("TEST"); print "Name: $name\n";

    See the documentation for Exporter for more information. Have fun!

    Cheers,
    Ovid

    New address of my CGI Course.

Re: First Perl Module!!
by broquaint (Abbot) on Sep 26, 2003 at 16:20 UTC
    This is because in perl when a method is called the calling thing (be it the class name or object) is passed in as the first argument e.g
    sub func { print "first arg: $_[0]\n" } my $o = bless []; main->func(); $o->func(); __output__ first arg: main first arg: main=ARRAY(0x80fbb0c)
    So in the first case the string main is being passed in as that's what the method was called on, and in the second case the object $o. With that in mind you're test() method should look something like this
    sub test{ my( $self, $name ) = @_; return $name; }
    See. perltoot for more information on methods.
    HTH

    _________
    broquaint