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

this is the result of "a few simple questions" part 3
this deals with the changing of $/ while in a loop
heres the code:
#!/usr/bin/perl -w use strict; my @array; open(FILE, "<file1.txt") || die $!; local $/ = "****"; while (<FILE>) { chomp $_; push (@array, $_); $/= "####"; } close (FILE); my $i="0"; foreach (@array) { $i++; print "$i$_$i";}
Here is the file1.txt:
stuff****more stuff####this shouldnt show up
and here is the result that perl gives:
C:\Perl>perl test.pl 1stuff12more stuff23this shouldnt show up3
so in conclusion if you change the $/ inside a loop you cannot use local and the <> does give the rest of the file even if the file doesnt end with the $/
Any thoughts, reasons not use this in code? Hope you found this enlightening monks.

Edit title by tye

Replies are listed 'Best First'.
Re: FYI
by Cine (Friar) on Aug 23, 2001 at 21:52 UTC
    Why are you changing it each time? Why not use this instead:
    open(FILE, "<file1.txt") || die $!; { local $/ = "****"; my $a = <FILE>; chomp $a; push @array,$a; } { local $/= "####"; while (<FILE>) { chomp $_; push (@array, $_); } close (FILE); }


    T I M T O W T D I
      almost but that would produce an array like this
      @array = ("stuff", "stuff****more\nstuff", "the rest of the file")
      instead of:
      @array = ("stuff", "more\nstuff", "the rest of the file")
        Then put the two in the same scope and remove the second local...

        T I M T O W T D I
        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: FYI
by maddfisherman (Sexton) on Aug 23, 2001 at 21:46 UTC
    i guess because i couldnt use local with $/ i should set $/ ="\n" at the end of the loop.