Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

struct::class HELP PLEASE

Status
Not open for further replies.

mreverman

Programmer
Dec 5, 2002
4
US
I have several pm files like so
xST.pm
xHeader.pm
x276.pm
xSeparator.pm

xHeader.pm includes (uses) xST.pm

x276.pm includes (uses) xHeader.pm

x276.pm includes (uses) xSeparator.pm

I create a new instance of a class like so:

my $new = new X_276.
The complete class looks like this
$new->header->st->function();

What I am trying to do is set a variable at this level
$new->SegmentTerminator("~").

But I want this value to be seen by
$new->$header->st->function();

Any suggestions on how I can do this? If this makes sense?
 
One way to do it is to have x276 objects inherit the function() from the xST class. to implement this, add to x276.pm
Code:
use base 'xHeader';
and this to xHeader.pm
Code:
use base 'xST';
You could also inherit directly from the xST class by adding
Code:
use base 'xST';
to x276.pm instead.

Then set the SegmentTerminator in x276.pm
Code:
sub SegmentTerminator {

    my ($self, $term) = @_;
    $self->{'segterm'} = $term;
}
Then for the following function defined in xST.pm
Code:
sub function {

    my $self = shift;
    print "segterm: $self->{'segterm'}\n";

}
You can call it with your X_276 objects
Code:
my $new = new X_276;

my $term = $new->SegmentTerminator('~');

print "term = $term\n";

$new->function();
This is a little convoluted, but you will have to share more about your pm's if you want a cleaner implementation.

jaa
 
This is how the program looks. This does not work

The first file is xST.pm

package xST;
use Class::Struct;
use strict 'vars';

my $TsIdCode_sz = 3;
my $TsControlNumber_sz = 9;

struct
(
'ST' =>
{
TsIdCode => '$',
TsControlNumber => '$',
}
);
sub ST::parseST
{
my $self = shift;
my $line = shift;
my $garb;
my $pattern;


$pattern = "\\" . $self->{'ElementDelimeter'};
print "[$pattern]\n";

$line = $self->StripSegmentTerminator($line);

($garb, $self->{TsIdCode},$self->{TsControlNumber}) = split /$pattern/,$line;
}
return 1;

Second file xHeader.pm
package xHeader;
use Class::Struct;
use base 'xST';
use base 'xBHT';

@ISA = qw(xST xBHT);
use strict 'vars';

struct
(
'Header' =>
{
st => 'ST',
bht => 'BHT',
}
);
return 1;

third file x276.pm
package x276;
use Class::Struct;
use base 'xHeader';
@ISA = qw(xHeader);
use strict 'vars';

struct
(
'X_276' =>
{
header => 'Header',
}
);

sub X_276::SegmentTerminator
{
my $self = shift;
$self->{SegmentTerminator} = shift;
print $self->{'SegmentTerminator'}, "\n";
return $self->{SegmentTerminator};
}
return 1;

the test program is test.pl
#!/usr/bin/perl
use x276;
@ISA = qw(x276);

my $rec = new X_276;
$st = "ST*276*0003~";
$rec->SegmentTerminator("~");
$rec->header->st->ParseST($st);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top