Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Perl's "system ('some command')" behave differently from c:\> some command?

Tags:

cmd

perl

spawn

In my (cmd.exe's) current working directory is a file named libre-office-document.odt. I can open this document using the start command without problem:

c:\path\to\> start libre-office-document.odt

Yet, if I use the following simple Perl script

use warnings;
use strict;

print system("start libre-office-document.odt")

and execute it in the same directory, I get the following message box:

LibreOffice 4.4 - Fatal Error

The application cannot be started.
User installation could not be completed.

Obviously, something behaves differently when using Perl's system command. I am unable to determine what that is and how I can open the document with the system command.

like image 695
René Nyffenegger Avatar asked Nov 08 '22 12:11

René Nyffenegger


1 Answers

Use backticks to capture output. You can check file associations with assoc and ftype, than hardcode it, or try this:

use strict;
use warnings;

my $assoc = `assoc .odt`;

# .odt=LibreOffice.WriterDocument.1
chomp $assoc;

$assoc =~ s/.*=//g;

my $ftype = `ftype $assoc`;

# LibreOffice.WriterDocument.1="C:\Program Files\LibreOffice\program\soffice.exe" -o "%1"
chomp $ftype;

$ftype =~ /.*=("[^"]*")/;
$ftype = $1;

# "C:\Program Files\LibreOffice\program\soffice.exe" libre-office-document.odt    
my $cmd = "$ftype libre-office-document.odt";

system($cmd);
like image 148
k-mx Avatar answered Nov 15 '22 06:11

k-mx