sub [ name ] [ (prototype) ] [ : attributes ] [ {statements;} ]
sub name [
(prototype) ]
Forward declaration. The actual subroutine will be defined later but this
allows us to refer to it now. The prototype is optional.
sub [ name ]
{statements;}
Normal subroutine definition. Any number of scalar arameters can be passed
to the subroutine and will show up in @_. Note that name IS optional. A function
can return a sub or a sub can be passed as a parameter. e.g. return sub
{ $_ += 1; };
sub name
(prototype) {statements;}
The prototype string helps to specify what this subroutine needs to recieve
in the way of parameters.
Parameters are passed to the subroutine as scalars in @_ and returned as list, scalar, or void.
The return value of a subroutine is the value of the last expression evaluated. More explicitly, a return statement may be used to exit the subroutine, optionally specifying the returned value
The interesting thing about & is that you can generate new syntax with it, provided it's in the initial position: sub try (&@) { my($try,$catch) = @_; eval { &$try }; if ($@) { local $_ = $@; &$catch; } } sub catch (&) { $_[0] } try { die "phooey"; } catch { /phooey/ and print "unphooey\n"; }; That prints "unphooey".
file: /Techref/language/perl/sub.htm, 2KB, , updated: 2004/2/11 17:02, local time: 2024/8/21 22:39,
3.144.91.72:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://piclist.com/tecHREF/language/perl/sub.htm"> Perl Subroutines</A> |
Did you find what you needed? |
![]() o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr. |
Ashley Roll has put together a really nice little unit here. Leave off the MAX232 and keep these handy for the few times you need true RS232! |
.