Consider the following example
#! /bin/perl
use strict;
sub sample
{
my @arr=(1,2,3,4);
return @arr;
}
my ($a,$b,$c,$d) = &sample;
print "$a\n$b\n$c\n$d\n";
In the above code, How can I get the $c without using the
arguments such as $a,$b.
I don't want to use any array to get the return values.
Answer Posted / guest
# Here is another solution
use strict;
sub sample {
my @arr = (1,2,3,4);
return \@arr; # return the array reference
}
my $aref = &sample;
print $aref->[2];
Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
package MYCALC; use Exporter; our @EXPORT = (); our @ISA = qw(Exporter); our @EXPORT_OK = qw(addition multi); our %EXPORT_TAGS = (DEFAULT => [qw(&addition)],Both => [qw(&addition & +multi)]); sub addition { return $_[0] + $_[1]; } sub multi { return $_[0] * $_[1]; } 1; Program: use strict; use warnings; my @list = qw (2 2); use Module qw(:DEFAULT); print addition(@list),"\n"; Above coding is my module MYCALC and the program which using this module, I have not exported any function using @EXPORT, but I have used the DEFAULT in %EXPORT_TAGS with the function addition, when I call this function from the main it says the error as,
What is cpan ? What are the modules coming under this?
What is the usage of -i and 0s options?
Explain '->' in perl?
I have one question regarding to eval function. I know eval function is use for error checking but I am not able to understand below line. eval \'exec perl -S $0 ${1+\"$@\"}\' if 0; $0 for script name $@ set if error occur
Why to use perl?
How interpreter is used in perl?
What is the purpose of redo statement?
Explain arrays in perl.
How to close a directory in perl?
How do you find the length of an array?
What is the difference between localtime() and gmtime() functions?
What $! In perl?
Define perl scripting?
What are some of the key features of objects in perl?