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

I am learning Perl using a book downloaded from the internet.

Opal is a system for charging fares on public transport in NSW Australia. I have had four Opal cards and they identified by 16 digit numbers. I have four target directories into which the files related to these cards go and I have tried to set up a hash to map between card numbers and the destination directories but it does not work and the error messages make no sense to me.

This is the current form of the hash definition which is at line 26 in the script:-

my %OCN_PFX_To_TDir=( 3085220111379778 => '3085220111379788_Rexx', 3085220334620644 => '3085220334620644_Spare', 3085220346432582 => '3085220346432582_Rexx2', 3085220352420034 => '3085220352420034_Algol' ); This is the input followed by the error messages I get:- carl@md-tower-004:~$ perl ~/bin/copy_opal1.plx Can't modify constant item in scalar assignment at /home/carl/bin/copy +_opal1.plx line 26, near ");" Execution of /home/carl/bin/copy_opal1.plx aborted due to compilation +errors (#1) (F) You aren't allowed to assign to the item indicated, or otherwi +se try to change it, such as with an auto-increment. Uncaught exception from user code: Can't modify constant item in scalar assignment at /home/carl/bin/ +copy_opal1.plx line 26, near ");" Execution of /home/carl/bin/copy_opal1.plx aborted due to compilat +ion errors.

I am writing a small program to copy Opal related files from my downloads directory to the appropriate destination directories.

Basically it tells me that I am trying to assign a scalar value to the % sign representing the modulus operator which cannot receive such a value but the % sign is to mark the variable as being a hash. I have tried various changes on the definition, originally I had both keys and values in double quotes now I have the values in single quotes, originally I used commas to separate key value pairs. No matter what I change the error messages remain the same.

It may be that the book I downloaded is old and some syntax suggested in the book no longer works but no matter how many queries I have put into Google and how many AI overviews I have seen and hit list links that I have followed nothing has confirmed this.

Replies are listed 'Best First'.
Re: Problem Declaring a hash
by Corion (Patriarch) on Nov 05, 2025 at 20:39 UTC

    You didn't show the whole (reduced) program, but my guess is that in line 25, your are missing a semicolon. When I have the following program:

    #line 25 my $x = '123' = my %OCN_PFX_To_TDir=( 3085220111379778 => '3085220111379788_Rexx', 3085220334620644 => '3085220334620644_Spare', 3085220346432582 => '3085220346432582_Rexx2', 3085220352420034 => '3085220352420034_Algol' );

    I get the error output

    Can't modify constant item in scalar assignment at tmp.pl line 31, nea +r ");" Execution of tmp.pl aborted due to compilation errors.

    So maybe your problem is some lines earlier.

    If you can't spot it, copy the file and start removing lines until the error goes away. Then, if you still can't spot it, post the smallest version of your file that still reproduces the error. See also https://www.sscce.org/ - "smallest, self-contained example".

      Much thanks Corion. Your answer is spot on, I looked at the preceding code and there was a declaration statement that lacked the terminating semi colon.

      Added the semi colon and it ran OK. It did not do anything because in deleting code to find possible problem sources I had deleted all active parts of the program.

      One other question. How do I mark my question solved?

      Regards TEO.

        one possibility is editing the subject of your original question to start with "[SOLVED]" or words to that effect,
        and possibly add some note for posterity¹, what the solution was (either as a reply, like you just did 👍, or as an update of the original post).

        ¹ i.e. others with a similiar problem 😉 [Update: typo/wrong paronym corrected, thanks hippo]

Re: Problem Declaring a hash
by choroba (Cardinal) on Nov 05, 2025 at 22:06 UTC
    I've encountered this error many times. I usually trigger it by forgetting the second half of a fat comma, i.e. using = instead of =>.
    perl -e '%h = (a = 42)' Can't modify constant item in scalar assignment at -e line 1, near "42 +) "

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Problem Declaring a hash
by jwkrahn (Abbot) on Nov 05, 2025 at 20:40 UTC

    You show six lines of code, so which one is line 26?

    I can't see any problem with the code you provided. If you could provide some context, say two or three lines before and after the line provided, we may be able to help.

    Naked blocks are fun! -- Randal L. Schwartz, Perl hacker