PERL WEEKLY CHALLENGE – 031

After learning about the Perl Weekly challenge from the London Perl Workshop, I’ve decided to “challenge” myself and use it as a tool for learning Raku.

I’ve never touched Raku before this week and I plan to do both challenges when time allows. I quickly put together this blog and I expect to make more changes to this blog once I have some time.


Easy Challenge

“Create a function to check divide by zero error without checking if the denominator is zero.”

This wasn’t too difficult, just catch the exception for the division by 0. I created a few test cases in perl 5 and raku to make sure that the program didn’t error out.

It was interesting that Raku didn’t throw the exception until the Rational was coerced into a String.

Perl 5 solution

#!/usr/bin/perl
use strict;
use warnings;
use feature qw/ say /;

# A few test cases
for my $statement ('1/0', '1/2', '0/100') {
	my $answer = divide_by_zero_check($statement);
	(defined($answer)) ?
			say $statement . ' - ' . $answer :
			say $statement . ' - ' . 'divide by 0 error';
}

# Return the $answer or undef if we can't divide by 0
sub divide_by_zero_check {
	my $answer;
	eval { $answer = eval shift; };
	return $answer;
}

Output

1/0 – divide by 0 error
1/2 – 0.5
0/100 – 0

Raku solution

use v6.d;

sub MAIN () {
	# A few test cases
	my @array = ['1/0', '1/2', '0/100'];
	for @array -> $array-item {
		divide-by-zero-check($array-item);
	}
}

# Catch the error and say if we can't divide by 0
sub divide-by-zero-check(Str $statement) {
	try {
		my $answer = Rat($statement);
		say $answer;

		CATCH {
			default { say "divide by 0 error, $_" }
		}
	}
}

Output

divide by 0 error, Attempt to divide by zero when coercing Rational to Str
0.5
0


Hard Challenge

“Create a script to demonstrate creating dynamic variable name, assign a value to the variable and finally print the variable. The variable name would be passed as command line argument.”

This one is a bit more difficult, creating a dynamic variable in perl5 wasn’t a problem.

Doing it in perl 6 was a bit difficult But I learned quite a few things in Raku like it’s inability to declare variables at run-time. In the end I decided to use the GLOBAL namespace after a coworker showed it to me.

Perl 5 solution

#!/usr/bin/perl
use strict;
use warnings;
use feature qw/ say /;
no strict "refs";

our $test; # for testing this

# Randomly populate the dynamic variable
my $dynamic_variable_name = $ARGV[0];
my $random_value          = rand('99999999999');
$$dynamic_variable_name   = $random_value;

# Say random variable name and value
say 'Random variable name: ' .  $dynamic_variable_name;
say 'Random value: ' . ${$dynamic_variable_name};

# test like this: ch2.pl test
say 'Variable test is: ' .  $test 
	if ($dynamic_variable_name eq 'test');

Output

Random variable name: test
Random value: 19870290795.9904
Variable test is: 19870290795.9904

Raku solution

use v6.d;

sub MAIN (Str $variable) {
	# Randomly populate the random value
	my $random_value = (0..^9).roll(12).join;
	GLOBAL::{'$' ~ $variable} = $random_value;

	# Say dynamic variable name and random value
	say 'Dynamic variable name: ' ~  $variable;
	say 'Random value: ' ~ GLOBAL::{'$' ~ $variable};

	# test like this: perl6 ch2.p6 test
	say 'Variable test is: ' ~ $*test if ($variable eq 'test');
}


Output

Dynamic variable name: test
Random value: 821038806157
Variable test is: 821038806157

3 thoughts on “PERL WEEKLY CHALLENGE – 031

  1. Hi Javier,

    you write: it was interesting that Raku didn’t throw the exception until the Rational was coerced into a String. That’s entirely correct, But, more finely, you could say that the exception is not thrown when you assign the result of an illegal division by zero to a variable, but only when you want to use this variable for something else.

    I’m not entirely convinced by this, but I guess that, in some cases, it prevents the program from aborting when there is no need for this to happen. For example, suppose you compute a large array of values, and one of these values has a division by zero error, there is no reason to let the program die if, in reality, that value is never used afterward. In some cases, this can make your programs more flexible and help you to avoid having manage separately special cases.

    Best, Laurent.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s