CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [Olive] Dereference an array: Edit Log



FishMonger
Veteran / Moderator

Oct 15, 2017, 9:29 AM


Views: 4709
Re: [Olive] Dereference an array

You've mentioned the use of subroutines, but none of your code snippets use subs and you don't give a clear description of how you're using them.


Quote
... would avoid the need to use the dereference operator (->) everywhere

Why do you think you would need to use the -> operator everywhere? Just dereference the passed array ref once at the start of the sub and be done with it.


Code
use 5.010; 
use warnings;
use strict;
use Data::Dumper;

my @letters = ("a","b","c","d");
@letters = letter2word(\@letters);
say Dumper \@letters;

sub letter2word {
my @letters = @{ $_[0] };
$letters[1] = 'hello';
return @letters;
}


If your goal is to not make a copy of the array or use -> to dereference, then do something like this:

Code
use 5.010; 
use warnings;
use strict;
use Data::Dumper;

my @letters = ("a","b","c","d");
@letters = letter2word(@letters);
say Dumper \@letters;

sub letter2word {
$_[1] = 'hello';
return @_;
}


If your goal is to use OO dot notation for method calls, you're not going to get that with perl 5, but you do get that with perl 6. I haven't starting working with perl 6 yet so I can't give an example, but there are plenty out there on youetube (look for Larry's YAPC talks). In his talks he often takes a perl 5 script and converts it step-by-step into perl 6.

Finally, as Bill already pointed out, DON'T use $a or $b (even in code snippet examples) outside of sort routines.


(This post was edited by FishMonger on Oct 15, 2017, 9:34 AM)


Edit Log:
Post edited by FishMonger (Veteran) on Oct 15, 2017, 9:34 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives