in reply to Help with building modules

You shift a bunch of data into various variables in the main body of the module...

... but you pass those bits of data into the new() method which doesn't do anything with them.

Take a look at the section on Constructors and Instance Methods in perltoot.

Replies are listed 'Best First'.
Re^2: Help with building modules
by neilwatson (Priest) on Jan 26, 2006 at 15:15 UTC
    If I put the shifts into the new() method.
    sub new { my $class = shift; my $self = {}; # Get message strings from subroutine call $msg = shift; # message text $app = shift; # application label $obj = shift; # object $sev = shift; # severity of message $msgg = shift; # message group $ser = shift; # service id return bless $self, $class; }

    The data still goes missing.

    Neil Watson
    watson-wilson.ca

      Given the use strict that code won't compile, so one presumes that you still have my $msg = shift; etc. That means that those variables are scoped to the new sub and disappear after it returns. You want to store the values off in your instance and then retrieve them from there later.

      package Foo; sub new { my $class = shift; my %self = @_; return bless \%self, $class; } sub do_something { my $self = shift; print "msg is: ", $self->{msg}, "\n"; } ## Meanwhile, back on the ranch . . . erm, in the main package package main; my $foo = Foo->new( msg => "WUBBA WUBBA" ); $foo->do-something;

      Those scalars drop out of scope and are lost. You should store the values in the $self hashref ... as the example I referenced previously shows.