sub routine call


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sub routine call
# 1  
Old 11-10-2005
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,
# 2  
Old 11-10-2005
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;
# 3  
Old 11-10-2005
Quote:
Originally Posted by jim mcnamara
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.
The fact is a little bit more complicated.

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:
Originally Posted by jim mcnamara
you call a subroutine without parameters: ⊂
you call a subroutine by reference &$subref;
Also note that in these two cases the current @_ as a list of params will be passed to the called subroutine. So

⊂
&$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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PERL: Calling a sub routine from another module - help!!!

Hi, I am an occasional PERL user. I am trying to call a sub routine (passing parameters) in perl module from a driver .pl file. I have been "tinkering" for a while now and have confused myself. Could someone please look at the code below and spot where I am going wrong. testPerl.pl ... (0 Replies)
Discussion started by: chris01010
0 Replies

2. Shell Programming and Scripting

Paramerter pass for function(sub routine) need help

Hi, Please help me here while passing the paramert to fuction i am facing problem. i tryied passing 7 PARAMeter in side single quote,double quate even tried tild sign not working. how can assign it properly . usage () { typeset -i NumPARAMs=$1 typeset -i PARAM1=$2 typeset PARAM2=$3... (3 Replies)
Discussion started by: nitindreamz
3 Replies

3. Shell Programming and Scripting

Return a hash table from a sub routine

hello, i am new to scripting and would like to know how to return a hash table from a sub routine. i tried the following, my %hash_function = (); hash_function = &return_hash(); sub return_hash { my %hash = (); ///populate the hash return %hash; } but it dosent seem to... (1 Reply)
Discussion started by: hemalathak10
1 Replies

4. Shell Programming and Scripting

Perl - Call a sub routine in a command

Hi all I have written a simple perl script that has different options i.e. myscript -l -p etc i have it so when it runs without any switches it runs a subroutine called nvrm_norm i want to be able to do a -p option and run pall -w -f and then called the subruotine pall is... (1 Reply)
Discussion started by: ab52
1 Replies

5. Shell Programming and Scripting

awk routine help

Hi, I use awk but not as a programming language. Just generally in piplelines to split things out by fields. I am trying to accomplish this one thing that I think a short awk routine would do great for, but can't figure it out. Lets say I have a file that contains database columns. The file... (25 Replies)
Discussion started by: fwellers
25 Replies

6. Shell Programming and Scripting

File exists routine

Hello experts, I need some help here.. I've written the following routine to check for existence of files. The routine does the following. It will look for a compressed ( .Z ) file and if it exists, then it will uncompress it, if it is already uncompressed, then it will just diplay a message... (9 Replies)
Discussion started by: kamathg
9 Replies

7. Shell Programming and Scripting

how to cp files to dir,using routine?

hi all, I wanted to know how we can copy files to dirs, through a routine and when the file and the dir are specified as parameters for that routine and explicitly called? Eg: suppose i want to copy file1 to /tmp then myproc() { . . } myproc /path/file1 /tmp/ These parameters when... (4 Replies)
Discussion started by: wrapster
4 Replies

8. UNIX for Advanced & Expert Users

how to cp files to dir,using routine?

hi all, I wanted to know how we can copy files to dirs, through a routine and when the file and the dir are specified as parameters for that routine and explicitly called? Eg: suppose i want to copy file1 to /tmp then myproc() { . . } myproc /path/file1 /tmp/ These parameters when... (1 Reply)
Discussion started by: wrapster
1 Replies

9. UNIX for Dummies Questions & Answers

Routine Task being a Solaris Administrator

Hi, What are routine task being a solaris administrator ? Thanks, Far (1 Reply)
Discussion started by: Far
1 Replies

10. Programming

Entry Points Routine

How do we pronounciate bdevsw and cdevsw Kernel resources ? I presume it as block or charcter device software table. Am I Correct in my assumption ? Thanks in advance. (9 Replies)
Discussion started by: S.P.Prasad
9 Replies
Login or Register to Ask a Question