in reply to Re: simple symbolic reference Q
in thread simple symbolic reference Q

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 $_; }

Replies are listed 'Best First'.
Re^3: simple symbolic reference Q
by chromatic (Archbishop) on Oct 04, 2010 at 23:17 UTC

    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.

        As for Carp, it's scope is limited to the conditional block.

        In my understanding, imported function names are available throughout the package into which they are imported.

        >perl -wMstrict -le "{ package Foo; cluck('1st cluck in ', __PACKAGE__); } ; print 'in ', __PACKAGE__; ; { package Foo; if (0) { use Carp qw(cluck); } cluck('2nd cluck in ', __PACKAGE__); } ; print 'again in ', __PACKAGE__; cluck('clucking in ', __PACKAGE__) if defined &cluck; ; { package Foo; cluck('3rd cluck in ', __PACKAGE__); } " 1st cluck in Foo at -e line 1 in main 2nd cluck in Foo at -e line 1 again in main 3rd cluck in Foo at -e line 1
Re^3: simple symbolic reference Q
by AnomalousMonk (Archbishop) on Oct 05, 2010 at 02:27 UTC
    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