This one only took four hours to get to the first working version. The reason it took me so long is that I wanted to try to do it without doing the same conditional test more than once (also, I'm not good at this). It isn't very obfuscaturated:

#!/usr/bin/perl -w use strict; for my $num (1 .. 100) { my $mod3num = $num % 3; my $mod5num = $num % 5; my $result = ""; if ( $mod3num == 0 ) { $result .= "fizz"; } if ( $mod5num == 0 ) { $result .= "buzz"; } if ( $result =~ /z/ ) { print "[$num] $result\n"; } else { print "[$num] $num\n"; } }

Here's a different one I spent some of that four hours trying to do, then completed in 15 minutes the next day. It seems pretty annoying but I think it could be better:

#!/usr/bin/perl -w use strict; for my $n (1 .. 100) { my $nm3 = $n % 3; my $nm5 = $n % 5; print((!$nm3 && !$nm5) ? "[$n] fizzbuzz\n" : (!$nm5? "[$n] buzz\n" : (!$nm3? "[$n] fizz\n" : "[$n] $n\n"))) }

Questions:

  1. How bad is it?
  2. How much worse can it be?
  3. If I did this in an interview, would you hire me?
    1. Why would you do that to yourself?
  4. Which version offends the sensibilities more?
  5. What techniques for making my code more compact and less maintainable have I overlooked?

Replies are listed 'Best First'.
Re: Rate my fizzbuzz
by tybalt89 (Monsignor) on Oct 12, 2025 at 15:16 UTC
    What techniques for making my code more compact and less maintainable have I overlooked?
    #!/usr/bin/perl print +($_%3==0&&'fizz').($_%5==0&&'buzz')||$_,"\n" for 1 .. 60;
Re: Rate my fizzbuzz
by LanX (Saint) on Oct 12, 2025 at 13:37 UTC
    First of all, here the context for the unaware Fizz_buzz

    > If I did this in an interview, would you hire me?

    no yet, because it's only an entry filter

    Fizz buzz (often spelled FizzBuzz in this context) has been used as an interview screening device for computer programmers. Writing a program to output the first 100 FizzBuzz numbers is a relatively trivial problem requiring little more than a loop and conditional statements in any popular language, and is thus a quick way to weed out applicants with absolutely no programming experience.

    (from the WP article, emphasize added)

    > What techniques for making my code more compact and less maintainable have I overlooked?

    TIMTOWTDI

    for short idomatic Perl you could just stack the "Fuzz" behind the "Bizz" for the "double case"

    for my $i (1..100) { my $result; $result .= "Fizz" unless $i % 3; $result .= "Buzz" unless $i % 5; $result //= $i; # "unless defined $result;" print "$result\t\t"; print "\n" unless $i % 5; # that's just sugar for nicer tabul +ar view }

    the vanilla approach is to use if-elsif-else-chains

    for my $i (1..100) { if ( not $i % 15 ) { print "FizzBuzz"; } elsif ( not $i % 3 ) { print "Fizz"; } elsif ( not $i % 5 ) { print "Buzz"; } else { print $i; } print "\t\t"; print "\n" unless $i % 5; # that's just sugar for nicer tabular v +iew }

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

Re: Rate my fizzbuzz
by ysth (Canon) on Oct 13, 2025 at 00:32 UTC
    What techniques for making my code more compact and less maintainable have I overlooked?
    There are an infinite number of very different ways. Here's just one example:
    use 5.012; use warnings; sub task { state $task='@PERLMONKSFAITH'; exit if ++$_[0] > 100; $_->[ +ord("`"^" "^($task.=substr $task,0,1,''))] } $_->[0](my $n=1) for [ sub { say $_[0]; goto &{&task} }, sub { say "Fizz"; goto &{&task} }, sub { say "Buzz"; goto &{&task} }, sub { say $_[0]; goto &{&task} }, sub { say "FizzBuzz"; goto &{&task} }, sub { say "Fizz"; goto &{&task} }, sub { say $_[0]; goto &{&task} }, sub { say "Fizz"; goto &{&task} }, sub { say "FizzBuzz"; goto &{&task} }, sub { say $_[0]; goto &{&task} }, sub { say $_[0]; goto &{&task} }, sub { say "Fizz"; goto &{&task} }, sub { say "Buzz"; goto &{&task} }, sub { say "Fizz"; goto &{&task} }, sub { say $_[0]; goto &{&task} }, sub { say $_[0]; goto &{&task} }, sub { say $_[0]; goto &{&task} }, sub { say "Buzz"; goto &{&task} }, sub { say $_[0]; goto &{&task} }, sub { say "Buzz"; goto &{&task} }, sub { say $_[0]; goto &{&task} }, ];
      Or to be more compact:
      use 5.012;sub t{state$t='@PERLMONKSFAITH';++$_[0]>100?exit:$_->[ord("@ +"^($t.=substr$t,0,1,''))]}$_->[0](my $n=1)for[map{my$s=$_;sub{say$s?" +${s}zz":$_[0];goto&{&t}}}qw/0 Fi 0 0 0 Fi 0 0 FizzBu 0 0 Fi Bu Fi 0 0 + 0 0 0 Bu 0/]
Re: Rate my fizzbuzz
by CountZero (Bishop) on Oct 16, 2025 at 12:49 UTC
    What techniques for making my code more compact and less maintainable have I overlooked?
    Without rewriting your second example:
    • Use nonsensical variable names or variable names which differ only by one character or use variable names with lots of "O", "0", "l" or "1".
    • Put the whole program on one single line.
    • Then add random "cr/lf" at inappropriate places and, to annoy the snake-lovers, add random indents.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics