Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Newbie question

by kcott (Archbishop)
on Aug 23, 2022 at 10:33 UTC ( [id://11146304]=note: print w/replies, xml ) Need Help??


in reply to Newbie question

G'day oldB51,

Don't write "My code appears to work." and leave us to verify it; show your output. It didn't work for me because:

$ perl -e 'print “\n”;' Unrecognized character \xE2; marked by <-- HERE after print <-- HERE n +ear column 7 at -e line 1.

Using a subroutine is usually a good thing. It abstracts your code so it can be reused within your script. If you find it's particularly useful, you can put it in a module and many scripts can reuse it.

There are many functions whose default argument is $_. Writing abs instead of abs($_) is absolutely fine. Pun intended. :-)

Aim for readability and maintainability. The following might look clever but, for many, it would be difficult to understand; it's not easy to modify; and, it can't be reused.

#!/usr/bin/env perl use strict; use warnings; use List::Util 'sum'; my @input = qw{-222 -221 -21 0 1 1 2 3 5 8 13 21 34 55 89 144}; print join(' ', map +(sum(split //, abs) % 2 ? $_ : ()), @input), "\n" +;

Output:

-221 -21 1 1 3 5 21 34 89 144

Always use the strict and warnings pragmata. You may get one or both of those for free, with different values of VERSION, if you "use VERSION;". Some modules also do this for you.

I would also recommend that you unpack @_ at the start of your subroutines. There are other variables that you should deal with as soon as they become available; for example, $@ containing errors; $1, $2, and so on from regex matches; etc. There are various things that can change their values, perhaps not in the code when first written, but via insidious bugs that might be introduced following modifications. The following would've been better:

sub oddDigitSum { my (@nums) = @_; my @ans; for (@nums) { ...

Of course, all of the above are my opinions. Others may have different opinions. Form you own opinions as your experience grows. :-)

— Ken

Replies are listed 'Best First'.
Re^2: Newbie question
by oldB51 (Sexton) on Aug 23, 2022 at 12:21 UTC

    Thanks for a very helpful reply. I did have

    #!/Users/barry/localperl/bin/perl use 5.036; use warnings FATAL => 'all';

    at the head of my code but forgot to include it in post. I will also include output next time. Sorry, first post but will remember in future.

      "Thanks for a very helpful reply."

      You're very welcome. I'm always happy to help those who are eager to learn.

      "I did have ... forgot to include it ... Sorry, first post but will remember in future."

      That's fine and no need to apologise. My first post, a dozen years ago, was a complete mess: it took me several edits to clean it up.

      When I don't know what version someone is using, I'll usually default to code that works in v5.8. Had I known that you were using the current latest, v5.36, I would've made a few more suggestions.

      A simple one is say(), which has been around since v5.10. That saves you having to tag "\n" onto the end of print statements: not always what you want, but often is.

      Of more recent vintage is "subroutine signatures". That's been flagged as experimental for quite a few versions, but no longer in v5.36. My suggested change for oddDigitSum() could have been written as:

      sub oddDigitSum (@nums) { my @ans; for (@nums) { ...

      When writing code just for myself, I typically use the latest version. If others are involved, I sometimes need to wind that back (or at least advise what newer features I'm using that require that version).

      The online documentation, https://perldoc.perl.org/perl, shows the latest version; it does provide easy access to earlier versions. I'd suggest using that while you're learning; although, I would recommend staying away from experimental features: do read about them; don't use them in code, as many may change or even be removed; wait until they become stable.

      — Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-20 02:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found