Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: portability / system / elegance

by ww (Archbishop)
on Dec 04, 2016 at 14:03 UTC ( [id://1177159]=note: print w/replies, xml ) Need Help??


in reply to portability / system / elegance

Minimally more elegant (for some values of 'elegant'): use an alternative to the forward slash as a delimiter to change the path-separator in a code segment that ascertains the OS upon which the program is running (if ($^O eq 'MSWin32') { ... (adapted from below); } elsif ($^O eq 'linux') { ....

D:\_STM_work>perl -E "my $foo='/path/path/somepgm.exe'; if ($^O eq 'MS +Win32') {$foo =~ s?/?\/\/?g;} say $foo;" //path//path//somepgm.exe

Proofreading all the leaning toothpicks is a practice fraught with possibility of error; for elegance, stuff the separator changing into a sub which is called only when the script ascertains that the OS is some (recent) flavor of Windows.

Replies are listed 'Best First'.
Re^2: portability / system / elegance
by atcroft (Abbot) on Dec 04, 2016 at 15:59 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1177159]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-23 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found