Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: BEGIN block and prototyped subroutines

by ikegami (Pope)
on Apr 18, 2010 at 08:30 UTC ( #835319=note: print w/replies, xml ) Need Help??

in reply to BEGIN block and prototyped subroutines

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://835319]
[stevieb]: ...down to implement now. Here's the sketch as it currently sits
[shmem]: well I use I2C and SPI and stuff, but creating a pseudo chip looks to me like lot of indirection and memory clutter... not?
[choroba]: Are you going to use the device soon? Related to your comment about "not having much time to do a lot of coding"...
[stevieb]: sure, but I'm just learning ;) I consider it practice to get a good understanding of what goes on *after* an I2C/SPI request is made
[shmem]: ah ok. Gonna read that. but now....
shmem compiles himself into his template
[stevieb]: this is my RPi:: distribution overall automation build system physical layout. It doesn't encompass all of my RPi dists yet, ...
[stevieb]: ...but I'm getting close. I've been soldering my own PCBs to gain that experience as well, before I have my own manufactured.
[stevieb]: heh, nice *template* ;) 'night shmem!

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2017-06-25 22:50 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (572 votes). Check out past polls.