in reply to portability / system / elegance

Hi skendric,

In my experience, the best thing is to use modules to help you. A quick glance at the source of File::Which shows that it uses File::Spec internally, so the filenames that which is returning should already be in the native format. You can test this via use Data::Dumper; $Data::Dumper::Useqq=1; print Dumper($tshark_binary); (although keep in mind that the output will be in Perl's notation, so e.g. $VAR1 = "C:\\Foo\\Bar.txt" means that the string is actually C:\Foo\Bar.txt).

As for calling external commands, I would not recommend the system(STRING) form, but instead the system(LIST) form (Update: that is, system called with more than one argument, or even better, the system PROGRAM LIST form), or even better, a module such as IPC::Run3, which allows you to dodge most shell quoting and escaping issues (on Linux, it can avoid the shell completely, and on Windows, it automatically uses Win32::ShellQuote).

Unfortunately I can't test this on Windows at the moment, but I think this should work (it works on Linux): Updated: Tested and works on both Linux and Windows. On Windows, which('tshark') does indeed return "C:\\Program Files\\Wireshark\\tshark.EXE" as I suspected above. Also, as I mentioned below, if tshark is already in your PATH, you don't need the which, it works fine without (run3 ['tshark', ...) on both OSes.

use File::Which qw/which/; use IPC::Run3 qw/run3/; run3 [which('tshark'), '-r', $pcap, qw/ -e frame.number -e frame.time_epoch -e ip.src -e -T text -T fields -E /,'separator=,'], undef, $temp_file;

Although, if tshark is already in your $ENV{PATH}, why use which at all?

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^2: portability / system / elegance (updated)
by Marshall (Canon) on Dec 05, 2016 at 00:13 UTC
    I like this:

    Although, if tshark is already in your $ENV{PATH}, why use which at all?

    Just do a tshark command.

    If it fails then tshark was not in a path to an executable and you get "Command not found".