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

I am running a Windows 7 box with ActiveState Perl v.5.26.1. The perl 5 interpreter crashes on this simple program--why?

#!perl use strict; use warnings; open INFILE, '<', $0; while (<INFILE>) { chomp; parser $_; } sub parser { print "Hello, world\n"; } __END__

Replies are listed 'Best First'.
Re: perl 5 interpreter crashes
by Laurent_R (Canon) on Aug 26, 2018 at 21:43 UTC
    This code line:
    parser $_;
    is wrong, because the parser subroutine has not been declared at this point and the Perl compiler does not know yet what parser is supposed to be.

    So either you give the compiler a clue that it is a subroutine call by putting the argument between parentheses:

    parser($_);
    or you define the parser subroutine beforehand:
    use strict; use warnings; sub parser { print "Hello, world\n"; } open INFILE, '<', $0; while (<INFILE>) { chomp; parser $_; } __END__
    which will duly print Hello, world a number of times.

    You could even define your parser subroutine afterwards as in your code, provided you pre-declare it before you use it:

    use strict; use warnings; sub parser; open INFILE, '<', $0; while (<INFILE>) { chomp; parser $_; } sub parser { print "Hello, world\n"; } __END__
    Note: all my three suggested corrections above run correctly under Windows.
Re: perl 5 interpreter crashes
by LanX (Saint) on Aug 26, 2018 at 20:00 UTC
    I think because the script is reopening itself (which is still opened because of the __END__ tag.)

    You know what $0 means?

    What's your goal?

    update

    If you really want to re-read the script, there are easier ways like reading from DATA after a seek 0

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      I do not think that is the issue. I chose $0 so that the code would be simple to read. If I replace the line that says "parser $_;" with "parser($_);" then it runs fine (Prints "Hello, world" 16 times). But I assume the interpreter should not crash...

        > I do not think that is the issue

        Do you think or did you try another variable?

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: perl 5 interpreter crashes
by Anonymous Monk on Aug 26, 2018 at 20:42 UTC
    I don't know why it crashes on your platform but Perl should give this error: Can't locate object method "parser" via package "#!perl" (perhaps you forgot to load "#!perl"?) at script.pl line 10, <INFILE> line 0.000000.

    Change line 10 to: parser($_);

      Thanks -- maybe my development environment is screwed up... time to install Linux I guess :)
        I certainly wouln't refrain you from using Linux, but your program runs correctly under Windows provided you look at the error message ("Can't locate object method "parser" via package...") and make the corrections accordingly.
Re: perl 5 interpreter crashes
by Anonymous Monk on Aug 26, 2018 at 21:29 UTC
    seems to have something to do with 64-bit builds on Windows, Strawberry Perl 5.26.2 64-bit crashes, 32-bit doesn't, Linux 64-bit doesn't
      I am on 64-bit windows (I should of said so) -- thanks for looking into this. I will type up a report for perlbug. :)
        I will type up a report for perlbug

        No need - this has been fixed in perl-5.28.0.

        Cheers,
        Rob