Can I use Getopt::Long to set struct fields?
use strict;
use diagnostics;
use Getopt::Long;
use Class::Struct foo [ a => '$', b => '$' ];
my $foo = foo->new();
GetOptions("a=i" => \$foo->a, "b=i" => \$foo->b);
does not seem to work, “-a 10” does not seem to set $foo->a.
obviously, I would rather avoid
GetOptions("a=i" => sub { $foo->a($_[1]); }, ...);
also, I would rather keep the struct foo an array, not a hash.
\$foo->awill be a reference to the return value of$foo->a, which is certainly not what you want.You can probably use the ability of
GetOptionsto store the values in a hash reference, then pass that to your object’snew: