Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

You seem to misunderstand both BEGIN and prototypes.

Let's look at BEGIN first. It causes the enclosed code to be executed as soon as it is compiled.

For the following code:

print "a"; BEGIN { print "b"; } print "c";

the following happens:

  • print "a"; is compiled.
  • BEGIN { ... } is compiled.
    • print "b"; is compiled.
  • BEGIN { ... } is executed.
    • print "b"; is executed.
  • print "c"; is compiled.
  • Compilation finished. Execution begins.
  • print "a"; is executed.
  • print "c"; is executed.

BEGIN is completely useless around a sub definition because a sub definition doesn't produce any runnable code.

Now, let's look at prototypes. Prototypes affect how sub calls are parsed and they affect the code into which a sub call is compiled. Obviously, that means the prototype of a sub must be known at the time a call to the sub is encountered.

For the following code:

foo('bar'); sub foo($) { print "foo called: @_\n"; }
the following happens:
  • foo('bar') is compiled. foo hasn't been declared, so a stub with no prototype is created. No prototype exists, so parsing is not affected, and no check is done on the args.
  • sub foo($) { ... } is compiled. The prototype doesn't match the one that was previously declared, which means Perl used the wrong prototype when generating earlier calls to foo, so a warning is issued.
  • Compilation finished. Execution begins.
  • ...

You need to declare the sub before any calls to the sub are compiled. It's the only way the sub call can be affected by the prototype.

sob foo($); foo('bar'); sub foo($) { print "foo called: @_\n"; }

By the way, prototypes are by and large discouraged in Perl. They are not a good way to check the validity of arguments.


In reply to Re: BEGIN block and prototyped subroutines by ikegami
in thread BEGIN block and prototyped subroutines by hangon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-28 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found