First, results of a search for read-only.

Read-only variable in foreach loop? References, Prototypes, and read-only values Modification of a read-only

Next, a first for me

F:\dev\vladb>perl -MO=Deparse readonly.pl Can't call method "PADLIST" on an undefined value at C:/Perl/lib/B/Dep +arse.pm li ne 1039. CHECK failed--call queue aborted.
And now an attempt an an explanation.

perlsyn explains this well, but is going on when $path_list is the empty string (""), what your *ugly* code breaks down to is

foreach my $some_path ( (defined $path_list && $#$path_list >= 0) ? @{$path_list} : "" ) { clean_path($some_path); # $some_path is now "" ## i'd rewrite it as foreach my $some_path ( (defined $path_list and ref($path_list) eq 'ARRAY' and scalar @{$path_list} ) ? @{$path_list} : '') {
anyway, as I was saying, you ought to check if $path_list is a reference to an ARRAY, and if it's not, you ought to return one([] what I meant was, undef, or (), or maybe even @{[]}, but definetly not '', hopefullly you get the picture) instead of the empty string(""), because what happens is the same thing I demonstrate with the following one line perl -e"for my $orK(1,2,3,4) { $orK =~ s/.//g; } " which would also throw the read-only error. Why, because (1,2,3,$foo,$4) is not a data structure (an array). It is a list. The for loop implies list context. And since it is not an array, $orK, which becomes the alias for each element of the list, points to hard coded members of a list, which are read only. The following perl -e"@f=qw,1 2 3 4,;for my $orK(@f){ $orK =~ s/.//g; }" gets no read-only error. Why? cause $orK becomes an alias for each member of the list, which in this case is a reference to $f[0] which is a data structure. Its pretty much the same as perl -e"@f=qw,1 2 3 4,;for my $orK($f[0],$f[1],$f[2],$f[3]){ $orK =~ s/.//g; }" That's it. Try it out, read perlsyn again, read japhys excellent context tutorial (List is a 4 letter word, it's on his website, link to which is on his homenode).

I'm done.

 
______crazyinsomniac_____________________________
Of all the things I've lost, I miss my mind the most.
perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"


In reply to (crazyinsomniac) Re: How could I check if a variable is 'read-only'? by crazyinsomniac
in thread How could I check if a variable is 'read-only'? by vladb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.