in reply to Re: Help with building modules
in thread Help with building modules

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

Replies are listed 'Best First'.
Re^3: Help with building modules
by Fletch (Bishop) on Jan 26, 2006 at 15:20 UTC

    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;
Re^3: Help with building modules
by dorward (Curate) on Jan 26, 2006 at 15:53 UTC

    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.