cored has asked for the wisdom of the Perl Monks concerning the following question:

Hi, guys is me again, i want that you check my logic on this script and i want to know if i need to use the pack/unpack functions to make a binary/decimal converter The next script makes any multiplication table...
#!/usr/bin/perl -w use strict; my $vlnum = 0; print "Introduzca un numero: "; chomp ( $vlnum = <STDIN> ); for(1..10){ print "$vlnum * $_ =", $vlnum * $_; }
what do you think... and give me some help plz i want to learn...

Replies are listed 'Best First'.
Re: Mathematics skills
by Zaxo (Archbishop) on Oct 11, 2002 at 22:36 UTC

    Your logic is fine. You may want to also print a newline or some spaces, just for readibility.

    The pack and unpack functions are very handy for that, and are probably the best choice. Also oct can produce a number from a binary string representation, and vec can be used to set and unset bits.

    After Compline,
    Zaxo

Re: Mathematics skills
by Aristotle (Chancellor) on Oct 12, 2002 at 14:22 UTC
    Looks good, and Zaxo covered the rest of the points. I just want to point out the my $vlnum = 0; is sort of ugly there: you never use that zero. If I do not explicitly want a specific initial value for a variable, I purposely leave it undefined so that I get a warning if I use it before I was planning to. In this case you can roll the initialization and input together: chomp (my $vlnum = <STDIN>);

    my returns a valid Lvalue, that is, you can write my $var just about anywhere you can write $var.

    Beware though: my ($vlnum) = <STDIN> (note the parens) is not what you want here. It gives you list context, in which the <FH> operator slurps the entire input first, then you get the first line assigned to $vlnum, and the rest is thrown away.

    Makeshifts last the longest.

      If I do not explicitly want a specific initial value for a variable, I purposely leave it undefined so that I get a warning if I use it before I was planning to.

      If I do not want an explicit initial value for a variable, I tend to set it explicitly to undef. It tends to pair up well with a latter test for definedness, and it also shows that I have considered that the variable containing an undefined value is actually, er, defined behaviour. Consider the snippet:

      my $key = undef; for my $k( keys %h ) { $key = $k if $h{$k} eq 'foo'; } if( defined $key ) { # ... }

      I find it makes it clearer to see that the variable starts out undefined and that the loop could set the variable to some value.


      print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
        I'm not sure that makes sense. Someone fluent in Perl is supposed to know that my $var; leaves $var undefined. I wouldn't consider the following any less readable than your code - or maybe, vice versa, don't consider your code any more readable than the following:
        my $key; for my $k( keys %h ) { $key = $k if $h{$k} eq 'foo'; } if( defined $key ) { # ... }
        It's the same as using $_ - you can of course go and write
        while(defined($_ = <STDIN>)) { next unless $_ =~ /^something/; # ... }
        but in my opinion, that is, if anything, less rather than more readable. It's more accomodating to a Perl beginner maybe, who would be a bit lost as to what's happening if the idiomatic forms were used, but it's a whole lot more laborous to read too. But that may just be my personal preference.

        Makeshifts last the longest.