Reputation: 67
In order to simplify some existing tools, I wanted to replace several arrays with an array of structs. So I made a simple example code to test the creation of the structs. The code used is as follows:
use strict;
use warnings;
#package Net_Node;
use Class::Struct Net_Node => [host => '$', access => '$', pass => '$'];
#struct( host => '$', access => '$', user => '$', pass => '$' );
my $example = new Net_Node;
$example->host('dir ip');
$example->access('TELNET');
$example->user('hjack');
$example->pass('buttstalion');
print "\n\n\n";
print "${example->host}\n";
print "${example->access}\n";
print "${example->pass}\n";
The issue is when I try to execute this script, I get this error:
Can't locate object method "host" via package "example" (perhaps you forgot to load "example"?) at test_2.pl line 15.
Can you please help me find out what do I need to correct??
Upvotes: 3
Views: 160
Reputation: 6808
Your code has an error in structure accessor and you missed 'user' in Class::Struct
definition
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
#package Net_Node;
use Class::Struct Net_Node => [host => '$', access => '$', user => '$', pass => '$'];
#struct( host => '$', access => '$', user => '$', pass => '$' );
my $example = new Net_Node;
$example->host('dir ip');
$example->access('TELNET');
$example->user('hjack');
$example->pass('buttstalion');
say "\n\n";
say $example->host;
say $example->access;
say $example->pass;
Output
dir ip
TELNET
buttstalion
Upvotes: 0
Reputation: 62227
Reading Class::Struct EXAMPLES, it looks like you need to call the struct
function:
use warnings;
use strict;
use Class::Struct;
struct(Net_Node => [host => '$', access => '$', pass => '$']);
my $example = Net_Node->new();
$example->host('dir ip');
$example->access('TELNET');
$example->pass('buttstalion');
print $example->host, "\n";
print $example->access, "\n";
print $example->pass, "\n";
This prints:
dir ip
TELNET
buttstalion
Also, it seems $example->host
won't interpolate inside double quotes.
Upvotes: 2