in reply to Modification of a read-only value attempted
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 =~ /^-/
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Modification of a read-only value attempted
by josthi (Initiate) on May 21, 2020 at 17:30 UTC |