in reply to Re: What makes good Perl code?
in thread What makes good Perl code?

#!/usr/bin/perl -w ... use warnings; ... BEGIN { $^W = 1; } sub BEGIN { if ($^W == 1) {say "Let's get started..."} else {die $@} }

Why do you want to turn on warnings 3 times and then die if they're not turned on? use warnings; is sufficient for anything but an ancient version of perl, in which case you might need the -w switch.

my $resistance = <STDIN>; if ($current =~ /mA/) { $current =~ s/mA//; $current =~ s/(^\s+| )//; $current /= 1000; }

That's kind of a mess. First you match on mA and then remove mA, not to mention that it will fail on bad input. Consider an answer like 0 or "more amps than you'll ever know!" It's much better to extract the expected input and complain if you can't find it. The following is a quick, untested example...

my $input = <STDIN>; my ($current) = $input =~ /^\s*[\d.]+\s+/ or do { error_stuff(); ... # next, redo, or return, as appropriate }; $input =~ /\s+mA\s*$/ and $current /= 1000;

Replies are listed 'Best First'.
Re^3: What makes good Perl code?
by Anonymous Monk on Aug 18, 2011 at 10:18 UTC

    Gah, I'm an idiot...

    my ($current) = $input =~ /^\s*[\d.]+\s+/ or do {

    should be

    my ($current) = $input =~ /^\s*([\d.]+)\s+/ or do {

    or there won't be a capture of the number.