Editorial_Response has asked for the wisdom of the Perl Monks concerning the following question:

I am not passing any argunments to my subroutines yet the global variable is not being passed/read/or written...cannot get it to print out. As you can see I am trying a few different things to get the data to print out but alas it does not. No matter what I try to do I cannot get the following line to print out its argument.
print "MY LINE IN: $my_line_in\n\n";
Your wizdom is greatly appreciated.
#!/usr/bin/perl # variable data to be input by the admin # $my_line_in=""; # contains the name of the section we are currently processing $processing_section; # open file to read the data open (INFILE, "readData.txt") or die("Cannot open the input file. $!") +; sub processSectionA1 { # Process data within the header # #### SECTION A1 (DO NOT REMOVE THIS LINE) # Process data print "In #### SECTION A1\n"; print "MY LINE IN: $my_line_in\n\n"; return; } sub processSectionD1 { # Process data within the header # #### SECTION D1 (DO NOT REMOVE THIS LINE) # Process data print "In #### SECTION D1\n"; print "MY LINE IN: $my_line_in\n\n"; return; } sub processSectionD2 { # Process data within the header # #### SECTION D2 (DO NOT REMOVE THIS LINE) # Process data print "In #### SECTION D2\n"; print "MY LINE IN: $my_line_in\n\n"; return; } LINE: while ( my $my_line_in = <INFILE> ){ chomp $my_line_in; # ignore blank lines next LINE if ($my_line_in =~ m/^$/); # debug print "In while loop POINT 1: $my_line_in\n"; # ignore comment lines with one hash tag # next LINE if ($my_line_in =~ m/^#{1,3}/); # debug print "In while loop POINT 2: $my_line_in\n"; if ($my_line_in =~ m/^#### SECTION A1.*/){ # debug print "In while loop POINT 3: $my_line_in\n"; $processing_section = "A1"; &processSectionA1; } elsif ($my_line_in =~ /^#### SECTION D1.*/){ # debug print "In while loop POINT 4: $my_line_in\n"; $processing_section = "D1"; processSectionD1(); } elsif ($my_line_in =~ /^#### SECTION D2.*/){ # debug print "In while loop POINT 5: $my_line_in\n"; $processing_section = "D2"; processSectionD2(); } else { # process hold value by processing last called subrout +ine if ($processing_section =~ /A1/){ # debug print "In while loop POINT 6: $my_line_in\n"; processSectionA1(); }elsif ($processing_section =~ /D1/){ # debug print "In while loop POINT 7: $my_line_in\n"; processSectionD1(); }elsif ($processing_section =~ /D2/){ # debug print "In while loop POINT 8: $my_line_in\n"; processSectionD2(); } } } close (INFILE);
and the data file looks like
#### SECTION A1 (DO NOT REMOVE THIS LINE) ## Data in section A1 Data in section A1 #### SECTION D1 (DO NOT REMOVE THIS LINE) ## Data in section D1 Data in section D1 #### SECTION D2 (DO NOT REMOVE THIS LINE) ## Data in section D2 Data in section D2
The following is what is print out:
[root@localhost admin_scripts]# ./processData.pl In while loop POINT 1: #### SECTION A1 (DO NOT REMOVE THIS LINE) In while loop POINT 2: #### SECTION A1 (DO NOT REMOVE THIS LINE) In while loop POINT 3: #### SECTION A1 (DO NOT REMOVE THIS LINE) In #### SECTION A1 MY LINE IN: In while loop POINT 1: ## In while loop POINT 2: ## In while loop POINT 6: ## In #### SECTION A1 MY LINE IN: In while loop POINT 1: Data in section A1 In while loop POINT 2: Data in section A1 In while loop POINT 6: Data in section A1 In #### SECTION A1 MY LINE IN: In while loop POINT 1: Data in section A1 In while loop POINT 2: Data in section A1 In while loop POINT 6: Data in section A1 In #### SECTION A1 MY LINE IN: In while loop POINT 1: #### SECTION D1 (DO NOT REMOVE THIS LINE) In while loop POINT 2: #### SECTION D1 (DO NOT REMOVE THIS LINE) In while loop POINT 4: #### SECTION D1 (DO NOT REMOVE THIS LINE) In #### SECTION D1 MY LINE IN: In while loop POINT 1: ## In while loop POINT 2: ## In while loop POINT 7: ## In #### SECTION D1 MY LINE IN: In while loop POINT 1: Data in section D1 In while loop POINT 2: Data in section D1 In while loop POINT 7: Data in section D1 In #### SECTION D1 MY LINE IN: In while loop POINT 1: Data in section D1 In while loop POINT 2: Data in section D1 In while loop POINT 7: Data in section D1 In #### SECTION D1 MY LINE IN: In while loop POINT 1: #### SECTION D2 (DO NOT REMOVE THIS LINE) In while loop POINT 2: #### SECTION D2 (DO NOT REMOVE THIS LINE) In while loop POINT 5: #### SECTION D2 (DO NOT REMOVE THIS LINE) In #### SECTION D2 MY LINE IN: In while loop POINT 1: ## In while loop POINT 2: ## In while loop POINT 8: ## In #### SECTION D2 MY LINE IN: In while loop POINT 1: Data in section D2 In while loop POINT 2: Data in section D2 In while loop POINT 8: Data in section D2 In #### SECTION D2 MY LINE IN: In while loop POINT 1: Data in section D2 In while loop POINT 2: Data in section D2 In while loop POINT 8: Data in section D2 In #### SECTION D2 MY LINE IN:

Replies are listed 'Best First'.
Re: Subroutine argument list
by hilitai (Monk) on Nov 16, 2011 at 00:53 UTC

    Hint: I think the key is in this line:

    LINE: while ( my $my_line_in = <INFILE> ){

    See if you can tell why.

      I tried it and you were exactly right. I declared the variable $my_line_in in two different places. I removed "my" and all was set. I really banged my head on the wall for about two hours on that one. The code for this is actually around 300 lines long and is the first code that I have written in two years in any language...my bad. It adds users, groups, locks and unlocks accounts on RedHat linux. Maybe when I am done I will open source it. I am sure that everyone that manages Linux/UNIX boxes needs it if they manage accounts in bulk. I promise I will log some serious time in reading, coding and being involved here on Perl Monks. I need too badly.

        It wouldn't have caught this problem for you, but a great general purpose life belt is to always use strictures (use strict; use warnings;).

        True laziness is hard work
Re: Subroutine argument list
by toolic (Bishop) on Nov 16, 2011 at 01:15 UTC
    You have a variable of the same name ($my_line_in) at two different scopes. The while loop uses my to localize that variable to the scope enclosed by the loop. It's value is not seen outside of the loop (or inside the sub's). Removing that "my" will give you what you probably expect to see, although it is not the best solution.
      Thank you. I will take a close look at those documents.