Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: looking for feedback on my first script

by haukex (Bishop)
on Jan 08, 2021 at 16:10 UTC ( #11126611=note: print w/replies, xml ) Need Help??


in reply to looking for feedback on my first script

My overall first impression is that this is pretty good code - subdivided into subroutines, nicely formatted, etc. It could be compacted / made mode "Perlish" in a couple places, but because TIMTOWTDI and the code is still clean and pretty easy to follow, I think it's fine the way it is. Just a few other issues / comments:

  • Most importantly, you're using the system function with a single argument and with user input (command-line arguments), which is vulnerable to injections, and you're not checking the calls for errors. See Calling External Commands More Safely for alternatives.
  • opendir also needs to be checked for errors, and it's better to use lexical file/dir handles, as in opendir(my $dh, $path) or die "$path: $!";.
  • Instead of your sub pad, you can use sprintf, as in sprintf("%02d", $num).
  • I would suggest you take the time to study Time::Piece and/or DateTime instead of the basic localtime stuff you're doing here, I think you'll be much happier in the long run.
  • You may also want to consider looking into Getopt::Long to pass arguments to the script instead of a plain list of arguments. IMHO it makes for much more readable script invocations.
  • Unless you're certain this will only ever be run on *NIX, you probably want to consider using a module for your filename manipulations. Personally I like Path::Class.
  • Now we're getting very nitpicky, but do you really need to provide two different date formats? For things like this I'd always stick to YYYY-MM-DD.

Replies are listed 'Best First'.
Re^2: looking for feedback on my first script
by hippo (Chancellor) on Jan 08, 2021 at 16:20 UTC
    I would suggest you take the time to study Time::Piece and/or DateTime instead of the basic localtime stuff you're doing here

    Completely agree. I'd also point out that of the two Time::Piece is always my first port of call as it is small and in core and is good enough for 95% of the date/time work I need to do. By contrast, DateTime is a bit of a monster but it does cater pretty well for the remaining 5% of cases.


    🦛

Re^2: looking for feedback on my first script
by thirtySeven (Acolyte) on Jan 08, 2021 at 16:25 UTC
    thanks for the feedback. I will be taking a lot of this advice. It will take me a few hours to work through everything you have said. I did not consider that it was unsafe to use system with user inputs. I am off to look into the various modules you suggested.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2021-07-28 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?