The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-10-2005
sekar sundaram sekar sundaram is offline
Registered User
  
 

Join Date: Jul 2005
Location: banagalore
Posts: 44
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 (permalink)  
Old 11-10-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,807
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 (permalink)  
Old 11-10-2005
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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..
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:09 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0