This is your problem line:
@PSarray = $PScmd ;
By that point,
$PScmd already contains the result of the command. You are assigning
all the lines to
$PSarray[0].
There are two ways to fix your code, that I can see:
- Split the result into lines: @PSarray=split(/\n/,$PScmd);
- Or do the substitution on the whole string, but change how the RE engine interprets it: s/^\s+//m. (The m modifier tells the regexp engine to make ^ and $ match start and end of line instead of start and end of string).