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

Hi, I had originally posted this question yesterday, but due to my lack of understanding of how posting on this board works, the question posting got garbled up. Hence reposting with my understanding of the problem so far. Being a novice at perl, I have been tasked with modifying a perl script that binds specific processes to a specific processor. It does this either by executing a script that returns the next available processor or finding similar processes and then binding to the processor used by those processes. The change I am implementing is the "binding to processor number returned by the script" part. I am trying to accomplish this using the following piece of code:
$processor = `/aa/bin/get_bind_cpus.ksh`
However owing to the fact that strict refs is enforced throughout the perl script, I get the error: Error Converting from string to SCALAR in line 325 . that line runs as below:
my $mgr = shift; my $processor = shift; my $proc_id = locate_process_id( $mgr ); foreach ( keys %{$proc_id} ) { next unless defined $$proc_id{$_}; system("$os_command_table{$^O}{pbind} -b $$processor $ +$proc_id{$_} >/dev/null 2>&1") if defined $$processor; } } }
This processor variable is also populated at anbother location (where the script tries to find similar processes using the same processor and bind to that) as illustrated by the code sample below:
sub get_cpu_used_by_other_proc { my $other_mgr = shift; my $proc_id = locate_process_id( $other_mgr ); 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+)$/; } } }
My simple question is how should the line of code I am trying to introduce:
$processor = `/aa/bin/get_bind_cpus.ksh`
be modified so that it populates the processor scalar correctly so that it dosent bomb onthe call to pbind:
system("$os_command_table{$^O}{pbind} -b $$processor $ $proc_id{$_} >/dev/null 2>&1")
Help is appreciated

Replies are listed 'Best First'.
Re: Converting from string to SCALAR when using strict "refs"
by kyle (Abbot) on Jan 21, 2009 at 21:21 UTC

    Here's what you have:

    $processor = `/aa/bin/get_bind_cpus.ksh`;

    This will leave $processor with a value like "1\n" (that's the numeral 'one', and a newline). What you want is to have a reference to a number '1'.

    So I say cast $processor as a number with 0+ to get rid of the newline, and then make it a reference.

    $processor = 0+ `/aa/bin/get_bind_cpus.ksh`; $processor = \$processor;

    You can do this all in one step like so:

    $processor = \( 0+ `/aa/bin/get_bind_cpus.ksh` );

    Note that this means if `/aa/bin/get_bind_cpus.ksh` outputs some nonsense, that will be interpreted as zero. If you want to be prepared for that, you'll have to validate that somewhere.

Re: Converting from string to SCALAR when using strict "refs"
by kennethk (Abbot) on Jan 21, 2009 at 21:26 UTC
    For interested monks, the original thread is 737662. It looks like you are being inconsistent in your definition of $processor. Your line $processor = `/aa/bin/get_bind_cpus.ksh` assigns a real value to $processor as does $processor = $1, whereas you attempt to read it as a reference in system("$os_command_table{$^O}{pbind} -b $$processor $$proc_id{$_} >/dev/null 2>&1"). I note that your subroutine get_cpu_used_by_other_proc actually returns \$processor, which is a pointer to the memory address, so if the final variable called is actually populated using this return value, your only inconsistency is $processor = `/aa/bin/get_bind_cpus.ksh`, which should be able to be fixed as per kyle's suggestion in 737880.
      Thanks Monks , that worked.