On Fri, 25 Nov 2011 10:50:01 +0100 timothy adigun <2teezp...@gmail.com> wrote:
> Hi Mike D, > Some comments on your codes: > Mike D <ekimduna...@gmail.com> wrote: > > > Considering the following code, are all my comments correct? > > > > # this function expects an array to be passed by reference > > sub foo > > { > > my ($thing1) = @_; # make a lexical variable for the array being passed > > > > Fine, but since you are getting a reference, I don't really think you need > the "()" > around $thing1, which gives a list context, with one element. > Better put: > my $thing1=shift @_; «my ($thing1) = @_;» is OK. «my $thing1 = @_;» is not OK, as it will take scalar(@_); which is the number of elements in it. «my $thing1 = shift(@_);» (or «my $thing1 = shift;» for short) does something a bit different and changes @_. > > > > for (@$thing1) # to access the whole array after referencing > > > for clarity use: for (@{$thing1}){...} It's not clearer, but both forms are equivalent. > > > { > > print $_."\n"; > > } > > print $thing1->[0]."\n"; # access single element in referenced array > > } > > > > my @array = (1,2,3,4); > > > my $array=[qw(1 2 3 4)]; > > > foo(\@array); # pass @array by reference to sub foo > > > foo ($array); You shouldn't call a scalar "$array" because it's confusing. It could be "$array_ref". And defining a @array and taking a reference to it is a valid strategy. > > your comments are ok, and may I say you should check out > perlreftut by typing * perldoc perlreftut * on your terminal [without the > stars], and if you will prefer to read the short tutorial as html file, > then you can type this on your terminal: > perldoc -oHTML -dreftutorial.html perlreftut > I believe this will help out alot. > > > > > It's pretty confusing, especially since BP uses prototypes during the > > example, which I'm told are bad? Never use them? > > > > Please, you can take time out to check on these links: > > http://docstore.mik.ua/orelly/perl/prog3/ch06_04.htm, > http://docstore.mik.ua/orelly/perl/cookbook/ch10_12.htm , > https://www.socialtext.net/perl5/prototype, > http://www.modernperlbooks.com/mt/2009/08/the-problem-with-prototypes.html > The first two links are to pirated copies of O'Reilly books. Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Escape from GNU Autohell - http://www.shlomifish.org/open-source/anti/autohell/ NASA Uses COBOL. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/