http://qs1969.pair.com?node_id=815158

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

I am learning Perl and I inherited old Perl code. I want to know what my predecessor was trying to do in the following line: $isacon = '/apps/home/pfuser/scripts_56/GENERAL/pers_lookup.csh $sids[$index]'; Notice the single quotes and .csh file? Thank you very much

Replies are listed 'Best First'.
Re: What was the programmer trying to accomplish?
by amir_e_a (Hermit) on Jan 01, 2010 at 11:02 UTC

    This is only a guess, but it's quite possible that your predecessor tried to construct a string that will be later used as shell command, that runs the script 'pers_lookup.csh' and passes it as an argument the value of the $sids[$index] variable. Sometimes later in the program there's probably a line that goes something like this:

    system $isacon;

    or maybe:

    $isacon_output = `$isacon`; # (Notice the single back quotes.)

    The way the code is currently written, it will probably not work, because it will literally use the string '$sids[$index]' as the argument. Of course, i don't know anything about your environment, so it's actually possible that it has some meaning to your shell.

    You can try, very carefully, to replace the single quotes with double quotes, so the value of $sids[$index] will be actually used:

    $isacon = "/apps/home/pfuser/scripts_56/GENERAL/pers_lookup.csh $sids[ +$index]"; print "isacon value: [$isacon]\n"; exit;

    Run it, and if the isacon value that appears between the square brackets makes sense as a shell command in your environment, remove the lines with 'print' and 'exit'. Again, be very careful, because it may do unexpected things to your environment.

      I thinks that is it! Thank you. He was making a direct assignment to "$isacon" by executing a unix script. The script got its valuse from "$sidsindex. He later used the value of "$isacon" as such "$isacon ~= tr/\n/|/;"
Re: What was the programmer trying to accomplish?
by bobr (Monk) on Jan 01, 2010 at 09:13 UTC
    Its assignment of exact string as it is in the single quotes into $isacon variable. Look what is done with the variable then -- I would guess that it will be parameter to system or backquotes to execute that as shell command.

    -- Roman

      Although I expect that it should be a double quoted string unless eval EXPR is somehow used later. $sids[$index] looks more Perl-ish than bourne-ish.
Re: What was the programmer trying to accomplish?
by stefbv (Curate) on Jan 01, 2010 at 11:02 UTC

    Are you sure about the single quotes? Could be backticks.

    Happy New Year to everybody!

      Indeed, maybe backticks (``) could be used on this line directly:

      $isacon = `/apps/home/pfuser/scripts_56/GENERAL/pers_lookup.csh $sids[$index]`;

      Backticks, like the double quotes (""), interpolate the string, which means that they translate every variable to its value. Then they run the string as a shell command and return the output.

      Since you are not sure about the programmer's intention, you should still test it first with double quotes and check the result, for example by printing it, and only then you should run it with backticks or system.

      Read more about all the different types of quotes here: Quote-like Operators in perlop.

Re: What was the programmer trying to accomplish?
by Anonymous Monk on Jan 01, 2010 at 08:09 UTC
    assignment, next question :)