Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Re (tilly) 3: No excuses about not using CGI.pm

by MeowChow (Vicar)
on May 04, 2001 at 00:53 UTC ( [id://77773]=note: print w/replies, xml ) Need Help??


in reply to Re (tilly) 3: No excuses about not using CGI.pm
in thread variables not posting?

But it's not silent (under -w):
mysub(1); BEGIN { package Foo; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(mysub); sub mysub ($) { print "mysub: @_\n" }; } BEGIN { Foo->import(); } ## Output main::mysub() called too early to check prototype at C:\Projects\GPerl +\Test\g1.pl line 1. mysub: 1
Though I don't quite understand, if BEGIN blocks are executed before the rest of the containing file is parsed (so sayeth perlmod), why does this happen at all?
   MeowChow                                   
               s aamecha.s a..a\u$&owag.print

Replies are listed 'Best First'.
Re: Re: Re (tilly) 3: No excuses about not using CGI.pm
by merlyn (Sage) on May 04, 2001 at 01:09 UTC
    You got it in the wrong order. You can't execute a BEGIN block before you compile it, and you compile it in the order in the file. So you've compiled the lone mysub at the top, then you compile the first BEGIN block, then execute it, then compile the second BEGIN block, then execute it and OOPS! there's the problem.

    Put the lone mysub at the bottom of the file, and it'll be all good.

    -- Randal L. Schwartz, Perl hacker

      Right, I was commenting on tilly's statement that reversing the order is a good way to silently ignore the prototype.

      However, I wonder if the documentation in perlmod should be revised from:

      A BEGIN subroutine is executed as soon as possible, that is, the moment it is completely defined, even before the rest of the containing file is parsed.
      To something like:
      A BEGIN subroutine is executed as soon as possible, that is, the moment it is completely defined, even before subsequent code is parsed, but after the code before it is parsed and compiled.
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
        I can't think of a time I would have expected it to be parsed before the code ahead of it is parsed. I mean, the compiler doesn't skip around willy-nilly in the source file!

        Are you familiar with languages where the parser skips around? And if so, where does it skip to without, uh, parsing the file? {shrug}

        -- Randal L. Schwartz, Perl hacker

Re (tilly) 5: No excuses about not using CGI.pm
by tilly (Archbishop) on May 04, 2001 at 01:11 UTC
    It is silent for me in 5.005_03.

    But try the following:

    mysub(1); BEGIN { package Foo; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(mysub); sub mysub ($) { print "mysub: @_\n" }; } BEGIN { warn("About to import\n"); Foo->import(); warn("Imported\n"); }
    I suspect that the warning comes when it imports and realizes that it has already parsed code whose meaning is being changed by the prototype. And the way I said that should go a long way to answering your question.

    BEGIN blocks are executed once the BEGIN block is parsed. That means that they run before later code is parsed, and after prior code is already parsed. This is also why BEGIN blocks run in the order they appear in the file...

      Strange, I only see this warning in Win32 AS Perl 5.6; it's not present under Linux 5.6.1/5.00503 or even Cygwin 5.6. With your variation, the warning comes out afterwards, so it looks as if the first line is the problem:
      About to import Imported main::mysub() called too early to check prototype at C:\Projects\GPerl +\Test\g1.pl line 1. mysub: 1
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
        On the contrary, the warning does occur under perl 5.6.1 on Linux, as well as perl 5.6.0 and perl 5.7.1. Are you sure you had warnings turned on for all your tests?

        The warning doesn't occur in 5.005_03 because the warning wasn't implemented until 5.6.0. :)

        Anyway, this warning is actually produced at the end of the compile phase, after the entire script has been compiled:

        warn "About to call\n"; mysub(1); warn "Called\n"; BEGIN { package Foo; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(mysub); sub mysub ($) { warn "mysub: @_\n" }; } BEGIN { warn("About to import\n"); Foo->import(); warn("Imported\n"); } CHECK { warn "Done compiling.\n" } INIT { warn "Running.\n" } __END__ About to import Imported main::mysub() called too early to check prototype at tmp.pl line 4. Done compiling. Running. About to call mysub: 1 Called
        Then it is a run-time check.

        BTW the fact that ActiveState's Perl does not correspond to things built from source bothers me...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://77773]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found