Would not the more "elegant" solution be to use a module that is designed for this purpose, File::Spec? From the documentation for the module, File::Spec requires an appropriate module (File::Spec::Cygwin, File::Spec::Unix, and File::Spec::Win32, among others), which alters such functions as catfile in an appropriate manner. Taking from ww's example (and setting the path value in a sub) using this code:

perl -MFile::Spec -le 'sub tshark_path { my @filename = qw( / usr bin +tshark ); if ( $^O eq q{MSWin32} ) { @filename = ( q{c:}, q{Program F +iles}, q{Wireshark}, q{tshark.exe} ); } return @filename; } my @file += tshark_path(); print File::Spec->catfile( @file );'

gives the following results when executed on Cygwin (under Win10), Win10, and linux:

# Result under Cygwin /usr/bin/tshark # Result under Win10 (Powershell) C:\Program Files\Wireshark\tshark.exe # Result from linux /usr/bin/tshark

Hope that helps.


In reply to Re^2: portability / system / elegance by atcroft
in thread portability / system / elegance by skendric

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.