Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: replacing literals with constants

by atcroft (Abbot)
on May 14, 2022 at 16:10 UTC ( #11143898=note: print w/replies, xml ) Need Help??


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

    G'day atcroft,

    "DON'T ASSUME"

    ++ I could not agree with this more.

    Decades of experience have told me that "Things will never change" is the catch cry of the damned. It doesn't matter how much you attempt to advise management otherwise, you find them unmoved.

    Here's a real world example. I started a new job and recommened moving from font to CSS; heard the "Things will never change"; and was told to stop rocking the boat and suggesting changes they couldn't afford. A few weeks later, their major sponsor changed their corporate colours. After a decision to slightly change their corporate colour, the 70,000 code changes required, versus my singular one CSS change, was a better solution.

    — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11143898]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2022-10-04 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (17 votes). Check out past polls.

    Notices?