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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.