Question

Regarding Perl, how can references be used in subroutines? Use code examples to support your reasoning.

Regarding Perl, how can references be used in subroutines? Use code examples to support your reasoning.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the following code for pass by references in perl.

Step 1: Declare two list variables @grade = ("one", "two", "three") and @grad1e = ("four", "five")

Step 2: Define function that handles two arguments and it will update the first variable by using the second argument "grade1".

Step 3: In the subroutine have additional variable "append" to be updated with the values from 2nd argument "grade1"

Step 4: Using for loop, iterate the "append" list and store it in "grade" variable which is a first argument.

Program:

USER>cat perlbyRef.pl


#!/usr/bin/perl

my(@grade) = ("one","two","three");
my(@grade1) = ("four","five");
print "Initial: @grade ";

&routineByReference(@grade, @grade1);

print "Updated grade via pass by reference : @grade ";

# This sub routine is used to update the grade variable via pass by reference
sub routineByReference

{
#copy the grade1 to append list variable because sub routines in perl accept only scalar variables
   my(@append) = @{$_[1]};

   my($temp);

#using a temp variable to traverse all the values from append list variable to grade variable.
#Eventually grade variable gets updated in this

   foreach $temp (@append)
   {
#push each value to argument 1, which is grade variable passed as reference
      push(@{$_[0]}, $temp);
   }
}

USER> perl perlbyRef.pl
Initial: one two three
Updated grade via pass by reference : one two three four five

Add a comment
Know the answer?
Add Answer to:
Regarding Perl, how can references be used in subroutines? Use code examples to support your reasoning.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT