in reply to Re: Syntactical changes in Perl 5.8 from 5.6.1
in thread Syntactical changes in Perl 5.8 from 5.6.1

And not everything is documented, the people working on Perl are only human!

I've met a couple of them, they are not what I would characterize as human, perhaps super-human.

Maybe I should elaborate on these scripts. They were conversions from python, and only accept integers and only return integers. They all look similar except for the operator, and are as follows:

if ($#ARGV != 1) { die "Usage: div numerator denominator\n" } foreach (@ARGV) { die "Integers only\n" if (/\D+/); } my $tot = sprintf "%lf\n", ($ARGV[0]/$ARGV[1]); $tot =~ s/(\d+)(\.\d+)/$1/; print $tot;

Perhaps someone can suggest a better way. These were very quicky hammered together to fix a problem with some scripts that rely on them after an OS upgarde.

Replies are listed 'Best First'.
Re: Syntactical changes in Perl 5.8 from 5.6.1
by Abigail-II (Bishop) on Oct 11, 2003 at 00:38 UTC
    The last three lines can be written as:
    print int ($ARGV [0] / $ARGV [1]);

    Abigail

      Hi Thanks for your reply.... The reason that I took that approach is that I have several scripts; the div one that you saw in the example but there is also a multiplication script. Since the result could not be expressed in scientific notation I chose to use "%lf" and the use regex to clean it up. This code was used to create the others by just changing the operators. Perhaps not the best method. The multiplication script follows:
      #!/usr/bin/perl -w use strict; if ($#ARGV != 1) { die "Usage: mult integer integer\n" } foreach (@ARGV) { die "Integers only\n" if (/\D+/); } my $tot = sprintf "%lf\n", $ARGV[0] * $ARGV[1]; $tot =~ s/(\d+)(\.0+)/$1/; print $tot;

      If you have any suggestions I'd love to hear them.

      Thanks!

        I've no idea what you are trying to do here. Since $ARGV [0] and $ARGV 1 are both integers, its product will be an integer too. Hence, their product will not have digits after the decimal point. Using sprintf and "%f" is a trick to avoid "scientific" output, when the result gets bigger than maxint, but you must realize the value will be inaccurate. However, there's no need for the substitution, the last three lines can be written as:
        printf "%.0f\n" => $ARGV [0] * $ARGV [1];

        Abigail