pass a variable to a system call ?

Discussion in 'Perl Misc' started by Jack, Jul 14, 2006.

  1. Jack

    Jack Guest

    Ok this is annoying.. I tried the code below tons of different ways,
    even with double quotes " " with no luck.. how do I pass a variable
    into a system call ??!!

    $filename = 'FileList.txt';
    system("dir /B c:\test_files > c:\test_files\".$filename);

    ... OR ...

    $filename = 'FileList.txt';
    system('dir /B c:\test_files > c:\test_files\'.$filename);

    Any ideas ??

    Thank you,
    Jack
     
    Jack, Jul 14, 2006
    #1
    1. Advertising

  2. Jack wrote:
    > Ok this is annoying.. I tried the code below tons of different ways,
    > even with double quotes " " with no luck.. how do I pass a variable
    > into a system call ??!!
    >
    > $filename = 'FileList.txt';
    > system("dir /B c:\test_files > c:\test_files\".$filename);


    For example exactly that way.
    However, are you certain that the first character of your folder name is a
    TAB character?
    And the escaped closing double quote yields a
    Can't find string terminator '"' anywhere before EOF at t.pl
    when I try it.

    > .. OR ...
    >
    > $filename = 'FileList.txt';
    > system('dir /B c:\test_files > c:\test_files\'.$filename);


    Same error here. Why do you escape the closing single quote?

    > Any ideas ??


    Yep. Use normal forward slashes to avoid the leaning toothpick syndrome.

    jue
     
    Jürgen Exner, Jul 14, 2006
    #2
    1. Advertising

  3. Jack

    DJ Stunks Guest

    Jack wrote:
    > Ok this is annoying.. I tried the code below tons of different ways,
    > even with double quotes " " with no luck.. how do I pass a variable
    > into a system call ??!!
    >
    > $filename = 'FileList.txt';
    > system("dir /B c:\test_files > c:\test_files\".$filename);
    >
    > .. OR ...
    >
    > $filename = 'FileList.txt';
    > system('dir /B c:\test_files > c:\test_files\'.$filename);
    >
    > Any ideas ??


    yes, I have some ideas.

    first idea: have you looked at perldoc -f system and, for good
    measure, perldoc -f exec?

    second idea: do you understand how interpolation works in a double
    quoted string? particularly with regard to backslashes?

    third (and final) idea: did you know Perl isn't a glorified shell
    scripting language or batch file replacement? if you want a directory
    listing as part of another, more complicated script, usually the best
    idea is using a Perl module to grab the information rather than
    shelling out to dir.

    HTH,
    -jp
     
    DJ Stunks, Jul 14, 2006
    #3
  4. Jack wrote:
    > Ok this is annoying.. I tried the code below tons of different ways,
    > even with double quotes " " with no luck.. how do I pass a variable
    > into a system call ??!!
    >
    > $filename = 'FileList.txt';
    > system("dir /B c:\test_files > c:\test_files\".$filename);
    >
    > .. OR ...
    >
    > $filename = 'FileList.txt';
    > system('dir /B c:\test_files > c:\test_files\'.$filename);
    >
    > Any ideas ??


    It looks like you need something like:

    my $filename = 'FileList.txt';
    my $dir = 'c:/test_files';

    open my $fh, '>', "$dir/$filename" or die "Cannot open '$dir/$filename' $!";

    opendir my $dh, $dir or die "Cannot open '$dir' $!";

    print $fh map "$_\n", readdir $dh;

    closedir $dh;
    close $fh;



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jul 14, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Kial
    Replies:
    1
    Views:
    678
    Eric Enright
    Nov 15, 2004
  2. system call and library call

    , Aug 22, 2005, in forum: C Programming
    Replies:
    7
    Views:
    3,032
    Jonathan Bartlett
    Aug 23, 2005
  3. Tricky
    Replies:
    0
    Views:
    591
    Tricky
    Mar 19, 2009
  4. Tricky
    Replies:
    0
    Views:
    1,027
    Tricky
    Mar 19, 2009
  5. Reuven
    Replies:
    0
    Views:
    604
    Reuven
    Mar 19, 2009
Loading...

Share This Page