![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to cp files to dir,using routine? | wrapster | Shell Programming and Scripting | 4 | 05-21-2008 11:41 PM |
| how to cp files to dir,using routine? | wrapster | UNIX for Advanced & Expert Users | 1 | 05-21-2008 03:18 PM |
| how to differentiate system call from library call | muru | UNIX for Advanced & Expert Users | 2 | 07-20-2007 12:20 AM |
| Routine Task being a Solaris Administrator | Far | UNIX for Dummies Questions & Answers | 1 | 02-09-2005 10:19 AM |
| Entry Points Routine | S.P.Prasad | High Level Programming | 9 | 10-21-2002 12:34 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
sub routine call
in windows machine...
C:\2\test>perl -version This is perl, v5.6.1 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail) ------------------------------------------ what is the difference b\w subroutine calls: sub_routine_name("-----"); and &sub_routine_name("-----"); thanks, |
|
||||
|
normally a sub can be called like a function - without the & in front as long as you have the ( ) part at the end so the interpreter can figure out it's a subroutine.
You must have the & when: you call a subroutine without parameters: ⊂ you call a subroutine indirectly by name &$subref(args); you call a subroutine by reference &$subref; |
|
||||
|
Quote:
The ( ) can be omitted as well, as long as the subroutine has already been defined at the point of the call. Code:
#!/usr/bin/perl -w
use strict;
sub echo {
my $str = shift;
print $str if (defined $str);
}
echo;
echo "Hello World!\n";
will not give any warnings at all. But if you put the sub at the bottom: Code:
#!/usr/bin/perl -w
use strict;
echo;
echo "Hello World!\n";
sub echo {
my $str = shift;
print $str if (defined $str);
}
The solitary "echo;" will give an error for bareword usage (if you remove the use strict, it will be a warning instead). If you place the & back in, it will now be okay: Code:
#!/usr/bin/perl -w
use strict;
&echo;
echo "Hello World!\n";
sub echo {
my $str = shift;
print $str if (defined $str);
}
So to "&" or not sometimes depends on the order you define and use subroutine and their references. For the other case, see below. Quote:
⊂ &$subref; is the same as sub(@_); OR &sub(@_); &$subref(@_); but not the same as &sub(); &$subref(); respectively. See perlsub manpage for total treatment of the subject. Last edited by cbkihong; 11-10-2005 at 08:47 PM.. |
![]() |
| Bookmarks |
| Tags |
| perl, perl shift, shift, shift perl |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|