SPUG: Odd read only error in 5.6 but not before?

David Dyck dcd at tc.fluke.com
Sun Apr 15 23:18:22 CDT 2001


On Sat, 14 Apr 2001, Chris Wilkes wrote:

> This little program fails with a "modification of read-only
> variable" error at the while(<FOO>) statement under 5.6 but not under
> previous versions.
>
> I can modify it to do a "foreach my $letter ..." and then send down
> "compit($letter)" which then works.
>
> Am I missing something here?


you can see that the foreach loop is setting $_ to
each readonly constant "a.txt" and "b.txt", right?
the while (<FOO>)  loop tries to assign to $_

The following patch creates a localized copy of $_

--- c.pl.orig	Sun Apr 15 21:12:23 2001
+++ c.pl	Sun Apr 15 21:12:47 2001
@@ -8,6 +8,7 @@
 sub compit {
   my $file = shift;
   open (FOO, $file) || die "Can't read file '$file'\n";
+  local ($_);
   while (<FOO>) {
      chomp;
   }

> #!/usr/bin/perl -w
>
> use strict;
> foreach (qw(a.txt b.txt)) {
>   compit($_);
> }
>
> sub compit {
>   my $file = shift;
>   open (FOO, $file) || die "Can't read file '$file'\n";
>   while (<FOO>) {
>      chomp;
>   }
>   close FOO;
> }


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list