Solved

How to call a subfunction with input and return values in another subfunction in PERL?

Posted on 2011-09-13
5
241 Views
Last Modified: 2012-05-12
Hi,
I have this code as a sub-function (submitFileParser) in another perl code. How can I call this function in another sub-function (processFile) with return values of "@Options, @ALLFiles, @CSS, @RRS, @CRS" ?  and the input ($cache) is another variable in the other sub-function (processFile).

Then I will use these return values in the other sub-function .

Example: (It will be something like this)
sub processFile{

[@Options, @ALLFiles, @CSS, @RRS, @CRS] = submitFileParse($cache);

then I will use these return calues in here
}

Open in new window


      
sub submitFileParser{
my $filename = shift;
my @paragraphs;
{
#   local($/) = '';
    open( FILE, "< $filename" ) or die "Can't open $filename : $!";
    @paragraphs = <FILE>;
    close FILE;
}
our @HEADERS = ("Files", "Comments", "Related Records", "Code Reviewers");

sub read_paragraphs (@) {
	# read lines as parameters
	my @rippedParagraphs = @_;

	# Storage for all sections
	my (@CRS, @RRS, @CSS, @ALLFiles, @Options);
	# Temporary storages for single section of each type
	my (@Files, @CR, @RR, @CS);
	# Flags for file traversal logic
	my ($opt_flag, $file_flag);

	#read the file
	for ( @rippedParagraphs ) {
		# If we encounter a comment or empty string
		if (/^\#/ || !/\S/) {
			# we haven't encountered an option to start doing anything
			next unless $opt_flag || $file_flag;
			# If we're in file reading stage
			if ($file_flag) {
				# Means section reading is over, let's append to the general
				# storage arrays
				push(@ALLFiles, [@Files]) if @Files;
				push(@CRS, [@CR]) if @CR;
				push(@CSS, [@CS]) if @CS;
				push(@RRS, [@RR]) if @RR;
				# and reset temporary storages
				@Files = @CR = @CS = @RR = ();
			}
			# If we're done with options, let's start reading file sections
			if ($opt_flag == 1) {
				$opt_flag = 0;
				$file_flag = 1;
			}
			elsif ($opt_flag > 1) {
				# Addresses the empty line within Options:
				$opt_flag--;
			}
			next;
		}
		if (/^Options/) {
			# We start reading options
			$opt_flag = 2;
			next;
		}
		# Matching beginning of the line to determine the type of the string
		# and placing it in temporary storage
                /^R(R|elated\sRecords):\s*(.*\n)/ && push(@RR, $_) && next;
                /^C(R|ode\sReviewer):\s*(.*\n)/ && push(@CR, $_) && next;
                /^C(S|omments):\s*(.*\n)/ && push(@CS, $_) && next;

#		/^CR:/ && push(@CR, $_) && next;
#		/^CS:/ && push(@CS, $_) && next;
#		/^RR:/ && push(@RR, $_) && next;
		# General text is either files or options info, depending on the
		# value of the option flag
		$opt_flag ? push(@Options, $_) : push(@Files, $_);
	}
	# Let's put the last read section into storages
	push(@ALLFiles, [@Files]);
	push(@CRS, [@CR]);
	push(@CSS, [@CS]);
	push(@RRS, [@RR]);
	# And we return the full datastructure - hashref of populated storage
	return {
		"Options"              => [@Options],
		"Files"                => [@ALLFiles],
		"Comments"             => [@CSS],
		"Related Records"      => [@RRS],
		"Code Reviewers"       => [@CRS],
	};
}
} # submitFileParser

Open in new window

0
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:parparov
ID: 36530527
Your suggestion botches the return values since Perl concatenates several lists into one single list without any delimiters between former ones.
The best way would be:
our @HEADERS = ("Files", "Comments", "Related Records", "Code Reviewers");
sub processFile ($) {
  my $cache = shift;
  my $cache_data = submitFileParser($cache);
  # then use $cache_data like I used it in printing.

}
sub submitFileParser ($) {
  my $filename = shift;
  my @paragraphs;
  local($/) = '';
  open( FILE, "< $filename" ) or die "Can't open $filename : $!";
  @paragraphs = <FILE>;
  close FILE;
  return read_paragraphs (@paragraphs);
}
sub read_paragraphs (@) {
...
}

Open in new window

0
 

Author Comment

by:Tolgar
ID: 36530586
Sorry I am not very clear about how to do it.

Is it possible if you can send me the entire code?

I appreciate for your help.

Thanks,
0
 
LVL 9

Expert Comment

by:parparov
ID: 36531306
What entire code do you need? The sub read_paragraphs remains the same, you may also copy the print_* subroutines from the previous questions and call them in line 6 like
 print_all_data_by_type($cache_data);

Open in new window

0
 

Author Comment

by:Tolgar
ID: 36531646
But i don't wanna print them to anywhere. I just want these variables to be available in the subfunction processFile after I call submitFileParser.

So if I just call this function, are all the return values of submitFileParser be reachable in scope of processFile?

Thanks,
0
 
LVL 9

Accepted Solution

by:
parparov earned 500 total points
ID: 36531710
Exactly. $cache_data will have all the information.
I just said that you can see how the print_* functions access them as an example.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question