In the Shiny Ball Book (Effective Perl Programming) under "Item 26: Pass references instead of copies" there is the "Using local * on reference arguments" subitem (pages 100-101). An example is given:
sub max_v_local { local (*a, *b) = @_; my $n = @a > @b ? @a : @b; my @result; for( my $i = 0; $i < $n; $i++ ) { push @result, $a[$i] > $b[$i] ? $a[$i] : $b[$i]; } @result; }
Presumably one would call this as follows:
my @answer = max_v_local( \@mylist, \@yourlist );
Can I use this typeglob trick with 'use strict'? If so then read on...

I thought this would be great to use in other circumstances but I keep running into problems. Here's some example code (The typeglob is near the middle in the foreach block; This snippet reads in a CSV file and generates an XML file; @c describes the CSV file; For the curious, grbr is an internal identifier).

sub test_xml_writer { my( @array, $aref ); my $io = new IO::File '<test.txt' or die( "IO Failure" ); my $csv = new Text::CSV_XS; while( 1 ) { $aref = $csv->getline( $io ); $#{ $aref } > -1 ? push @array, $aref : last; } $io->close(); $io = new IO::File '>test-out.xml' or die "Could not open output f +ile"; my $w = new XML::Writer( OUTPUT => $io, DATA_MODE => 1, DATA_INDEN +T => 2 ); $w->xmlDecl( undef , 1 ); $w->startTag( 'assets' ); foreach ( @array ) { $w->startTag( 'machine' ); my @c = qw( name ip_address snmp_community grbr model_serial ) +; local *a = $_; # my @a = @{$_}; $w->dataElement( $c[0], $a[0], src => 'netview' ) if defined $ +a[0]; $w->dataElement( $c[1], $a[1] ) if defined $a[1]; $w->dataElement( $c[2], $a[2], type => 'rw' ) if defined $a[2] +; $w->dataElement( $c[3], $a[3], src => 'data' ) if defined $a[3 +]; $w->dataElement( $c[4], $a[4], src => 'data' ) if defined $a[4 +]; $w->endTag(); } $w->endTag(); $w->end(); $io->close(); }
Which gives me the following errors: Note that I have use strict and use warnings and that the code above starts on line 92:
Variable "@a" is not imported at ./test.pl line 112. Variable "@a" is not imported at ./test.pl line 112. Variable "@a" is not imported at ./test.pl line 113. Variable "@a" is not imported at ./test.pl line 113. Variable "@a" is not imported at ./test.pl line 114. Variable "@a" is not imported at ./test.pl line 114. Variable "@a" is not imported at ./test.pl line 115. Variable "@a" is not imported at ./test.pl line 115. Variable "@a" is not imported at ./test.pl line 116. Variable "@a" is not imported at ./test.pl line 116. Global symbol "@a" requires explicit package name at ./test.pl line 11 +2. Global symbol "@a" requires explicit package name at ./test.pl line 11 +2. Global symbol "@a" requires explicit package name at ./test.pl line 11 +3. Global symbol "@a" requires explicit package name at ./test.pl line 11 +3. Global symbol "@a" requires explicit package name at ./test.pl line 11 +4. Global symbol "@a" requires explicit package name at ./test.pl line 11 +4. Global symbol "@a" requires explicit package name at ./test.pl line 11 +5. Global symbol "@a" requires explicit package name at ./test.pl line 11 +5. Global symbol "@a" requires explicit package name at ./test.pl line 11 +6. Global symbol "@a" requires explicit package name at ./test.pl line 11 +6. Execution of ./test.pl aborted due to compilation errors.

What am I doing wrong? -- Argel


In reply to Confused about typeglobs and references by Argel

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.