in reply to Re: Error with using structs as subroutine parameters
in thread Error with using structs as subroutine parameters

When I remove use Class::Struct line, I get a following error: Can't call method "value1" without a package or object reference

I figured the "new" line might be useless, but when nothing else worked I got desperate and figured that can't at least make things worse.

  • Comment on Re^2: Error with using structs as subroutine parameters

Replies are listed 'Best First'.
Re^3: Error with using structs as subroutine parameters
by almut (Canon) on Jun 08, 2009 at 09:08 UTC

    Here's an example that hopefully clears up the usage:

    use strict; use warnings; use Class::Struct; struct ( 'Structname', { value1 => '$', value2 => '$' } ); sub myfunction { my $mystruct = shift; # alternatively: my $mystruct = $_[0]; return $mystruct->value1." ".$mystruct->value2; } my $mystruct = new Structname; $mystruct->value1('foo'); $mystruct->value2(42); my $value = myfunction($mystruct); print "$value\n"; # "foo 42"

    (This also works if you put the sub in a separate file that you then require.)