Answer» here I am righting a perl script something like FOLLOWING :
Code: [Select]#!/usr/bin/env perl
#define some var...
sub function_main{
if (var eq some value){ Code_piece } else { Code_piece } } now here inside if and else some piece of code is calling in different conditions so I try to remove duplicate code here like :
Code: [Select] #!/usr/bin/env perl
#define some var...
sub function_main{
sub function_child{ Code_piece }
if (var eq some value){ call function_child() } else { call function_child() } }
So its giving me syntax error at the last line.
Here is the question : Can we define the Subroutine inside any existing subroutine? If yes How? what mistake I did here? If no then whats the other way ? because I dont want that piece of code outside of the function_main, as I am using some local variable of function_main in that code and if I create a subroutine(function_child) outside I need to pass some 20+ values while calling that subroutine function_child,.
THANKS
the two parts are the if are the same.
Code: [Select] if (var eq some value){ Code_piece } else { Code_piece } is equivalent to this:
Code: [Select]Code_piece
If there actually is different logic, REFACTOR it so that the duplicated code only get's called once.
Inside the if the code is calling for the loop .
I have an Array in which i have combination of array and normal var, so if the array element slected is an array that will go in the if condition and run that code for each element of array. and if the array element is normal var then I need to run that code once only.
Example:
Code: [Select]if (array{i} eq array) { foreach $var (array{i}){ code() } } else { code() }
If i dont use if and else condition how can I separate it?refactor it. That looks like it could be turned into a recursive routine.
Another method is to turn single elements into a single-element array and follow the iterative logic.
Thanks for the help .
Its working Now I missed one } at the end that why the error is comming I can WRITE the subroutine inside any other subroutine.A nested routine will access VARIABLES from their parent scope, but the nested routine will remain bound to the same variable from the first call. That is, when you call the parent subroutine again, the inner routine is still closed over the local variables from the first call, which are now orphaned. Example:
Code: [Select]#!/usr/bin/perl use strict; use warnings;
for (1..3) { print "run: [time $_]\n"; run(); } sub run{ my $counter = 0; inc(); inc(); sub inc{ $counter++; print "Counter = $counter \n"; } }
output:
Code: [Select]run: [time 1] Counter = 1 Counter = 2 run: [time 2] Counter = 3 Counter = 4 run: [time 3] Counter = 5 Counter = 6 As you can see, even though run explicitly sets counter to zero, every call to inc still increments by one. This is because the inc() routine binds to the $counter variable from the first call. If the routine in which you are adding nested functions is called more than once, it won't work as expected since all the nested functions will be closed over the first call, not the current one.
|