Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: What's your programming style?

by eyepopslikeamosquito (Chancellor)
on Dec 08, 2018 at 08:40 UTC ( #1226948=note: print w/replies, xml ) Need Help??


in reply to What's your programming style?

Which programming style do you like? Which one should I use?
Athanasius has already provided a much nicer alternative.

I'll briefly nitpick your style of using the || operator (as in index($Y,'WIN')<0||return'\\';). For flow of control, prefer the low precedence or operator. A common example of preferring or to || is:

open(my $fh, '<', $file) or die "error opening '$file': $!";
That is probably the principal use case as to why the low precedence operators were added to the language in the first place. Reserve && and || for logical expressions, not flow of control, for example:
if ($x > 5 && $y < 10) ...

This is discussed in detail in Perl Best Practices, Chapter 4, "Values and Expressions" in the "Don't mix high- and low-precedence booleans" item.

Re OS path portability, the core Perl module that deals with this is File::Spec and that is perhaps what you should be using (hard to know without understanding your bigger picture). Note that if you only need to target *nix and Windows (update: and modern Macs running BSD-derived MacOS), you can avoid File::Spec simply by using / as the path separator; this works because the Windows API treats forward slashes and back slashes equivalently in file path arguments to all Windows API functions. A minor annoyance is that Windows commands typically use forward slash for command line argument options. So, while using forward slashes in file paths is fine when calling all Perl internal functions on Windows, using them in command line arguments when running Windows commands is problematic - in the rare cases where you need to pass a file path to an external Windows command, you can translate it like so: $filepath =~ tr{/}{\\}

More useful than nitpicking low level details of programming style is to look at the bigger picture; some references on that follow:

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2020-05-29 11:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (169 votes). Check out past polls.

    Notices?