Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

End of subroutine

by sandy_1028 (Initiate)
on Nov 07, 2008 at 05:02 UTC ( [id://722153]=perlquestion: print w/replies, xml ) Need Help??

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

Why 1 is used at the end of subroutine
sub functionname { # system("sh processkill $childpid"); print"time out entered into sigalrmhandler";###############TEM +P LINE-----------> PS if($cmd =~ /aaa/){ $tmpkill =""; # code here syscall(&SYS_close, fileno(CMD)); } 1;

Replies are listed 'Best First'.
Re: End of subroutine
by mpeever (Friar) on Nov 07, 2008 at 05:15 UTC

    The 1 isn't at the end of the subroutine: the subroutine ends with the '}'. The '1' typically marks the end of a module, as modules need to "return" a true value. From the Simple Module Tutorial:

    We need the 1; at the end because when a module loads Perl checks to see that the module returns a true value to ensure it loaded OK. You could put any true value at the end (see Code::Police) but 1 is the convention.

Re: End of subroutine
by marcussen (Pilgrim) on Nov 07, 2008 at 05:21 UTC

    It is used outside the subroutine, your question is actually: why is 1; used at the end of a module?

    A use statement (as in use module;) evals the contents of a file. The last expression in the file must evaluate to true, or the eval dies.

    UPDATE: Correction of my statement by ikegami (see below);
    Confucius says kill mosquito unless cannon

      A use statement (as in use module;) evals the contents of a file. The last expression in the file must evaluate to true, or the eval dies.

      Not quite. eval never dies based on the value returned by the evaluated expression.

      >perl -e"eval { 0 }" >

      require (and thus use) dies if the values returned by the module is false.

      >copy nul Module.pm 1 file(s) copied. >perl -e"use Module;" Module.pm did not return a true value at -e line 1. BEGIN failed--compilation aborted at -e line 1. >echo 1; > Module.pm >perl -e"use Module;" >

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-20 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found