Problem creating a module in perl

software development

#1

So I’ve been playing around with creating my own module in perl. I’m having problems with passing data to subroutines inside the module. I already have some subroutines that simply return a scalar when called and those work fine. This is what I have:

I have a module called NewModule.pm. So I did this:

my $obj = new NewModule;
my $string = “hello world”;
$obj->somefunction($string);

Inside NewModule.pm, I simply have this:

sub somefunction {
print $_[0];
}

Now I want it to print “hello world” to the screen, but instead I get “NewModule=HASH(0x8170d48)”. What stupid mistake am I doing? Thanks.


#2

Use this:
sub somefunction {
my $var = shift;
print $var;
}

Also, how is your module written? If you’re actually getting that print “NewModule=HASH()…” instead of “Hello World”, you might have something setup incorrectly in Perl.

Perl’s OO design is VERY hack job. It’s nothing like anybody else’s. It’s SO bad that Perl6 won’t even by backwards compatible with Perl5. ha


yerba# rm -rf /etc
yerba#


#3

You’re making a method call on an object. The first parameter will be the reference to the object. Why are you using $_ anyway? It makes your code hard to read.

Try using the following:

[code]sub something {

lazy way

my ($self, $foo, $bar) = @_;
}

sub something {

bit better

my $self = shift;

document $foo

my $foo = shift;

document $bar

my $bar = shift;
}

sub something {

more flexible

my ($self, %param) = @_;

document $foo

my $foo = $param{foo} || ‘default_value’;

document $bar

my $bar = $param{foo} || ‘default_value’;
}[/code]BTW, the last method would be called as:

$object->something(foo=>'passed_value', bar=>'passed_value');
:cool: [color=#6600CC]Atropos[/color] | openvein.org


#4

Oh yeah, duh. I’m a dumbarse. Oops. Likes I said, Perl’s OO is a Hack job. :wink:

sub something {
my $this = shift;
my $var = shift;
print $var;
}

That’s how I would do it.


yerba# rm -rf /etc
yerba#


#5

Here is the full code (test.pl & NewModule.pm-both in same directory). I’ve changed somefunction() but still get the exact same result. I’ve tried this on both my own linux machine and the dreamhost server and get a similar result. I feel stupid because I wrote a module about a year ago that did this and much more. But I accidentally deleted it and I’m trying to recreate what I did.

----------------test.pl-----------------------------------------
#!/usr/bin/perl -w

#use CGI;
#use Sloose::Factory;
use NewModule;
use strict;

#my $cgi = new CGI;
#my $sf = new Sloose::Factory;
my $obj = new NewModule;
my $string = “Hello, my name is Walt Whitman”;

$obj->somefunction($string);
#print $obj->returnme;

---------------NewModule.pm---------------------------------
package NewModule;

use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);

require Exporter;

@ISA = qw(Exporter);
@EXPORT = qw();
$VERSION = ‘0.01’;

sub new {
my $package = shift;
return bless({}, $package);
}

sub somefunction {
my $str = shift;
print $str;
}

sub printme {
print “hi there”;
}

sub returnme {
my $foo = “blarhg”;
return $foo;
}

1;
END


#6

Ok, durr. I reread your post, so the first parameter is the object reference. I must’ve been so used to calling subroutines within a script that I had totally forgotten about this. I’m now getting the result I wanted.

Thanks guice and Atropos7.