Problem in subroutine calling


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in subroutine calling
# 1  
Old 01-17-2006
Problem in subroutine calling

Hi,
we can call the subroutines using two ways ....
1) calling subroutine name preceeded by & symbol.
2)Another one is without &symbol....
what is the diff b/w these two....
############################
#usr/bin/perl
fun;
sub fun
{
print "hi this is from perl\n";
}
############################
the above code is doesn't worked for me...... if i called like fun;
but i changed the subroutine call like this &fun;
then it works .............plz tell me what is happenning here.
In my project script, they called the subroutines either way....in that it works..........i tried this small experiment it doesn't worked.....plz help me out.
Thanks in advace...
Sarwan.
# 2  
Old 01-17-2006
Very simply put, if you have the subroutine definition before the actual invocation, then you can call the subroutine as is i.e. without the ampersand. The subroutine acts like a builtin, in this case.

In case its the other way round, i.e. invocation comes first, and then the definition, you would need the ampersand to let perl know that you are refering to the subroutine defined later.

There are other instances where you cannot avoid the & way of invoking subroutines.

See the Oreilly sample chapter - Subroutines
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Csh variable calling problem

First post on here. So I use csh shells for my research (physics... not a CS person). I am trying to rerun the same scripts, but there are ~10 files that have similar variables that I have to change for each different configuration, so I would like one central file for the variables I change that... (3 Replies)
Discussion started by: sabrepride
3 Replies

2. Shell Programming and Scripting

perl -Calling the Subroutine Only if the condition is met

Hello All, I am in the process of learning perl.I have a perl script and based on the arguments passed it would the appropriate subroutine that is defined in the script. Now, I need to check a value that is defined in the Environment variables and should call the subroutine only if the... (1 Reply)
Discussion started by: filter
1 Replies

3. Shell Programming and Scripting

Problem saving return value of subroutine in perl

Hi all, I have this code #This program read the triplets from file named "data" into #an array of array. use strict; use warnings; use Data::Dumper; use Graph; use Graph::Subgraph; my @S; while (<>) { push @S, ; } print "-----TRIPLETS-------\n"; print Dumper \@S; #Make... (6 Replies)
Discussion started by: rushadrena
6 Replies

4. Shell Programming and Scripting

Calling perl subroutine from shell script (sh)

Hi, ive a perl script, where it has a subroutine clear() in it, and i've one shell script which runs in background, from that shell script i wanted to call subroutine which is in perl script, that's perl script is not module, just simple script. Eg: perl script <test> #!... (4 Replies)
Discussion started by: asarunkumar
4 Replies

5. Shell Programming and Scripting

calling perl subroutine from perl expect module

All, Is it possible to call a subroutine from the perl expect module after logging to a system that is within the same program. My situation is I need to run a logic inside a machine that I'm logging in using the expect module, the logic is also available in the same expect program. Thanks,... (5 Replies)
Discussion started by: arun_maffy
5 Replies

6. Shell Programming and Scripting

Calling Function Problem

Hi, I had a scripts which calls two function. One function will call another function, script is working fine but the second function is not calling the first function. Below is the script #!/usr/bin/ksh fun1() { echo $DATETIME >> Test1.ksh return 0 } fun2() { typeset DATETIME=`date... (5 Replies)
Discussion started by: somu_june
5 Replies

7. Shell Programming and Scripting

calling problem in perl script

Hi , Here is my piece of code-- main(); sub main { $result = GetOptions ("LogDir=s" => \$LogDir, "Summary" => \$Summary, "Indiviual=s" => \$Individual , "Diagnostics=s" => \$Diagnostics, ... (1 Reply)
Discussion started by: namishtiwari
1 Replies

8. Shell Programming and Scripting

Calling a subroutine with arguments

Hello, I am having problem calling a subroutine with arguments, can any help? is the approach I am using correct? main() { # This is just a subset of the code #$b & $lnum is already define in this section of the code checkboard $b $lnum } checkboards() { ln=$lnum... (2 Replies)
Discussion started by: jermaine4ever
2 Replies

9. Shell Programming and Scripting

calling sed problem!!

please help: I want to append the word "world" after every line that ends with hello: $cat file hello hello$ sed '/hello/ a world' file sed: Function /hello/ a world cannot be parsed. What's wrong with the above command?? If a hit a new line it works: $ sed '/hello/ a\ > world' file... (1 Reply)
Discussion started by: andy2000
1 Replies

10. Shell Programming and Scripting

shell script calling problem

Hi all, I am calling one shell script from other ...as follow ---calling_proc---code line_no=10 proc_name='test' echo "set verify off feedback off pagesize 0 select count(*) from tp_mpolicy2 " | sqlplus -s/@CMKT | read cnt if ; then export $line_no $proc_name global_proc... (2 Replies)
Discussion started by: dhananjaysk
2 Replies
Login or Register to Ask a Question