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

I've a task to launch terminal or cmd in MAC or PC. Script should launch based on OS.

#To run from MAC open my $ws, '-|', @cmd or die "Error opening pipe: $!"; #To run from Windows open my $ws, "@cmd |" or die "Can't launch [@cmd]: $! / $?";

If there an efficient way to run open based on OS?

Replies are listed 'Best First'.
Re: Launch terminal in MAC and PC
by jellisii2 (Hermit) on Oct 30, 2014 at 16:53 UTC
    Are you looking for a way to determine what env you're working in? If so, there's a few ways, the most basic being checking $^O.

      Yes i want to know the env i'm working. Checked $^O and not useful data was available

      Users-MacBook-Pro:~ User$ perl -e print $^O Users-MacBook-Pro:~ User$

        You will need to learn about your shell and quoting. It's better to run such a program from a file if you are not sure how your command line shell behaves.

        You can try

        perl -wl -e 'print $^O'

        instead. Note the single quotes that tell Perl the program. Also see perlrun for -e.

Re: Launch terminal in MAC and PC
by aitap (Curate) on Oct 30, 2014 at 18:34 UTC
    IPC::Open2 seems to work on Windows even with lists of arguments:
    use IPC::Open2 'open2'; my @cmd = qw(perl -v); open2(my $read, my $write, @cmd) or die "[@cmd]: $!"; print while <$read>;
Re: Launch terminal in MAC and PC
by perlron (Pilgrim) on Oct 30, 2014 at 16:57 UTC
    are you aware of the system command in perl to execute system commands and return control to the srcipt ?
    perldoc -f system
    it should work in both environments, though windows there are a lot of environments and u have to be specificSystem command under Windows 7

    The temporal difficulty with perl is u need to know C well to know the awesome.else u just keep *using* it and writing inefficient code
Re: Launch terminal in MAC and PC
by Anonymous Monk on Oct 30, 2014 at 16:55 UTC

    Put open in a file as part of a complete, correct Perl program?.

    Or, are you asking how to decide on the open syntax based on OS?

      I've a script that calls open to launch terminal or cmd. My issue is to know how to integrate MAC launch terminal and PC launch terminal in a single script.

      How to decide on the open syntax based on OS?