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

Greetings Brothers and Sisters... When running Perl 5.8 on Linux I get a split loop error for the following....(perl 5.8 built for i386-linux-thread multi)
$line = $_; @outLine = split (/(<[^>]*>)/,$line);
Now, if I to the following instead it runs ok.
@outLine = split (/(<[^>]*>)/,$_);
And I do not get this error in either case when running on Solaris. (perl 5.8 built for sun4-solaris) Unfortunately i'm not able to fix this by replacing $line with $_ in my "real" code, so I'd like to figure this out. Any ideas? Thanks!

Replies are listed 'Best First'.
Re: split loop error
by PodMaster (Abbot) on Feb 03, 2003 at 02:30 UTC
    What's the exact error message?
    Is it a run-time or a compile-time error?


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      it's a run time error:
      Split loop, <> line 1.
      The program looks like this...
      while (<>) { ## fails on linux i386 multi thread $line = $_; @outLine = split(/(<[^>]*>)/,$line); ## works on linux i386 multi thread ## @outLine = split(/(<[^>]*>)/,$_); }
        There we go. It's very likely that it may be a bug. You're using one of those common idioms that a lot of people (including me), simply don't like. Here's how I'd write it (see <> in `perldoc perlop'):
        while(my $line = <>){ ... }
        Unless you're exploiting the magic of <> (you're simply reading of STDIN), or you have no idea there is any magic, i'd suggest you simply read off the filehandle you're using (as in <STDIN>).

        I personally like to go all the way and use defined, as in while(defined(my $line = <>)){...}.

        From perldoc perldiag

        Split loop
        (P) The split was looping infinitely. (Obviously, a split shouldn't iterate more times than there are characters of input, which is what happened.) See split in perlfunc.

        update: Try $line = "$_"; and see if that works (it may be that somehow $line was being aliased to $_ instead of a copy being made, and that somehow screwed up the length count -- wild guess).


        MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
        ** The Third rule of perl club is a statement of fact: pod is sexy.

        I find it hard to believe there could be a bug in something so commonly used as "while (<>)" or in the assignment of a scalar from $_. If there were, all sorts of other things would be breaking.

        The only thing that is slightly unusual about this split is the use of capturing parenthesis. Does the split still break if you take them out? (I know you need them to capture the angle brackets and what's inside them, but it might help to narrow down the bug if you tried that).

Re: split loop error
by John M. Dlugosz (Monsignor) on Feb 03, 2003 at 00:17 UTC
    Try some other change that you can do, such as moving that line to a small function.
      But should this fail?

      In other words, is Perl on Linux correct or is Perl on Solaris correct? Am I trying to do something that I should not, or is Linux not doing something it should!? Thanks, Ted
        It looks like a perfectly legitimate split statement either way to me, and it did not fail on my Linux version (at least with the simple input string  "abs <def> ddd <www>"). What input are you giving it?

        This is perl, v5.8.0 built for i686-linux-thread-multi
        Linux xxx.yyy.com 2.4.8-26mdk