Think Perl6-ish Variables, Expressions, and Statements
Continuing the Perl 6 adventures with Allen B. Downey's Think Python book.
Maybe a brief bit about objects in Chapter 1 would be appropriate, because Perl 6 lets you query an object directly about the type associated with a value.
> 'Hello, World!'.WHAT Str() > 17.WHAT Int() > 3.2.WHAT Rat()
Strings that have numbers are still strings by default.
> '17'.WHAT Str() > '3.2'.WHAT Str()
Commas make lists.
> 1,000,000 1 0 0
Variables need to be declared -
my is a favorite.
> $message = 'And now for something completely different' Variable $message is not declared > my $message = 'And now for something completely different' And now for something completely different
Simple string and number variables are prefixed with
$. Perl 6 looks at "type" in at least two ways: the type of a value (
Rat, and so on) and the type of the variable that holds the value. For now, just remind folks to declare variables with
my and use
$ sigil for the variables they're working with at this point.
> my $n = 17; 17 > my $pi = 3.1415926535897932 3.1415926535897932
You can ask a variable what type of value it is holding right now using
> $message.WHAT Str() > $n.WHAT Int() > $pi.WHAT Rat()
Perl 6 doesn't actually care about leading zero in a value ordinarily. Caveats about literals in other languages don't apply here, except to remind folks about the difference between
> my $zipcode = 02492; 2492 > $zipcode = '02492'; 02492
Rules for variable names are similar to Python and other languages, though that sigil means reserved names aren't such a big deal.
> my $76trombones = 'big parade'; Cannot declare a numeric variable > my $more@ = 1000000 Two terms in a row > my $class = 'Advanced Theoretical Zymurgy' Advanced Theoretical Zymurgy
Note that Perl 6 errors are more descriptive than the infamous "SyntaxError: invalid syntax" of Python.
Oh, and Perl 6 allows
- in variable names.
> my $class-name = 'Advanced Theoretical Zymurgy'; Advanced Theoretical Zymurgy
Perl 6 does not do floor division by default. It will convert
Rat as needed for an operation.
> my $minute = 59 59 > $minute / 60 0.983333
say - remember
say? - to get output when running a Perl 6 program as opposed to the interactive shell.
my $miles = 26.2 say $miles * 1.61
$ perl6 sample-01.p6 ===SORRY!=== Two terms in a row at sample-01.p6:2 ------> <BOL>⏏say $miles * 1.61 expecting any of: postfix infix or meta-infix infix stopper statement end statement modifier statement modifier loop
Yeah. Better start using semicolons now. Probably should've been using semicolons from the beginning, but I was being lazy. Speaking of lazy, I just noticed that
perl6 doesn't seem to require the
use v6 pragma. Still, good form is important.
use v6; my $miles = 26.2; say $miles * 1.61;
$ perl6 sample-01.p6 42.182
Assignment lines don't produce output when running outside the REPL.
use v6; say 1; my $x = 2; say $x;
$ perl6 sample-02.p6 1 2
Type the following statements in the Perl 6 interpreter to see what they do:
5; my $x = 5; $x + 1;
Now put the same statements into a script and run it. What is the output? Modify the script by transforming each expression into a
say statement and then run it again.
say 5; my $x = 5; say $x + 1;
Order of Operations
No huge difference here for simple operations. PEMDAS. When in doubt, use parens.
Remember that Perl 6 is still Perl.
> '2' - 1' 1 > 'eggs' / 'easy' Cannot convert string to number: base-10 number must begin with valid digits or '.' in '⏏eggs' (indicated by ⏏)
Python concatenates strings with
+. Perl 6 concatenates strings with
my $first = 'throat'; my $second = 'warbler'; say $first ~ $second; # says 'throatwarbler'
Python does string repetition with
*. Perl 6 does string repetition with
> 'spam' x 3 spamspamspam
# This is a line comment, same as in loads of other languages.
Perl 6 error messages are fairly helpful.
> my $bad name = 5; Two terms in a row
Typos are a common source of errors when caffeine passes the critical threshold. Here is one of the more common typo errors: misspelling a variable name.
> my $principal = 327.68; 327.68 > my $interest = $principle * $rate; Variable $principle is not declared
Assume that we execute the following assignment statements:
my $width = 17; my $height = 12.0; my $delimiter = '.';
For each of the following expressions, write the value of the expression and the type (of the value of the expression).
$width / 2=
$width / 2.0=
Rat()Perl 6 just don't care about floor division
$height / 3=
Rat()Looks like an
Intbut it's a
1 + 2 * 5=
$delimiter x 5=
Use the Perl 6 interpreter to check your answers.
Practice using the Perl 6 interpreter as a calculator:
Volume of a sphere w/radius
ris 4/3πr3. What is the volume of a sphere with radius
5? Python hint doesn't apply.
use v6; my $radius = 5; my $PI = 3.1415926; my $area = 4/3 * $pi * $radius**3; say $area;
book cover prices is $24.95, bookstores get 40% discount. Shipping is $3 for first copy and 75 cents per additional copy. What's the total wholesale cost for 60 copies?
Just your basic calculator solution:
> 24.95 * 0.6 * 60 + 0.75 * (60 - 1) + 3 945.45
I had to break the next one down carefully, as I am a bear of very small brain.
If I leave my house at 6:52 am and run 1 mile at an easy pace (8:15 per mile), then 3 miles at tempo (7:12 per mile) and 1 mile at easy pace again, what time do I get home for breakfast?
I am willing to bet good money that my solution is more verbose than it needs to be.
use v6; my $minute = 60; my $hour = $minute * 60; # Turn start time to seconds after midnight my $start-time = (6 * 60 + 52) * 60; my $easy-pace = 8 * 60 + 15; my $tempo-pace = 7 * 60 + 12; my $running-time = $easy-pace + 3 * $tempo-pace + $easy-pace; # Find finish time as seconds after midnight my $finish-time = $start-time + $running-time; # Convert finish time to a clock time, first in hours: my $finish-hour = ($finish-time / $hour).Int; # ... then the seconds left over, expressed as minutes. my $finish-minute = ($finish-time % $hour / 60).Int; say "Finish time was $finish-hour:$finish-minute";
That gave me:
$ perl6 jogging.p6 Finish time was 7:30