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

Could someone explain to me what causes this behaviour ?

#!/usr/bin/perl -w use strict; my @subs = ("_iata_city"); foreach my $sub(@subs) { $sub =~ s/^_//g; print $sub;; } foreach my $sub(qw/_iata_city/) { $sub =~ s/^_//g; print $sub;; }

1) First loop = iata_city
2) Second loop = Modification of a read-only value attempted
I am assuming it has something to do with the _ in the qw// but am not sure why ?

Replies are listed 'Best First'.
Re: Modification of a read-only value attempted
by kyle (Abbot) on Dec 03, 2008 at 14:23 UTC
Re: Modification of a read-only value attempted
by almut (Canon) on Dec 03, 2008 at 14:25 UTC

    In addition to what the A.M. said, it's maybe worth pointing out that $sub is not a copy of, but rather an alias to the elements being iterated over — which explains why the modification is being attempted on the constant "_iata_city" itself.

Re: Modification of a read-only value attempted
by Anonymous Monk on Dec 03, 2008 at 14:19 UTC
    In the first case, $subs[0] is a variable you can freely edit.
    In the second case, '_iata_city' is a constant, which you can't modify.

    Nothing to do with underscores, just constants vs variables as the error message says.