Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Modification of a read-only value attempted

by josthi (Initiate)
on May 21, 2020 at 13:05 UTC ( [id://11117032]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All,

I am reading a file which contains symbols. The task to parse each line in the file to check for line matching (($data =~ m/^\-\w/) || ($data =~ m/^\-/)).

The regex worked for the lines which has no special symbols. When it parse the line which has symbols like

"-M--M-^VM-E=M-[ 4JM-1e+PM-^YM-;M-)M-^KM-u^SSZM-Fr^UnM-oM-`M-^XM-^^M-& +^SGM-HM-L^\^\+~pM-NM-&M-^BM-^@iM-^OM-j9rjM-^[M-2M-RtM-I}M-fT.M--w^ZM- +\E+^WM-wM-I&v^SM-EM-^S8^_E^C^ZM-#M-XM-D"M-EM-"M-^IM-Ef^VM-MhM-$FM-^HM +-b^B>M-^KM-RM-TzM-^BM-mM-[^UM-$M-LM-\HJM-^[^T]H}9af-IhpM-JM-H^LM-iM-9 +TM-^WM-^SM-[^NM-yM-MM-G>M-^OM-b^K|^YM-EE\M-^JM-b+tFM-q5^NM-)M-x&M-^JM +-KM-xVAM-|AM-{[AM-cM-}M-"wM-^HBM-fc_M-DwQtpM-Dn^VM-;YM-lgM-QM-^GM-~(M +-.M-`*M-UM-GM-]M-)M-Na^EM-5M-^K^YM-T^]]x_[M-XM-}M-w,~M- M-^[oM-([xY[M +-DM-wM-BM-(M-$M-oM-r{M-_M-"M-OI^QkM-(sM-MFiU0^^IQM-1`M-Eu%MUC]M-qM-^P +M-^TM-A^VM-f$}+M-GM-"8M-^NN^E^[M-=+M-;}M-PM-2sM-dM- M-^M5M-mM-6IM-^AM +-^K^G^EM-^M^NwUM-^HM-^KM-+M-^U^UlxM-^@M-^YM-KM-}^^M-1M-X~@M-KLM-IM-^N +M-oM-#M-.M-^MwwM-_^[0^G^KFVM-,M-h]9C+M-(MNM-jYM-ZM-mM-pz^^=M-^?M-2#[d +M-TM-m^EM-^C0bM-^[]M-y'YM-i^NAoM-^KM-lM-[2M-^ZeM-9^_M->PM-^RM-C^C^^M- +DM-~^KM-UM-"M-v%M-?!M-CM-jpF^CM-UM-^M^FAM-#M-Wd]M-$ZpM-PM-hYM-1M-%M-l +M-1M-^V~#$M-iWM-^JM-^J^RM-^^^LM-^DJx8M-^H5M-eM-,MM-NM-:M-YYM-^[M-^]M- +^Uf^DM--M-UPM-hM-E?EM-ri@iM-^A^O^QM-RM-6&M-^R^MsP^RM-^M3(IM-TM-xgM"

it is throwing "Modification of a read-only value attempted"

Regex tries to match if a line start with - or - followed by space.

Any idea on this?

Cheers,
Joseph

Replies are listed 'Best First'.
Re: Modification of a read-only value attempted
by choroba (Cardinal) on May 21, 2020 at 13:45 UTC
    Please, format your question properly. See Markup in the Monastery on how to do that.

    If you use some kind of representation of non-printable characters, please indicate which one (is it cat -v?).

    Matching doesn't modify anything, so it would be surprising for it to trigger the error you claim. And really, trying the following didn't throw any error:

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use IPC::Run qw{ run }; my $data = '-M--M-^VM-E=M-[ 4JM-1e+PM-^YM-;M-)M-^KM-u^SSZM-Fr^UnM-oM-` +M-^XM-^^M-&^SGM-HM-L^\\^\\+~pM-NM-&M-^BM-^@iM-^OM-j9rjM-^[M-2M-RtM-I} +M-fT.M--w^ZM-\\E+^WM-wM-I&v^SM-EM-^S8^_E^C^ZM-#M-XM-D"M-EM-"M-^IM-Ef^ +VM-MhM-$FM-^HM-b^B>M-^KM-RM-TzM-^BM-mM-[^UM-$M-LM-\\HJM-^[^T]H}9af-Ih +pM-JM-H^LM-iM-9TM-^WM-^SM-[^NM-yM-MM-G>M-^OM-b^K|^YM-EE\\M-^JM-b+tFM- +q5^NM-)M-x&M-^JM-KM-xVAM-|AM-{[AM-cM-}M-\'wM-^HBM-fc_M-DwQtpM-Dn^VM-; +YM-lgM-QM-^GM-~(M-.M-`*M-UM-GM-]M-)M-Na^EM-5M-^K^YM-T^]]x_[M-XM-}M-w, +~M- M-^[oM-([xY[M-DM-wM-BM-(M-$M-oM-r{M-_M-\'M-OI^QkM-(sM-MFiU0^^IQM- +1`M-Eu%MUC]M-qM-^PM-^TM-A^VM-f$}+M-GM-"8M-^NN^E^[M-=+M-;}M-PM-2sM-dM- + M-^M5M-mM-6IM-^AM-^K^G^EM-^M^NwUM-^HM-^KM-+M-^U^UlxM-^@M-^YM-KM-}^^M +-1M-X~@M-KLM-IM-^NM-oM-#M-.M-^MwwM-_^[0^G^KFVM-,M-h]9C+M-(MNM-jYM-ZM- +mM-pz^^=M-^?M-2#[dM-TM-m^EM-^C0bM-^[]M-y\'YM-i^NAoM-^KM-lM-[2M-^ZeM-9 +^_M->PM-^RM-C^C^^M-DM-~^KM-UM-\'M-v%M-?!M-CM-jpF^CM-UM-^M^FAM-#M-Wd]M +-$ZpM-PM-hYM-1M-%M-lM-1M-^V~#$M-iWM-^JM-^J^RM-^^^LM-^DJx8M-^H5M-eM-,M +M-NM-:M-YYM-^[M-^]M-^Uf^DM--M-UPM-hM-E?EM-ri@iM-^A^O^QM-RM-6&M-^R^MsP +^RM-^M3(IM-TM-xgM'; my %translate; for my $i (0 .. 255) { my $char = chr $i; run ['cat', '-v'], \$char, \ my $out; $translate{$out} = $char; } my $regex = join '|', map quotemeta, sort { length $b <=> length $a } +keys %translate; $data =~ s/($regex)/$translate{$1}/g; if (($data =~ m/^\-\w/) || ($data =~ m/^\-/)) { say 1; }

    Also note that a dash is not special in regexes outside of a character class, so there's no need to backslash it. Moreover, if the string starts with a dash, it also starts with a dash followed by a word character, so

    ($data =~ m/^\-\w/) || ($data =~ m/^\-/)

    is just a complicated way to write

    $data =~ /^-/

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Hi, Thanks for the quick reply. These weird characters came from the linux diff between the Jar files. Now we fixed the diff task and no more weird characters. The script working as expected. Thanks a log for spending your time on this. Cheers, Joseph
Re: Modification of a read-only value attempted
by hippo (Bishop) on May 21, 2020 at 13:19 UTC
     (($data =~ m/^\-\w/) || ($data =~ m/^\-/))

    I don't see what purpose the first match there has since it appears to be a special case of the second one. Can you explain?

Re: Modification of a read-only value attempted
by kcott (Archbishop) on May 22, 2020 at 05:50 UTC

    G'day Joseph,

    There are a number of ways in which that diagnostic message could be generated and the cause can be difficult to track down. Take a look at perldiag and search for "Modification of a read-only value attempted": it describes a number of subtle ways in which you might be attempting this modification that are not immediately obvious. Now look at your code, around the line number of the error, for something that's similar to what's described.

    I also note that your string uses interpolating double-quotes. Perl is not seeing what you perhaps think it's seeing. Using a cut-down version of what you posted:

    $ perl -E 'my $x = "...-L^\^\+~p...M-^@iM-^OM...M-\E+^W...M-\HJ..."; s +ay $x' ...-L^^+~p...M-^-^OM...M-+^W...M-HJ...

    If you use the strict and warnings pragmata, Perl will tell you about these types of problems. With the same cut-down string, here's a selection:

    $ perl -Mstrict -Mwarnings -E 'my $x = "...-L^\^\+~p...M-^@iM-^OM...M- +\E+^W...M-\HJ..."; say $x' Possible unintended interpolation of @iM in string at -e line 1. Useless use of \E at -e line 1. Unrecognized escape \H passed through at -e line 1. Global symbol "@iM" requires explicit package name (did you forget to +declare "my @iM"?) at -e line 1. Execution of -e aborted due to compilation errors.

    — Ken

Re: Modification of a read-only value attempted
by ikegami (Patriarch) on May 21, 2020 at 14:53 UTC

    You might have something like

    if (...) { <- Line number in error message ... } elsif (...) { <- Actual location of the error ... }

    Generally, run-time errors use the line number of the start of the statement.

    Update: This particular construct seems to give the correct line number since 5.10.

Re: Modification of a read-only value attempted
by AnomalousMonk (Archbishop) on May 21, 2020 at 14:10 UTC

    I agree with choroba's reply that the error you describe does not seem possible given the OPed code.

    Please see Short, Self-Contained, Correct Example and perhaps try to reproduce the problematic behavior in a small code example (using a clearly defined string, not a mish-mash). If what's going on doesn't become evident in the process of writing and running the SSCCE, you can post it for comment.


    Give a man a fish:  <%-{-{-{-<

Re: Modification of a read-only value attempted
by AnomalousMonk (Archbishop) on May 21, 2020 at 14:50 UTC

    Perl sometimes gets "confused" about line numbers for errors that occur within conditional blocks. E.g., for something like

    if ('s' =~ /s/) { # error message may report this line number ... ...; ...; for (qw(q r s t u)) { s{ s }{ t }xms; # ... for error on this line. } ...; ...; } # ... or this line number ...
    Unfortunately, I can't reproduce this behavior with some nice example code ATM, so you'll have to take my word. Be that as it may, take a look inside the if-block for problematic code, substitutions in particular. Also, see "Modification of a read-only value attempted" in perldiag for some other suggestions.


    Give a man a fish:  <%-{-{-{-<

Re: Modification of a read-only value attempted
by perlfan (Vicar) on May 21, 2020 at 17:13 UTC
    This string of line noise is being interpreted by perl, no?
    "-M--M-^VM-E=M-[ 4JM-1e+PM-^YM-;M-)M-^KM-u^SSZM-Fr^UnM-oM-`M-^XM-^^M-& +^SGM-HM-L^\^\+~pM-NM-&M-^BM-^@iM-^OM-j9rjM-^[M-2M-RtM-I}M-fT.M--w^ZM- +\E+^WM-wM-I&v^SM-EM-^S8^_E^C^ZM-#M-XM-D"M-EM-"M-^IM-Ef^VM-MhM-$FM-^HM +-b^B>M-^KM-RM-TzM-^BM-mM-[^UM-$M-LM-\HJM-^[^T]H}9af-IhpM-JM-H^LM-iM-9 +TM-^WM-^SM-[^NM-yM-MM-G>M-^OM-b^K|^YM-EE\M-^JM-b+tFM-q5^NM-)M-x&M-^JM +-KM-xVAM-|AM-{[AM-cM-}M-"wM-^HBM-fc_M-DwQtpM-Dn^VM-;YM-lgM-QM-^GM-~(M +-.M-`*M-UM-GM-]M-)M-Na^EM-5M-^K^YM-T^]]x_[M-XM-}M-w,~M- M-^[oM-([xY[M +-DM-wM-BM-(M-$M-oM-r{M-_M-"M-OI^QkM-(sM-MFiU0^^IQM-1`M-Eu%MUC]M-qM-^P +M-^TM-A^VM-f$}+M-GM-"8M-^NN^E^[M-=+M-;}M-PM-2sM-dM- M-^M5M-mM-6IM-^AM +-^K^G^EM-^M^NwUM-^HM-^KM-+M-^U^UlxM-^@M-^YM-KM-}^^M-1M-X~@M-KLM-IM-^N +M-oM-#M-.M-^MwwM-_^[0^G^KFVM-,M-h]9C+M-(MNM-jYM-ZM-mM-pz^^=M-^?M-2#[d +M-TM-m^EM-^C0bM-^[]M-y'YM-i^NAoM-^KM-lM-[2M-^ZeM-9^_M->PM-^RM-C^C^^M- +DM-~^KM-UM-"M-v%M-?!M-CM-jpF^CM-UM-^M^FAM-#M-Wd]M-$ZpM-PM-hYM-1M-%M-l +M-1M-^V~#$M-iWM-^JM-^J^RM-^^^LM-^DJx8M-^H5M-eM-,MM-NM-:M-YYM-^[M-^]M- +^Uf^DM--M-UPM-hM-E?EM-ri@iM-^A^O^QM-RM-6&M-^R^MsP^RM-^M3(IM-TM-xgM"
    In order to put this string into a variable with out perl throwing all sorts of weird things I've never even seen is to use quote meta (after trial and error):
    my $data = q/-M--M-^VM-E=M-[ 4JM-1e+PM-^YM-;M-)M-^KM-u^SSZM-Fr^UnM-oM- +`M-^XM-^^M-&^SGM-HM-L^\^\+~pM-NM-&M-^BM-^@iM-^OM-j9rjM-^[M-2M-RtM-I}M +-fT.M--w^ZM-\E+^WM-wM-I&v^SM-EM-^S8^_E^C^ZM-#M-XM-D"M-EM-"M-^IM-Ef^VM +-MhM-$FM-^HM-b^B>M-^KM-RM-TzM-^BM-mM-[^UM-$M-LM-\HJM-^[^T]H}9af-IhpM- +JM-H^LM-iM-9TM-^WM-^SM-[^NM-yM-MM-G>M-^OM-b^K|^YM-EE\M-^JM-b+tFM-q5^N +M-)M-x&M-^JM-KM-xVAM-|AM-{[AM-cM-}M-"wM-^HBM-fc_M-DwQtpM-Dn^VM-;YM-lg +M-QM-^GM-~(M-.M-`*M-UM-GM-]M-)M-Na^EM-5M-^K^YM-T^]]x_[M-XM-}M-w,~M- M +-^[oM-([xY[M-DM-wM-BM-(M-$M-oM-r{M-_M-"M-OI^QkM-(sM-MFiU0^^IQM-1`M-Eu +%MUC]M-qM-^PM-^TM-A^VM-f$}+M-GM-"8M-^NN^E^[M-=+M-;}M-PM-2sM-dM- M-^M5 +M-mM-6IM-^AM-^K^G^EM-^M^NwUM-^HM-^KM-+M-^U^UlxM-^@M-^YM-KM-}^^M-1M-X~ +@M-KLM-IM-^NM-oM-#M-.M-^MwwM-_^[0^G^KFVM-,M-h]9C+M-(MNM-jYM-ZM-mM-pz^ +^=M-^?M-2#[dM-TM-m^EM-^C0bM-^[]M-y'YM-i^NAoM-^KM-lM-[2M-^ZeM-9^_M->PM +-^RM-C^C^^M-DM-~^KM-UM-"M-v%M-?!M-CM-jpF^CM-UM-^M^FAM-#M-Wd]M-$ZpM-PM +-hYM-1M-%M-lM-1M-^V~#$M-iWM-^JM-^J^RM-^^^LM-^DJx8M-^H5M-eM-,MM-NM-:M- +YYM-^[M-^]M-^Uf^DM--M-UPM-hM-E?EM-ri@iM-^A^O^QM-RM-6&M-^R^MsP^RM-^M3( +IM-TM-xgM/; # didn't know what you were doing with '(($data =~ m/^\-\w/) || ($data + =~ m/^\-/)).', so I took liberty: print (($data =~ m/^\-\w/) || ($data =~ m/^\-/)) . '';
    I'd look at the assignment of that crazy string as the culprit.
Re: Modification of a read-only value attempted
by Anonymous Monk on May 21, 2020 at 14:01 UTC
    Also, when writing regexes to parse strings like this, watch out for "greedy." Ordinarily a regex will "greedily" try to match the longest possible string, and this is often the wrong thing to do here.
      Ordinarily a regex will "greedily" try to match the longest possible string

      No.

      # Matches 2 chars at pos 0, not 6 chars at pos 2. "acabbbbc" =~ /ab*c/

      Greediness applies to individual regex atoms, causing those to match as much as possible at the current position.

      Further to ikegami's reply:   The rule is to find the leftmost longest match. This has tripped me up more that once!


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11117032]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-18 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found