I'm redoing old cryptic code and found this seemingly useful idiom for casting array refs back to arrays.
fit_data(\@array); sub fit_data { my $refCopy=$_[0]; *ydata=$refCopy; print "array contains: ",join(",",@ydata)," \n"; print "last index is $#ydata \n"; }
but I can't find a way to get this to work under 'strict', even if we excuse the glob part.
As the following 3 versions illustrate, I've tried declaring my @ydata as a lexical, and as a global outside the routine (which I'd like to get away from), but they all have their drawbacks. Is there a way to do this cleanly within a subroutine, and still use strict for your uses of the array?
use strict; my @valueArr = (1,2,3,4); fit_data(\@valueArr); fit_dataLex(\@valueArr); fit_dataGlobal(\@valueArr); sub fit_data { no strict "vars"; my $refCopy=$_[0]; *ydata=$refCopy; print "array contains: ",join(",",@ydata)," \n"; print "last index is $#ydata \n"; #but you can't use strict on @ydata references } #if we define @ydata with my, it's not in the glob table, but strict i +s happy. sub fit_dataLex { use strict; my @ydata; my $refCopy=$_[0]; *ydata=$refCopy; #but @ydata not set print "array contains: ",join(",",@ydata)," \n"; print "last index is $#ydata \n"; } # if we create @ydata as global, we have to qualify it every time, # and dirty our global space with 'local' vars. @main::ydata = (); #to use a typeglob below, sub fit_dataGlobal { use strict; my $refCopy=$_[0]; *ydata=$refCopy; print "array contains: ",join(",",@main::ydata)," \n"; print "last index is $#main::ydata \n";}
In reply to aliasing arrays using typeglob under strict by RockyMtn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |