Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime Error "Can't modify non-lvalue subroutine"

Tags:

perl

I have an array @jobs that contains 1 or more strings, but when I run the following code I get a runtime error "Can't modify non-lvalue subroutine call at /home/xa341d/bin/hourly.pl line 32"

elsif (@jobs > 0) {
    my $my_jobs = "";
    my $i;

   for ($i = 0; i <= $#jobs; i++) {
       my $temp = $jobs[$i];
       $my_jobs += "-j $temp ";
   }

   print "my_jobs = $my_jobs\n";
    open $alOut, "/home/zn035b/bin/autorep2.pl $my_jobs -l 0 | grep `date \"+%m/%d/%Y\"` | sort -k 3,3|" or die "Can't open: $!";
}

does anyone know what might be going on here?

like image 967
Brandon Avatar asked Aug 24 '26 20:08

Brandon


2 Answers

A Perl programmer would write that foreach loop as:

foreach my $job (@jobs) {
  $my_jobs .= "-j $job ";
}

Or perhaps:

$my_jobs .= "-j $_ " foreach @jobs;

C-style for loops are rarely used in Perl. And for good reason. A foreach loop is usually far easily to understand.

You could also do it with map:

my $jobs = join ' ', map { "-j $_" } @jobs;
like image 95
Dave Cross Avatar answered Aug 27 '26 21:08

Dave Cross


Add this to the top of the script.

use warnings;
use strict;

You'll see that you're using i instead of $i in the loop, and also += instead of .= which should be used for concatenation. +=, on the other hand, adds numeric value of the right side (which is 0, since "-j" is not a number) to the left side.

That said, I failed to get the "non-lvalue sub" message out of this code so you probably should provide more info or at least tell us what line 32 is.

like image 38
Dallaylaen Avatar answered Aug 27 '26 22:08

Dallaylaen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!