usrlocal has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Im trying to modify a script that uses strict "refs". I assigning a number" to a particular variable thats being used elsewhere. when I try to run the modified script, I get Can't use string ("0") as a SCALAR ref while "strict refs" in use at ./xxxx.mod line 325 I was able to bypass it by issuing "no strict refs" right before the point where it is being used, but would like to not make such a change that could potentially impact other things. alternately id like to conform to the strict "refs" and convert the string to a SCALAR as required by the script. any idea how I can accomplish this?

Original content restored above by GrandFather. Updated content follows

I tried out kyle suggestion of putting a '\' before the $processor but got the error message
Processor is SCALAR(0x15f1a8) and qmgr is QM_P266 In bind_cpu_solaris , Processor is SCALAR(0x15f1a8) In for loop ... In Next Loop ... /usr/sbin/pbind: must specify at least one pid usage: /usr/sbin/pbind -b processor_id pid[/lwpids] ...
The processor variable is populated in one other location where it looks for pid's of other processes already bound to a specific processor and tries to bind to the same (i.e populate $processor with the same proocessor id). the code is like the below:
sub get_cpu_used_by_other_processes { my $other_process = shift; my $proc_id = locate_process_id( $other_proc ); my $processor = undef; my $pid = undef; my @cmd_output = `$os_command_table{$^O}{pbind} -q 2>/dev/null +`; die "ERROR:failed to retrieve the bound processors\n" unless ( + $? == 0 ); ##### increase the weight of processors based on used or not # +#### foreach ( keys %{$proc_id} ) { $pid = $$proc_id{$_}; next unless defined $pid; foreach ( @cmd_output ) { $processor = $1, return \$processor if /proces +s\s+id\s+${pid}\s*:\s*(\d+) $/; } } }

Replies are listed 'Best First'.
Re: converting from string to SCALAR when using strict "refs"
by ikegami (Patriarch) on Jan 20, 2009 at 22:12 UTC

    ( The original content of the OP talked about getting around Can't use string ("0") as a SCALAR ref while "strict refs" by turning off strict. )

    Unless you really are trying to access the variable $0, use strict 'refs'; caught a real error. Don't ignore it.

    Sounds like you did

    $ref = @array; or $hoa{foo} = @array; or $aoa[$i] = @array;

    earlier in your program where you should have done

    $ref = \@array; or $hoa{foo} = \@array; or $aoa[$i] = \@array;

    or

    $ref = [ @array ]; or $hoa{foo} = [ @array ]; or $aoa[$i] = [ @array ];
Re: converting from string to SCALAR when using strict "refs"
by bart (Canon) on Jan 20, 2009 at 20:51 UTC
    but would like to not make such a change that could potentially impact other things.
    Put the no strict 'refs'; and the dangerous line in a bare block of their own. The scope of strict is lexically limited to that block.

    p.s. obligatory link to varvarname

Re: converting from string to SCALAR when using strict "refs"
by kennethk (Abbot) on Jan 20, 2009 at 20:53 UTC
    Posting code, particularly line 325, would be helpful. What is happening is your syntax is telling the interpreter to think the string is supposed to contain a symbolic reference (see perlref). One possible way you may have done this is the code ${$string}. If the content of the string is "0" at that point, the symbolic reference is interpreted as a reference to the variable $0, which I strongly suspect you did not intend. I would likely be a very good idea to leave use strict; in effect and fix what is likely a typo in line 325.
Re: converting from string to SCALAR when using strict "refs"
by Corion (Patriarch) on Jan 20, 2009 at 20:54 UTC

    Are you really, really sure that the code in question is using the variable $0? Otherwise, I would assume a plain programming error, where a string was stored in a variable that is then used as a reference, something that strict 'refs' catches, for a reason.

Re: converting from string to SCALAR when using strict "refs"
by kyle (Abbot) on Jan 20, 2009 at 20:58 UTC

    I suspect that you won't be happy to get what you're asking for. If you really do use the string "0" as a scalar ref, you'll be accessing the variable, $0, and I'm not sure why you'd want to do that via reference. So I'm calling XY Problem.

    Can you show us more of the code that's having the problem? It would help if you can come up with a minimum snippet that shows the problem.

    If you want to turn off strictures for only a small place you can do that in a scope like so.

    { no strict 'refs'; print ${$zero}; }
Re: converting from string to SCALAR when using strict "refs"
by gwadej (Chaplain) on Jan 20, 2009 at 20:55 UTC

    Without the code it's difficult to be sure. But, it sounds like you are assigning a number to a reference to a scalar instead of to the scalar that's referred to. When the scalar is later dereferenced, a problem occurs.

    Using no strict refs is very rarely the right solution to any problem. The few times it makes sense, you would know enough not to be asking the question.

    Show the code and the monks can probably do a better job of helping.

    G. Wade
Re: converting from string to SCALAR when using strict "refs"
by zentara (Cardinal) on Jan 20, 2009 at 20:57 UTC
    Can you give a small example of what the code looks like. You just need to setup a hash properly, to avoid the errors. You don't want to have "variable scalar variables". For instance, see bad variable naming. The exact same functionality can be achieved without warnings by putting things in a hash, but we need to see some of your troublesome code.

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: converting from string to SCALAR when using strict "refs"
by kyle (Abbot) on Jan 21, 2009 at 16:41 UTC

    Here's the code from your update with <code> tags added.

    $processor = `/aa/bin/get_pbind_cpus.ksh`; # Line 325 system("$os_cmd_table{$^O}{pbind} -b $$processor $$proc_id{$_} >/dev/n +ull 2>&1") if defined $$processor;

    From this, I think that $processor is being misused as a reference. Line 325 should be:

    system("$os_cmd_table{$^O}{pbind} -b $processor $$proc_id{$_} >/dev/nu +ll 2>&1") if defined $processor;

    Try it and let us know how it works.

      Line 325 cant really be modified as it is used many other methods too... can the $processor = `/aa/bin/get_pbind_cpus.ksh`; be modified to populate the processor number correctly?

        If it needs to be a reference to a scalar (as it appears it does), then you want this:

        my $processor = \`/aa/bin/get_pbind_cpus.ksh`;

        That's kind of odd, however. I'd look at the other usages to see if they really are passing in a scalar reference. I'm not saying it's impossible, but I find it strange since there isn't really any advantage to passing in a reference rather than the variable itself.

        I don't have ksh or the commands to test, but it might be a simple as predeclaring the hash %proc_id, but you may need to go a bit deeper depending on how $proc_id{} is setup prior to the command. Just a WAG,
        # borrowing from [kyle]'s translation, # at program beginning my %proc_id; system("$os_cmd_table{$^O}{pbind} -b $processor $$proc_id{$_} >/dev/nu +ll 2>&1") if defined $processor;

        I'm not really a human, but I play one on earth Remember How Lucky You Are