please dont rip this site

Perl Subroutines

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.

\$
single scalar parameter
\@
single list
\%
single hash
*
bareword, constant, scalar expression, typeglob, or a reference to a typeglob. The value will be available to the subroutine either as a simple scalar, or (in the latter two cases) as a reference to the typeglob. If you wish to always convert such arguments to a typeglob reference, use Symbol::qualify_to_ref()
&
requires an anonymous subroutine, which, if passed as the first argument, does not require the sub keyword or a subsequent comma. eg ( {statements;} )
$
converts the parameter to scalar context
@
provides all the remaining parameters as a list
%
provides all the remaining parameters as a hash
; (semicolon)
separates mandatory arguments from optional arguments. It is redundant before @ or %

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: 2017/10/22 01:27,
TOP NEW HELP FIND: 
54.81.210.99:LOG IN

 ©2017 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?
Please DO link to this page! Digg it! / MAKE! / 

<A HREF="http://www.piclist.com/techref/language/perl/sub.htm"> Perl Subroutines</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2017 contributors:
o List host: MIT, Site host massmind.org, Top posters @20171022 RussellMc, Van Horn, David, James Cameron, Sean Breheny, IVP, alan.b.pearce, Neil, David C Brown, Bob Blick, Denny Esterline,
* 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.
 

Welcome to www.piclist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .