Some brief remarks, taking into account that you're asking for comments on programming style.

I'll refrain from suggesting a module, because that's clearly not what you're asking here, but that might actually be the best answer.

Removing all spaces as in styles 3 to 5 is certainly not a good practice in terms of code readability.

Although the use of index rather than a regex may in some cases be faster, this certainly does not matter in a subroutine that is (presumably and hopefully) called only once in your program. I would recommend using regexes in such a case, as this make the code somewhat simpler.

A Perl beginner would most likely use style 2 ("Neat") rather than style 1 ("Beginner").

Using single-character variables such as $S or $Y for storing the uppercase version of the OS is not the best choice for readability. Using $a is an even poorer choice. A variable name such as $os would already improve significantly legibility.

In style 1, you don't need parentheses for the postfix conditional. Removing them would tend to reduce line noise.

As you mentioned yourself, the subroutine may not work properly (as an example, you'll end up with a Windows separator if you're using Cygwin), but, with this limitation in mind, this is a suggested variation on style 1:

sub get_path_separator { for ($^O) { # stores the OS in $_ to simplify the regex syntax return ':' if /mac/i; return '/' if /cyg/i; return '\\' if /win/i; return '.' if /vms/i; return '/'; } }
My other favorite would be style 3, but spread over several lines to make the intent clearer:
$_ = $^O; my $separator = /mac/i ? ':' : /cyg/i ? '/' : /win/i ? '\\' : '/';
Please also note that launching Perl from a bash terminal under Windows, you might get something like "msys" as the value for $^O (I don't know why).

In reply to Re: What's your programming style? by Laurent_R
in thread What's your programming style? by harangzsolt33

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.