Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, In my script, I get the error "Can't modify non-lvalue subroutine call" with the following:
my $ok = "N"; my $zipcheck = Archive::Zip->new(); $ok = "Y" if ($zipcheck->read($arczip) = AZ_OK); unlink $originalfile if ($ok eq 'Y');
What's the graceful way to fix this? Tnx

Replies are listed 'Best First'.
Re: lvalue - howto fix?
by Nitrox (Chaplain) on Nov 20, 2002 at 20:39 UTC
    $ok = "Y" if ($zipcheck->read($arczip)== AZ_OK);

    First you need a comparative operator to check AZ_OK, and is AZ_OK a constant you've defined elsewhere?

    -Nitrox

Re: lvalue - howto fix?
by dreadpiratepeter (Priest) on Nov 20, 2002 at 20:41 UTC
    I would think that you want a comparison, not an assignment on line 3. I.e.:
    my $ok = "N"; my $zipcheck = Archive::Zip->new(); $ok = "Y" if ($zipcheck->read($arczip) == AZ_OK); unlink $originalfile if ($ok eq 'Y');

    Hope this helps,

    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: lvalue - howto fix?
by BrowserUk (Patriarch) on Nov 20, 2002 at 21:15 UTC

    If you enabled warnings (use warnings or -w) perl would prabably have told you what you were doing wrong. Eg.

    C:\>perl -we "print 'OK' if (1=1)" Found = in conditional, should be == at -e line 1. Can't modify constant item in scalar assignment at -e line 1, next cha +r ) Execution of -e aborted due to compilation errors. C:\>

    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

      Just wondering if eq should be used rather than == as AZ_OK looks more character-based than numerical to me...

        Actually, AZ_OK is defined as 0.

Re: lvalue - howto fix?
by pg (Canon) on Nov 20, 2002 at 21:06 UTC
    Just want to give you some more extended info on this. Hope those are useful.

    • Although it is not your purpose here, Perl does support l-value functions. An example would be the substr function:
      $str = "135"; substr($str, 1, 1) = "234"; print $str; This should give you "12345";
    • If you are not comparing numbers, but strings, then you should use 'eq', instead of '=='.
      $str1 = "01"; $str2 = "1"; print "equal as numbers\n" if ($str1 == $str2); print "equal as strings\n" if ($str1 eq $str2);
Re: lvalue - howto fix?
by ibanix (Hermit) on Nov 20, 2002 at 23:08 UTC
    Everyone else already hit the nail on the head, so I'll just throw out a few useful links:

    Perl Operators
    Archive::ZIP Frequently Asked Questions

    /rgrds

    <-> In general, we find that those who disparage a given operating system, language, or philosophy have never had to use it in practice. <->