Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

New Perl Question

  • 19-01-2009 05:46PM
    #1
    Registered Users, Registered Users 2 Posts: 465 ✭✭


    Hi

    I am trying to open a command prompt inside my perl script and run a command. It is not recognising the two variables as i think the syntax i am using is wrong. Can anybody shed any light on this? Thanks.


    $printLocn = "\\\\server\\printername" ;

    my $cmd = 'perl -pi -e -d copy $file $printLocn'; (think this is wrong?)
    runCMDCommand();

    sub runCMDCommand {

    my @output = `$cmd`;
    print("CMD:$cmd\n");
    }


Comments

  • Registered Users, Registered Users 2 Posts: 26,449 ✭✭✭✭Creamy Goodness


    You need to use "" double quotes so that the variables on line 3 get substituted the actual variable value.

    Using '' single quotes causes perl to see $var as just that $var

    Also your sub routine does not know of the variable $cmd as it is not passed to the routine although perl may let you away with this if you omit the use strict pragma


  • Registered Users, Registered Users 2 Posts: 817 ✭✭✭Burial


    You need to use " instead of ' when using variables. Your program just reads it as what its written as.

    So lets say, $Variable = 5;

    1.) print("$Variable");

    2.) print('$Variable');

    Output:

    1.) 5

    2.) $Variable

    I hope that helps.


  • Registered Users, Registered Users 2 Posts: 465 ✭✭coco06


    Hey

    Once i looked at it with a clear head again this morning i realized i can just use the copy function in the script itself and dont need to open command prompt window...

    Thanks


  • Registered Users, Registered Users 2 Posts: 6,653 ✭✭✭daymobrew


    coco06 wrote: »
    Once i looked at it with a clear head again this morning i realized i can just use the copy function in the script itself and dont need to open command prompt window...
    Do you mean use the File::Copy module?

    I always encourage the use of native functions over command prompt and other external tools. Native functions will be more portable.


  • Registered Users, Registered Users 2 Posts: 465 ✭✭coco06


    daymobrew wrote: »
    Do you mean use the File::Copy module?

    I always encourage the use of native functions over command prompt and other external tools. Native functions will be more portable.

    Yes the file::copy module. Just took me a little time to get the brain working..


  • Advertisement
Advertisement