in reply to Please critique this script -- Read emails and write URLs into bookmarks

"This is my first Perl script."

++ This is an excellent, first effort!

"I'm looking for any kind of feedback, especially pitfalls and 'better' idioms etc."

Here's some observations; along with various suggestions and recommendations:

There may be other issues, but these were the ones that stood out for me.

Having pointed out all those issues, I just wanted to reiterate that this is a well-written script and an excellent first attempt.

See also: the online perl manpage (which I have bookmarked and refer to often); "Create A New User".

Update: Fixed typo: s/obversations/observations/

— Ken

Replies are listed 'Best First'.
Re^2: Please critique this script -- Read emails and write URLs into bookmarks
by afoken (Chancellor) on Oct 13, 2016 at 06:29 UTC
    I'd personally avoid hard-coding a path to perl in the shebang line. [...] This allows you to change the version of Perl in the current environment and run your script under this new version. In particular, it's useful when testing how your script runs under different versions of Perl

    The shebang line with a fixed interpreter does not prevent you from trying other interpreters:

    ./foobar.pl # <- perl from shebang perl foobar.pl # <- whatever perl is found first in $ENV{'PATH'} /home/me/bin/perl-special foobar.pl # <- a special perl /usr/local/bin/perl foobar.pl # <- common location for a local perl /usr/bin/perl foobar.pl # <- common location for system perl

    perlrun has an entire chapter "Location of Perl" with recommendations:

    • When possible, it's good for both /usr/bin/perl and /usr/local/bin/perl to be symlinks to the actual binary. If that can't be done, system administrators are strongly encouraged to put (symlinks to) perl and its accompanying utilities into a directory typically found along a user's PATH, or in some other obvious and convenient place.
    • You are advised to use a specific path if you care about a specific version.

    Perl has some surprises when it comes to the shebang line, see perlrun:

    • Parsing of the #! switches starts wherever "perl" is mentioned in the line. The sequences "-*" and "-" are specifically ignored
    • If the #! line does not contain the word "perl" nor the word "indir", the program named after the #! is executed instead of the Perl interpreter. This is slightly bizarre, but it helps people on machines that don't do #!

    Also, while most systems have /usr/bin/env, not all systems have it. Some have the env utility in /bin/env. Some systems use a symlink in one directory pointing to env in the other directory. See also http://www.in-ulm.de/~mascheck/various/shebang/#env.

    env is another process that has to be started. env does not just start perl; at least, it has to parse the command line for arguments. See busybox env.c for a quite minimal implementation. Entry point is env_main(). The GNU version does much more, see GNU env.c. So using env in the #! line increases the startup time. Generally, it does not matter that much, especially on modern machines. But the overhead is not zero.

    I typically start my scripts like this:
    #!/usr/bin/env perl use 5.014; use strict; use warnings;

    use strict is redundant here, as use VERSION with a VERSION >= 5.12.0 implies use strict. See use.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      ++ Thanks for the feedback.

      I'm aware of the shebang line points you raise. Hard-coding paths to perl has, in my (>20 years) experience, many more problems than using env. Of course, there are occasions where a hard-coded path might be more appropriate.

      You quoted me as saying:

      "I typically start my scripts like this: ..."

      A more accurate quote, that conveyed the point I was making, and didn't hide the text you elided, would have been:

      "Whenever I use the '/r' modifier, I typically start my scripts like this: ..."

      If I removed the '/r' modifier, but still had code with say, //=, etc., I only need to change a '4' to a '0' (i.e. 5.0145.010); I do not need to remember to add use strict.

      — Ken

Re^2: Please critique this script -- Read emails and write URLs into bookmarks
by Anonymous Monk on Oct 12, 2016 at 08:56 UTC

    This was a long one to parse, thank you for that :)

    I've done almost all you've recommended, especially the trivial ones.

    Nice performance considerations as well.