in reply to Mathematics skills

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.

Replies are listed 'Best First'.
Re:x2 Mathematics skills
by grinder (Bishop) on Oct 12, 2002 at 17:30 UTC
    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.