jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:
I have written a module which contains some oft used subroutines.
One routine is simply extracting data from a file into an array. I have written the code in a couple of ways, both seem to do the job ok:
# from wslib.pm sub gencli { open FH, '<', $gencli or die "Can't open $gencli $!"; flock (FH, 1) or die "Can't lock $gencli for reading: $!"; my @gencli = <FH>; close FH; return \@gencli; } #from script, subroutine call my $gencli_scalar = gencli(); my @gencli = @{ $gencli_scalar };
The other way
#from wslib.pm sub gencli { open FH, '<', $gencli or die "Can't open $gencli $!"; flock (FH, 1) or die "Can't lock $gencli for reading: $!"; @_ = <FH>; close FH; } #from script &gencli; my @gencli = @_; @_=();
In the former example can I get the scalar out of the subroutine without creating a sacrificial $gencli? Is the second way 'wrong'?
thank you
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Extracting an array from a module
by shmem (Chancellor) on Oct 13, 2007 at 09:05 UTC | |
by jonnyfolk (Vicar) on Oct 14, 2007 at 06:55 UTC | |
|
Re: Extracting an array from a module
by GrandFather (Saint) on Oct 13, 2007 at 23:32 UTC | |
by jonnyfolk (Vicar) on Oct 14, 2007 at 06:59 UTC |