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

I'm trying to learn a little more about PERL modules but I'm stumped with the following results: Results Below:
# result from running learn_module.pl VAL1: Learning=HASH(0x194ee0) VAL2: foo VAL1: Learning=HASH(0x194ee0) VAL2: bar
Perl Script Below:
use strict; use warnings; use Learning; my $new_module = Learning->new; my %hash = (); push @{$hash{vals}}, "foo"; push @{$hash{vals}}, "bar"; my $text2 = "This is a string"; foreach my $data1 ( @{$hash{'vals'}} ) { my ($val1, $val2) = $new_module->return_two_values($data1, $text2) +; print "VAL1: $val1\n"; print "VAL2: $val2\n\n"; }
Perl Module Below:
package Learning; use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); require Exporter; require AutoLoader; @ISA = qw(Exporter AutoLoader); @EXPORT = qw(); $VERSION = '0.01'; sub new { my $self=shift; my $class=ref($self) || $self; return bless {}, $class; } sub return_two_values { my ($val1, $val2) = @_; return ($val1, $val2); } 1;
Any help would be greatly appreciated

Replies are listed 'Best First'.
Re: Issue with creating module
by samtregar (Abbot) on Mar 12, 2008 at 19:23 UTC
    You're calling return_two_values() as a method, so the first argument is the object. Your code should read:

    my ($self, $val1, $val2) = @_;

    The $self variable here is a reference to your $new_module object.

    Also, it's worth noting that your choice of variable name for your object reference isn't the best. The module here is "Learning" but the variable $new_module actually holds an object not a module. You might call this variable $new_object instead, to keep things clearer in your mind.

    -sam

      Thanks sam...I still have a lot to learn :)