in reply to replacing literals with constants
If there is one thing I have learned (through hard-earned experience), DON'T ASSUME. If you want to ensure operations occur in the order you intend, use parentheses to force it. It makes it much more likely to get the result you desire. That with the suggestions of tybalt89, Corion, kcott, and LanX I believe will give you the results you want.
Test code:
#!/usr/bin/perl use strict; use warnings; use Test::More; sub ten { 10 } sub ten_() { 10 } my @expected = ( 9, ); { my @result = (); for my $i ( 9 .. 10 - 1 ) { push @result, $i; } is_deeply( \@result, \@expected, q{9 .. 10 - 1 without constant (OP)}, ); } { my @result = (); for my $i ( 9 .. ten() - 1 ) { push @result, $i; } is_deeply( \@result, \@expected, q{9 .. 10 - 1 with constant() (OP)}, ); } { my @result = (); for my $i ( 9 .. ten -1 ) { push @result, $i; } is_deeply( \@result, \@expected, q{9 .. 10 - 1 with constant (OP)}, ); } { my @result = (); for my $i ( 9 .. ( ten() - 1 ) ) { push @result, $i; } is_deeply( \@result, \@expected, q{9 .. 10 - 1 with constant() and parnes (proposed-me)}, ); } { my @result = (); for my $i ( 9 .. ( ten -1 ) ) { push @result, $i; } is_deeply( \@result, \@expected, q{9 .. 10 - 1 with constant and parens (proposed-me)}, ); } { my @result = (); for my $i ( 9 .. ten_() - 1 ) { push @result, $i; } is_deeply( \@result, \@expected, q{9 .. 10 - 1 with constant() (proposed-11143893)}, ); } { my @result = (); for my $i ( 9 .. ten_ - 1 ) { push @result, $i; } is_deeply( \@result, \@expected, q{9 .. 10 - 1 with constant (proposed-11143893)}, ); } { my @result = (); for my $i ( 9 .. ( ten_() - 1 ) ) { push @result, $i; } is_deeply( \@result, \@expected, q{9 .. 10 - 1 with constant() and parens (proposed-11143893/me)}, ); } { my @result = (); for my $i ( 9 .. ( ten_ - 1 ) ) { push @result, $i; } is_deeply( \@result, \@expected, q{9 .. 10 - 1 with constant and parens (proposed-11143893/me)}, ); } my @expected_2 = ( 9, 10, ); { my @result = (); for my $i ( 9 .. ten ) { push @result, $i; } is_deeply( \@result, \@expected_2, q{9 .. 10 with constant (OP)}, ); } { my @result = (); for my $i ( 9 .. ten_ ) { push @result, $i; } is_deeply( \@result, \@expected_2, q{9 .. 10 with constant (proposed-11143893)}, ); } done_testing(); __END__
Test results:
$ perl test.pl ok 1 - 9 .. 10 - 1 without constant (OP) ok 2 - 9 .. 10 - 1 with constant() (OP) not ok 3 - 9 .. 10 - 1 with constant (OP) # Failed test '9 .. 10 - 1 with constant (OP)' # at test.pl line 38. # Structures begin differing at: # $got->[1] = '10' # $expected->[1] = Does not exist ok 4 - 9 .. 10 - 1 with constant() and parnes (proposed-me) not ok 5 - 9 .. 10 - 1 with constant and parens (proposed-me) # Failed test '9 .. 10 - 1 with constant and parens (proposed-me)' # at test.pl line 60. # Structures begin differing at: # $got->[1] = '10' # $expected->[1] = Does not exist ok 6 - 9 .. 10 - 1 with constant() (proposed-11143893) ok 7 - 9 .. 10 - 1 with constant (proposed-11143893) ok 8 - 9 .. 10 - 1 with constant() and parens (proposed-11143893/me) ok 9 - 9 .. 10 - 1 with constant and parens (proposed-11143893/me) ok 10 - 9 .. 10 with constant (OP) ok 11 - 9 .. 10 with constant (proposed-11143893) 1..11 # Looks like you failed 2 tests of 11.
Hope that helps.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: replacing literals with constants
by kcott (Archbishop) on May 14, 2022 at 18:08 UTC |
In Section
Seekers of Perl Wisdom