Lvalue subroutines should have a single point of return – and not use the return keyword. So something like
my @x; sub foo :lvalue { my ($cond, $i) = @_; if ($cond) { return $x[$i]; } else { return $x[0]; } }
should instead be written as
my @x; sub foo :lvalue { my ($cond, $i) = @_; my $lvalue; if ($cond) { $lvalue = \$x[$i]; } else { $lvalue = \$x[0]; } $$lvalue; }
which is a general solution, or
my @x; sub foo :lvalue { my ($cond, $i) = @_; my $lvalue; ($cond) ? $x[$i] : $x[0]; }
Edit: further notes I see you are using the mem module, which is basically useless. If you need to perform an assignment at compile time, just use a BEGIN block:
our @fields; BEGIN { @fields=qw(one two three) }
While this is a little more verbose, you now don't need any non-standard modules. A similar rationale applies to the P package. It attempts to be very DWIM-y, but I find it's design distasteful. Here, explicitly using the builtin printf rather than the P function would have been clearer.
When posting minimal code snippets for others to reproduce a problem, then it's customary to remove such irrelevant dependencies – I don't shove my similar Util::Underscore in everyone's face.
In reply to Re: weird error message in middle-aged-perl(5.14)
by amon
in thread weird error message in middle-aged-perl(5.14)
by perl-diddler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |