in reply to simple symbolic reference Q

You can't (easily) use symbolic references with lexical variables. You must use package variables instead.

With that said, don't use symbolic references for this: use a hash or real references.

(As well, $#whatever doesn't evaluate to the number of elements of @whatever in scalar context; it evaluates to the index of the final element of @whatever. Use my $ar_count = @whatever; instead.)

Replies are listed 'Best First'.
Re^2: simple symbolic reference Q
by perl-diddler (Chaplain) on Oct 04, 2010 at 23:04 UTC
    Ok, so this works, that's how I got confused. My previous usages had been with package vars. So this works for a limited case of the vars being package vars, but I don't see how you would do the same with has or real references, without having to pass both the string name of the array and the really reference to the array. The hash example -- not sure what you are referring to, specifically.
    #!/usr/bin/perl -w use strict; my ($Devel, $Debuff_Output) = (1,1); if ($Debuff_Output) { select STDERR; $| = 1; select STDOUT; $| = 1; } if ($Devel) { use Carp qw(cluck confess); $SIG{__WARN__} = $SIG{__DIE__} = sub {confess @_} } our @array = (1, 2, 3, 4); our @array2 = (3, 5, 7); sub arlen ($) { my $ar_nam=$_[0]; no strict 'refs'; my $ar_len = @$ar_nam; sprintf "name=%s, length=%d", $ar_nam, $ar_len; } foreach ('array', 'array2') { no strict 'refs'; printf "Array %s\n", arlen $_; }

      Why do you care about the name of the array? (If you really, really do, use Smart::Comments which works just fine with lexicals.)

      (Why are you messing with the buffering of STDERR? Did you know that your code always loads Carp?)

        I mess with buffering of both STDERR and STDOUT. I ensure that they are not buffered when Developing or Debugging code. I believe it is the case that STDERR may not be buffered in perl5, but I'd have to verify that to be sure. It's old template code.

        As for Carp, it's scope is limited to the conditional block. The fact that it is usually loaded is not really that important, as I don't know of any code that includes this template header, that I've removed the 'Devel=1' status on. :-)

        I'm aware that the if around the 'use' doesn't provide conditional compilation, if that was your point.

      The hash example -- not sure what you are referring to, specifically.

      chromatic probably meant something like:

      c:\@Work\Perl\monks\AnomalousMonk>perl -wMstrict -le "my %hash = ( foo => [1, 2, 3, 4], bar => [3, 5, 7], ); ; elements(\%hash, qw(bar foo)); ; sub elements { my $hashref = shift; for my $name (@_) { print qq{elements of $name: }, scalar @{ $hashref->{$name} }; } } " elements of bar: 3 elements of foo: 4