Loading variables - Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loading variables - Perl
# 8  
Old 03-25-2010
Man Tyler, that's better that what I was trying to put together. Excellent explanation.
# 9  
Old 03-25-2010
Quote:
Originally Posted by adelsin
... 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:

Code:
$
$
$ cat -n blub8.pl
     1  #!/usr/bin/perl
     2  use strict;
     3  my $x = Config_Loader();
     4  print "Outside the function: $x\n";
     5  sub Config_Loader {
     6    $_ = "ABC123XYZ";
     7    if (/ABC(123)XYZ/){
     8      my $x = $1;
     9      print "Inside the function: $x\n";
    10      return $x;
    11    }
    12  }
$
$ perl blub8.pl
Inside the function: 123
Outside the function: 123
$
$

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 -

Code:
$
$
$ cat -n blub9.pl
     1  #!/usr/bin/perl
     2  use strict;
     3  my $x = Config_Loader();
     4  print "Outside the function: $x\n";
     5  sub Config_Loader {
     6    $_ = "ABC123XYZ";
     7    if (/ABC(123)XYZ/){
     8      my $x = $1;
     9      print "Inside the function: $x\n";
    10    }
    11    return $x;
    12    # won't work because $x is local to if block
    13    # and was discarded at the closing brace at line 10
    14    # BUT, "strict" won't complain because a GLOBAL $x
    15    # has already been declared, so $x at line 11 isn't
    16    # "without scope".
    17  }
$
$ perl blub9.pl
Inside the function: 123
Outside the function:
$
$

So, if you declare "my" local to the subroutine, rather than the if block, then you can also return from outside the if block -

Code:
$
$
$ cat -n blub10.pl
     1  #!/usr/bin/perl
     2  use strict;
     3  my $x = Config_Loader();
     4  print "Outside the function: $x\n";
     5  sub Config_Loader {
     6    my $x;
     7    $_ = "ABC123XYZ";
     8    if (/ABC(123)XYZ/){
     9      $x = $1;
    10      print "Inside the function: $x\n";
    11    }
    12    return $x;
    13    # works because $x is local to subroutine
    14    # and not the if block; it's available at line 12
    15  }
$
$ perl blub10.pl
Inside the function: 123
Outside the function: 123
$
$

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 -

Code:
$
$
$ cat -n blub11.pl
     1  #!/usr/bin/perl
     2  use strict;
     3  my $y = Config_Loader();
     4  print "Outside the function: $y\n";
     5  sub Config_Loader {
     6    my $x;
     7    $_ = "ABC123XYZ";
     8    if (/ABC(123)XYZ/){
     9      $x = $1;
    10      print "Inside the function: $x\n";
    11    }
    12    return $x;
    13    # works because $x is local to subroutine
    14    # and not the if block; it's available at line 12
    15  }
$
$ perl blub11.pl
Inside the function: 123
Outside the function: 123
$
$

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 -

Code:
$
$
$ cat -n blub12.pl
     1  #!/usr/bin/perl
     2  $x = Config_Loader();
     3  print "Outside the function: $x\n";
     4  sub Config_Loader {
     5    my $x;
     6    $_ = "ABC123XYZ";
     7    if (/ABC(123)XYZ/){
     8      $x = $1;
     9      print "Inside the function: $x\n";
    10    }
    11    return $x;
    12  }
$
$ perl blub12.pl
Inside the function: 123
Outside the function: 123
$
$

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.

Code:
$
$
$ cat -n blub13.pl
     1  #!/usr/bin/perl
     2  Config_Loader();                    # function was invoked, but return value was not assigned
     3  print "Outside the function: $x\n"; # Sorry, $x is unknown here because $x of the subroutine was discarded at line 12
     4  sub Config_Loader {
     5    my $x;
     6    $_ = "ABC123XYZ";
     7    if (/ABC(123)XYZ/){
     8      $x = $1;
     9      print "Inside the function: $x\n";
    10    }
    11    return $x;
    12  }
$
$ perl blub13.pl
Inside the function: 123
Outside the function:
$
$

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.
Thank you dday; appreciate it.

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

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)
Discussion started by: alex_5161
1 Replies

2. UNIX for Dummies Questions & Answers

Variables in perl script

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)
Discussion started by: mutley2202
0 Replies

3. Shell Programming and Scripting

Perl : Loading moduled in local directory.

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)
Discussion started by: popeye
1 Replies

4. UNIX for Dummies Questions & Answers

Global variables in perl

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)
Discussion started by: hemalathak10
1 Replies

5. Shell Programming and Scripting

Variables in perl

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)
Discussion started by: P3rl
1 Replies

6. Shell Programming and Scripting

using variables in perl not working

sdcprd@dotstoas110:$ echo $F2 1327332411 this works ---------- sdcprd@dotstoas110:$ perl -MPOSIX -le 'print strftime ("%m%d%y",localtime (1327332411))' 012312 <<<< correct date this doesnt ----------- sdcprd@dotstoas110:$ perl -MPOSIX -le 'print strftime ("%m%d%y",localtime... (10 Replies)
Discussion started by: aliyesami
10 Replies

7. Shell Programming and Scripting

Perl - Error loading module.

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)
Discussion started by: coolbhai
9 Replies

8. Shell Programming and Scripting

Help loading a program into perl from a file on desktop

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)
Discussion started by: silkiechicken
2 Replies

9. Shell Programming and Scripting

Function loading in a shell scripting like class loading in java

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)
Discussion started by: mpsc_sela
1 Replies

10. Shell Programming and Scripting

perl global variables

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)
Discussion started by: reggiej
3 Replies
Login or Register to Ask a Question