store a command to a variable.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fetaelin
    New Member
    • May 2007
    • 16

    store a command to a variable.

    Hi
    I need to store a command or a command combination to an Perl Variable, and then execute it in perl,
    how to do that ?
    I tried to put ls /etc/ tp $com and then store the result to $h but it did not wok.
    the most importent part for me is the first row "$com=`ls /etc/`;" but it seams to be the wrong way to do it. please help

    $com=`ls /etc/`;
    $h=`$com`;
    print $h;
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    don't use backtiks when assigning a value to $com use single-quotes:



    Code:
    $com = 'ls /etc/';
    $h = `$com`;
    print $h;

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Originally posted by fetaelin
      $com=`ls /etc/`;
      Somebody please correct me if I am wrong, but doesn't the line above actually stores the result of the "ls /etc/" into the variable $com? There shouldn't be a need to assign the $com variable to another variable unless you really want to use the other variable for some reason.

      Regards,

      jlk

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Hi numberwhun,

        You are correct but it is also common to store the command in a variable to be used later in the script.

        Welcome to TSDN,
        Kevin (aka KevinR - perlguru forum)

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Kevin,

          Originally posted by KevinADC
          Hi numberwhun,

          You are correct but it is also common to store the command in a variable to be used later in the script.

          Welcome to TSDN,
          Kevin (aka KevinR - perlguru forum)
          Thanks! and Thanks for the Welcome! I was wondering if the two posters were one in the same person. :-)

          Regards,

          Jeff

          Comment

          Working...