Timo, One can even pass hash to a subroutine with a little trick, 'cos the default argument of a subroutine is an array "@_". ** Check Example 2, in the code below.
I think this trick is formalized by context rule. @voo = ("boon", 12, "man", 88); %coo = @voo; my $line = ""; while(my ($key, $val) = each %coo){ $line.="$key => $val\n"; } print $line; On Sat, Jul 30, 2011 at 2:17 AM, timothy adigun <2teezp...@gmail.com> wrote: > Hi Rajeev, > with the link you provided, the statement "In Perl, you can pass only one > kind of argument to a subroutine: a scalar... a pointer (sort of)." was > made > Reference sub-topic. So, it will not be a total truth that one can pass > "only" one kind of argument to subroutine. > Generally in perl the subroutrine default argument is an array "@_", so > that > makes it possible to even pass arrays into subroutine! ** Check Example 1, > in the code below. > One can even pass hash to a subroutine with a little trick, 'cos the > default > argument of a subroutine is an array "@_". ** Check Example 2, in the code > below. > Finally, I believe that "one" the main purpose of reference in perl is to > help maintain the integrity of data passed to a subroutine. > In Code Example 3 below, two arrays were passed to a sub., inside the sub. > the two array merges to one, and lost identity, then printer with just one > for loop. > But one can keep these array intact, using reference as demonstrated in ** > Code Example 4! > To write Object oriented perl one might have to know reference well! Really > it like pointer or passing reference using pointer in c++. > > <CODES> > > #!/usr/bin/perl -w > use strict; > > ##Example 1################# > > sub getter(@); #declaration of subroutine > > my $list=""; > my @arr=qw( 12 timo kunle 067 23.90 come_hm); > > sub getter(@){ # defination of subroutine > foreach(@_){ > $list.="$_\n"; > }return $list; > } > > print getter(@arr); # print return value > > ################################################################## > ##Example 2################# > > my %hash=( fistname=>'larry', surname=>'wall', > street=>'earth', value =>'perl'); > > my $line=""; > sub getter_hash{ > my %hash=@_; # the trick convert ur @_ to %hash > while(my ($key,$val)=each %hash){ > $line.="$key => $val\n"; > } > return $line; > } > > print getter_hash(%hash); > > ################################################################### > ##Example 3################# > > sub getter3(@); #declaration of subroutine > > $list=""; > my @arr1=qw( 12 timo kunle 067 23.90 come_hm); > my @arr2=qw( US 23:46:13 local float GOP_DEBT -q34A); > > sub getter3(@){ # defination of subroutine > foreach(@_){ > $list.="$_\n"; > }return $list; > } > > print getter(@arr1,@arr2); # print return value > > ################################################################# > ##Example 4################# > > sub getter4($$); #declaration of subroutine > > my $count=0; > my @arr3=qw( 12 timo kunle 067 23.90 come_hm); > my @arr4=qw( US 23:46:13 local float GOP_DEBT -q34A); > > sub getter4($$){ # defination of subroutine > my ($val1,$val2)=@_; > foreach(@{$val1}){++$count; > print "$count:$_\n"; > } $count=0; > foreach(@{$val2}){++$count; > print "$count:$_\n"; > } > } > getter4(\@arr3,\@arr4); # print return value > > Regards. > > > > > On Fri, Jul 29, 2011 at 11:57 PM, Rajeev Prasad <rp.ne...@yahoo.com> > wrote: > > > Hello, > > > > from here: http://www.troubleshooters.com/codecorn/littperl/perlsub.htm > > > > i found: > > > > In Perl, you can pass only one kind of argument to a subroutine: a > scalar. > > To pass any other kind of argument, you need to convert it to a scalar. > You > > do that by passing a reference to it. A reference to anything is a > > scalar. If you're a C programmer you can think of a reference as a > pointer > > (sort of). > > > > is that still true? date on website is 2003... > > > > thank you. > > > > Rajeev > > > -- *Satajanus Nig. Ltd *