... Is that possible too? returning the variable if my was declared in that subroutine? and still keeping that attribute?
Yes, it's possible to return a value from a subroutine. I'll assume you do want to retain the "strict" pragma, and I'll work on the short testcases. Hopefully, you'll find it easy to extend it to your script.
Note the rules -
(1) "my" variable inside an if block is local to that if block and you cannot access it outside the block.
(2) "my" variable inside a subroutine will be local to that subroutine, and you cannot access it outside the subroutine.
(3) If your subroutine has a "my" variable $x, then you can declare a variable $x outside the subroutine, but its scope will be different. Which means, it is not the same variable as the one inside the function. You will have to: return the value from the subroutine, assign the value to the outer variable and then you'll have the same value.
Use the "return" statement to return a value from a Perl subroutine:
Note the scope above. I used "return" inside the if block because "my" variable was inside the if block, and I could access it over there.
Note what happens if "return" is outside the if block -
So, if you declare "my" local to the subroutine, rather than the if block, then you can also return from outside the if block -
Using the same variable name inside and outside the function may not be easy on the maintainers of your code though. Use different names thusly -
Note that if you do not add "use strict" pragma, you can still use "my" for the variable inside the subroutine, and return its value to a similarly named variable outside the subroutine -
Once again, note that the variable declared at line 2 is not the same as the variable declared at line 5.
It just happens to have the same name. The reason I am able to display the value "123" at line 3 is because the "outer" $x was assigned the value of the "inner" $x at line 2.
So, what happens if the "outer" $x is not assigned the value at line 2 ? Can you still access the value of the "inner" $x ?
If you've understood everything till here, you'll know that it won't work.
HTH,
tyler_durden
---------- Post updated at 03:49 PM ---------- Previous update was at 02:41 PM ----------
Quote:
Originally Posted by dday
Man Tyler, that's better that what I was trying to put together. Excellent explanation.
I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open())
I would like to run another PERL-script from first one, not... (1 Reply)
Hi Chaps,
Im after some advise with a script i've written however doesnt appear to work how I would like.
Basically I have a perl script which sucessfully pulls an expect script to login to multiple nodes and obtain some output from the nodes (total number of nat ports that are in use... (0 Replies)
I have the need to load modules in a local directory. I am a perl new guy. When I test Net::SSH2, I get the following message. I did load the required libssh2.
Not sure where to go from here. Is there a way to test that libssh2 is loaded correctly ? (1 Reply)
hi all,
i need a help for the following query.
Thanks in advance for your valuable time.
i have a main.pl file which has a global variable declared as below.
our myVar=0;
call first.pl script from the main.pl script.
print the value of myVar (the value is still 0 and not 10.)
i have a... (1 Reply)
I don't fully understand variables in perl.
If we have a variable defined like this "my $number = 1" then this is called a lexical variable? But if you define this at the top of a script then why isn't it a global variable because it would be available throughout the file?
Sorry if this is... (1 Reply)
Hi,
I have a strange issue in my script.
When script is run from command prompt it runs fine,but when run from cron it exist with error message.
I narrowed down the issue and found that " use Mail::Sender;" is the culprit.
If I comment the statment the code runs fine in both command and... (9 Replies)
So I want to use this program that I have downloaded from: PaGE - Patters from Gene Expression
However, I am not sure how to actually get in to and run the program... I can log into the server, and was assuming I needed to get the "PaGE_5.1.6.pl" file into a folder some how, but not sure how to... (2 Replies)
Like class loader in java, can we make a function loader in shell script, for this can someone throw some light on how internally bash runs a shell script , what happenes in runtime ...
thanks in advance.. (1 Reply)
Can someone give me "the lecture" on why you shouldn't make all your varables global when programming in perl. I have been doing this but I have heard that it is not a good practice. (3 Replies)