Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: (tye)Re3: Functions

by KM (Priest)
on Jan 30, 2001 at 02:24 UTC ( [id://55102]=note: print w/replies, xml ) Need Help??


in reply to (tye)Re3: Functions
in thread Functions

Yes? So you move the call to main below the block and you still don't need it in a BEGIN. You would squash a normal closure by doing this as well. The BEGIN isn't needed unless you design your script so it is.

#!/usr/bin/perl -w use strict; use vars qw($sub); #exit main(); # Useless use of closure below if called like this # BEGIN block would be needed in this case. { my $time = time(); $sub = sub {time - $time}; } exit main(); # No problem, no BEGIN needed. sub main { print &$sub; sleep 2; print &$sub; return 0; }

Cheers,
KM

Replies are listed 'Best First'.
(tye)Re4: Functions
by tye (Sage) on Jan 30, 2001 at 02:42 UTC

    It was one example of a case where the BEGIN is required. With the BEGIN, it works either way. The BEGIN block makes the code more robust. Moving the exit statement down defeats the whole purpose of that line (which is to prevent the maintainer from having to parse all of the lines between the exit and the sub main looking for hidden run-time code. It forces any run-time code to be put up top or into BEGIN blocks making it easy to find).

    Sure, it is easy to produce cases where you don't happen to call GlobalRoutine during the window between its definition and the initialization of $sub. That doesn't mean you won't get bit by it.

            - tye (but my friends call me "Tye")
      This has nothing to do with the original discussion, which I was responding in context to. This has nothing to do with the fact that in the example you gave, I said it didn't need a BEGIN block.. which it doesn't. Then when you start wrapping code around it you start saying that it does. Well, yeah.. when you make it need it, it needs it. Your original example doesn't.

      You could put all your initializations in BEGIN and all your function calls in END for all I care.. your first snippet still doesn't need a BEGIN.

      Cheers,
      KM

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-16 04:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found