Learning RPerl
~ or ~
Let's Write Fast Perl!
.
...
.....
.......
being
The Official Introductory-Level Reference, User Manual, and Educational Documentation
~ for ~
Restricted Perl, The Optimizing Perl 5 Compiler
For Anna.
0th Edition, Pre-Release Copy
"FOREWORD"
"PREFACE"
"CHAPTER 1: INTRODUCTION"
"CHAPTER 2: SCALAR DATA"
"CHAPTER 3: LISTS & ARRAYS"
"CHAPTER 4: SUBROUTINES"
"CHAPTER 5: INPUT & OUTPUT"
"CHAPTER 6: HASHES"
"CHAPTER 7: REGULAR EXPRESSIONS"
"CHAPTER 8: REGULAR EXPRESSIONS MATCHING"
"CHAPTER 9: REGULAR EXPRESSIONS PROCESSING"
"CHAPTER 10: CONTROL STRUCTURES"
"CHAPTER 11: CLASSES, PACKAGES, MODULES, LIBRARIES"
"CHAPTER 12: FILE TESTS"
"CHAPTER 13: DIRECTORY OPERATIONS"
"CHAPTER 14: STRINGS & SORTING"
"CHAPTER 15: SMART MATCHING & GIVEN-WHEN"
"CHAPTER 16: PROCESS MANAGEMENT"
"CHAPTER 17: SOME ADVANCED TECHNIQUES"
"APPENDIX A: EXERCISE ANSWERS"
"APPENDIX B: RPERL GRAMMAR"
"APPENDIX C: BEYOND THE ROADRUNNER"
[ INSERT FOREWORD CONTENT HERE ]
My name is William N. Braswell, Jr.; I am also known by a number of other names including Will the Chill, The Voice In The Wilderness, Skipper Brassie ("braz-ee"), and just Will.
I have a degree in computer science and mathematics from Texas Tech University, I have worked as a Perl software developer for over 15 years, and I am the founder of the Auto-Parallel Technologies consulting company.
LinkedIn Profile
GitHub Profile
I am 1 of 3 co-founders of the Perl 11 movement.
Perl11.org
I am also the President of the Austin Perl Mongers.
Austin.pm
Most importantly, I am the creator of the RPerl optimizing compiler for Perl 5, about which you are currently reading!
RPerl.org
Using RPerl is different enough from normal Perl 5 to necessitate the creation of in-depth user documentation.
Manual pages and cookbooks and example source code alone are not enough, only a full textbook can provide the level of detail necessary to truly learn RPerl.
This is that textbook.
RPerl v1.0 was released on US Independence Day, July 4th, 2015; 6 days later, work began on the source code solution to exercise 1 of chapter 1 of this book:
First Learning RPerl Commit on GitHub
Significant GitHub commit dates include, but are not limited to the following:
<noncode>
$ git log --reverse --all --date=short --pretty='%cd: %s' | grep 'Learning RPerl' 2015-07-10: Learning RPerl, Chapter 1, Exercise 1, Hello World 2015-07-11: Learning RPerl, Chapter 2, Exercise 1, Circumference Of Circle With Radius 12.5 2015-07-12: Learning RPerl, Chapter 2, Exercise 1, Circumference Of Circle With Radius 12.5, Part 2 2015-07-13: Learning RPerl, Chapter 2, Exercise 2, Circumference Of Circle With Any Radius 2015-07-14: Learning RPerl, Chapter 2, Exercise 3, Circumference Of Circle With Any Positive Radius 2015-07-15: Learning RPerl, Chapter 2, Exercise 4, Product Of Any Two Numbers 2015-07-16: Learning RPerl, Chapter 2, Exercise 5, String Repeat 2015-07-18: Learning RPerl, Chapter 3, Exercise 1, STDIN Strings Reverse 2015-07-17: Learning RPerl, Chapters 1 & 2, Add Source Comments 2015-07-19: Learning RPerl, Chapter 3, Exercise 2, STDIN Array Indices 2015-07-20: Learning RPerl, Chapters 2 & 3, Add Source Comments 2015-07-21: Learning RPerl, Chapter 3, Exercise 3, STDIN Strings Sort 2015-07-22: Learning RPerl, Chapter 4, Exercise 1, Subroutine For Stringified Numbers Total 2015-07-23: Learning RPerl, Chapter 4, Exercise 2, Subroutine For Total Of 1 To 1,000 2015-07-25: Learning RPerl, Chapter 4, Exercise 3, Subroutines For Above-Average Array Elements 2015-10-03: Learning RPerl, Chapter 4, Exercise 4, Subroutine To Greet User 2015-10-04: Learning RPerl, Chapter 4, Exercise 5, Subroutine To Greet Multiple Users 2015-10-05: Learning RPerl, Chapter 5, Exercise 1, Print Input File(s) Lines In Reverse, Part 1 2015-10-06: Learning RPerl, Chapter 5, Exercise 1, Print Input File(s) Lines In Reverse, Part 2 2015-10-07: Learning RPerl, Chapter 5, Exercise 2, Print Input Line(s) Right-Justified 2015-10-08: Learning RPerl, Chapter 5, Exercise 3, Print Input Line(s) Variable-Width Right-Justified, Part 1 2015-10-09: Learning RPerl, Chapter 5, Exercise 3, Print Input Line(s) Variable-Width Right-Justified, Part 2 2015-10-10: Learning RPerl, Chapter 6, Exercise 1, Print Family Names 2015-10-11: Learning RPerl, Chapter 6, Exercise 2, Unique Word Count 2015-10-12: Learning RPerl, Ensure All Exercises Parse, Part 1 2015-10-13: Learning RPerl, Ensure All Exercises Parse, Part 2 2015-10-19: Learning RPerl, Ensure All Exercises Parse, Part 3 2015-10-20: Learning RPerl, Ensure All Exercises Parse, Part 4 2015-10-21: Learning RPerl, Ensure All Exercises Parse, Part 5 2015-10-22: Learning RPerl, Ensure All Exercises Parse, Part 6 2015-10-23: Learning RPerl, Ensure All Exercises Parse, Part 7 2015-10-24: Learning RPerl, Ensure All Exercises Parse, Part 8 2015-10-25: Learning RPerl, Ensure All Exercises Parse, Part 9 2015-10-26: Learning RPerl, Ensure All Exercises Parse, Part 10 2015-10-27: Learning RPerl, Ensure All Exercises Parse, Part 11 2015-10-28: Learning RPerl, Ensure All Exercises Parse, Part 12 2015-10-28: Learning RPerl, Ensure All Exercises Parse, Part 13 2015-10-28: Learning RPerl, Ensure All Exercises Parse, Part 14 2015-11-07: CPAN Release, v1.200002; Temporarily Fix Macintosh Build Failures, Learning RPerl Parse Failure, Etc. 2015-11-10: Learning RPerl, Content, Part 1 2015-11-11: Learning RPerl, Content, Part 2 2015-11-14: Learning RPerl, Content, Part 3 2015-11-15: Learning RPerl, Content, Part 4 2015-11-16: Learning RPerl, Content, Part 5 2015-11-16: Learning RPerl, Content, Part 6 2015-11-17: Learning RPerl, Content, Part 7 2015-11-18: Learning RPerl, Content, Part 8 2015-11-18: Learning RPerl, Content, Part 9 2015-11-19: Learning RPerl, Content, Part 10 2015-11-20: Learning RPerl, Content, Part 11 2015-11-21: Learning RPerl, Content, Part 12 2015-11-22: Learning RPerl, Content, Part 13 2015-11-24: Learning RPerl, Content, Part 14 2015-12-31: Learning RPerl, Content, Part 15 2015-12-31: Learning RPerl, Content, Part 16 2016-01-01: Learning RPerl, Content, Part 17 2016-01-02: Learning RPerl, FontAwesome & MetaCPAN Files, Initial Commit 2016-01-02: News, RPerl v1.5 Release; Learning RPerl, FontAwesome & MetaCPAN Files, Move To Subdirectories 2016-01-02: Learning RPerl, Update pod2rperlhtml.pl Script, Correct CSS & JS Paths 2016-01-02: Learning RPerl, HTML Content, Initial Commit 2016-01-03: Learning RPerl, Content, Part 18 2016-01-05: Learning RPerl, Content, Part 19 2016-01-06: Learning RPerl, Content, Part 20 2016-01-07: Learning RPerl, Content, Part 21 2016-01-08: Learning RPerl, Content, Part 22 2016-01-09: Learning RPerl, Content, Part 23 2016-01-09: Learning RPerl, Content, Part 24 2016-01-10: Learning RPerl, Content, Part 25 2016-01-11: Learning RPerl, Content, Part 26 2016-01-12: Learning RPerl, Content, Part 27 2016-01-14: Learning RPerl, Content, Part 28 2016-01-15: Learning RPerl, Content, Part 29 2016-01-16: Learning RPerl, Content, Part 30 2016-01-16: Learning RPerl, Content, Part 31 2016-01-16: Learning RPerl, Content, Part 32 2016-03-03: Learning RPerl, Chapter 2 Content, Part 1 2016-03-03: Learning RPerl, TPF Grant #2, Part 1
</noncode>
This book was made possible in part by 2 generous grants from The Perl Foundation, as part of the September 2015 and January / Febuary 2016 rounds of funding.
Special thanks to TPF Grants Committee Secretary, Makoto Nozaki; TPF Grant Manager, Mark Jensen; TPF Grants Committee's various supporting members; and everyone who gave positive feedback on the grant proposals.
A history of TPF grant #1 may be found at the following links:
Call For Grant Proposals
Grant Proposal
Grant Proposals
Grant Proposal Votes
Grant Report 1
Grant Report 2
Grant Report 3
A history of TPF grant #2 may be found at the following links:
Grant Proposal: RPerl User Documentation, Part 2
Jan / Feb 2016 Grant Votes
Countless people have contributed to the development of RPerl; from source code to bug testing to financial donations to emotional support, it truly takes a village to build a compiler!
Below are the contents of the official RPerl thank-you file, listing the handles (online nicknames) and names of the most important RPerl contributors. If you don't already know who these people are, you will be pleasantly surprised by researching each of them.
Latest THANKS File
Many Thanks To irc.perl.org: #perl5 Founder timtoady #perl6 Founder timtoady (again) #perl11 Founders ingy & rurban & willthechill (yours truly) #perl11 Members bulk88 & mst #inline Founders ingy (again) & nwatkiss #inline Members davido & mohawk & sisyphus Additional Thanks To: Eyapp Creator Casiano Rodriguez-Leon, PhD Austin Perl Mongers All RPerl Contributors & Users & Supporters
I'm sure I will make errors while writing this book.
I may even upset some people, particularly those who have an emotional or financial investment in slow Perl software.
Despite my best efforts, I remain a fallible human being; thus, bad spelling and grammer and run-on sentences and parts that are hard to understand and parts that are not funny and formattiNg errors and bad spelling adn repetitions and other annoyances will doubtless plague this tome but we must not allow such trivialities as, improper punctuation to affect our willingness and ability to learn how to write super-fast RPerl software.
If you find a mistake in this book (other than in the immediately preceeding paragraph), please utilize the following link to create a new GitHub issue (bug report) using a title starting with the words "Learning RPerl":
New GitHub Issue
I will try my best to create an engaging and educational experience for you, the reader; however, in anticipation of the inevitable disappointment you may experience, I can only humbly offer...
I'M SORRY!
"The Pod format is not necessarily sufficient for writing a book."
http://perldoc.perl.org/perlpod.html
~ Saint Larry Wall & Sean M. Burke
"Challenge accepted."
https://github.com/wbraswell/rperl/blob/master/script/development/pod2rperlhtml.pl
~ Will Braswell
You are about to learn the basic concepts of writing software using the RPerl optimizing compiler for the Perl computer programming language. With the skills gained by reading this book, you will be empowered to create new super-fast RPerl programs which can be intermixed with the enormous amount of existing Perl software available on the Internet.
This book is named and stylized for the animal mascot for RPerl, Roadie the Roadrunner. RPerl, like Roadie, "runs really fast".
Throughout this text, the following 12 typography conventions are utilized:
"Literal Quotation"
"First Occurrence Of Key Concept"
Emphasis
Stronger Emphasis
/path/to/program.pl
Program Name
Book Title
BEST PRACTICES
Hyperlink
$inline_code_snippet = 'unhighlighted';
my string $indented_code_block = 'highlighted on RPerl.org & MetaCPAN.org'; # with comments my integer $more_code = 17; # http://www.catb.org/jargon/html/R/random-numbers.html return 'end of indented code block';
$ terminal_command.pl with arguments Please provide input: foo bar Your output is: howdy howdy howdy
This book is purposefully patterned after the popular educational text Learning Perl, affectionately known as the Llama Book. Both the Roadrunner Book and the Llama book are meant as introductory texts on Perl topics. The Llama Book is focused on normal Perl, and the Roadrunner Book is focused on optimized Perl.
This book copies the same chapter topics as Learning Perl, but all content is re-written for RPerl. Learning RPerl also copies the same exercise concepts as Learning Perl, but all solutions are re-written in RPerl. Both books are canonical and may be used together in the classroom; the source code solutions are meant to be compared side-by-side as textbook examples of normal Perl versus optimized Perl.
Please support the Perl community by purchasing a copy of Learning Perl from our friends at O'Reilly:
http://shop.oreilly.com/product/0636920018452.do
Are you totally new to computer programming, and you want to learn how to write your first program?
Do you already know Perl, and now you want to make your Perl code run faster?
Do you already know some other computer language, and Perl has always intrigued you?
Do you love learning about new languages and compilers?
Do you miss you favorite old language Perl, and you're looking for a legitimate reason to go back?
Are you a scientist and you just want your code to run really fast without the headaches of C or C++?
If you answered "yes" to any of these questions, then the Roadrunner Book is definitely for you!
If you answered "no" to all of these questions, then this book may still be for you, give it a try!
If you hate Perl, or only love slow software, or wish all computers would explode, then we suggest some soul-searching and a few Saint Larry videos. You'll thank us in the morning.
This is a purposefully simple book, in the same way RPerl is a purposefully simple subset of the full Perl 5 programming language.
There are one or more programming exercises at the end of every chapter, and full answers to each problem are given near the end of the book in Appendix A.
For maximum educational effect, we suggest you attempt to write each piece of code on your own before looking at our solutions.
If you are using this as an official textbook for certification or academic credit, such as at LAMPuniversity.org or a traditional school, you are obviously expected to write all your own code without refering to our or anyone else's solutions whatsoever. We suggest you enclose Appendix A with a paperclip or discard it altogether to avoid the potential for accidental academic dishonesty.
The original authors of Learning Perl meant the numbers at each exercise to indicate the approximate number of minutes required for an average person to reach a full working solution. If it takes you less time, good for you! If it takes you more time, don't worry, it's no big deal; learning technical skills requires time and dedication. All experts were once novices.
Thank you for helping spread the love of Perl and the speed of RPerl!
As previously mentioned, this book may either be used solo or combined with Learning Perl. For students who are not already familiar with Perl, you may wish to use this text alone in order to simplify and ease the learning experience. For students who are already familiar with Perl or other dynamic programming languages like the snake or the red gemstone, you may wish to use both textbooks for a more in-depth compare-and-contrast approach.
RPerl stands for "Restricted Perl", in that we restrict our use of Perl to those parts which can be made to run fast. RPerl also stands for "Revolutionary Perl", in that we hope RPerl's speed will revolutionize the software development industry, or at least the Perl community. RPerl might even stand for "Roadrunner Perl", in that it runs really fast.
Will loves Perl and the Perl community.
Will is a scientist and needs his code to run really fast.
Will doesn't like the hassle of writing code in C or C++ or XS or Inline::C or Inline::CPP.
Will waited a decade or two before realizing he had to do it himself.
Dynamic languages like Perl are fast at running some kinds of computational actions, such as regular expressions (text data pattern matching) and reading from a database.
Unfortunately, dynamic languages are slow at running general-purpose computations, such as arithmetic and moving data around in memory. Sometimes very slow.
Dynamic languages like Perl are also flexible, powerful, and relatively easy to learn. Sometimes too flexible.
RPerl's goal is to keep all of Perl's power and ease-of-use, while removing the redundant parts of Perl's flexibility in order to gain a major runtime speed boost.
The most complex and flexible parts of Perl are called "high magic", so RPerl is focused on supporting the "low magic" parts of Perl which can be made to run fast.
RPerl is specifically designed to remove the confusing and complicated parts of Perl.
RPerl also introduces a number of additional rules and templates which are not present in normal Perl, notably including the use of real data types.
The net effect of removing Perl complexity and adding RPerl rules falls in favor of RPerl, due primarily to the exceedingly complex nature of Perl.
In other words, RPerl is easier to learn and use than dynamic languages like normal Perl, and most any other language in general.
The RPerl team has been regularly promoting RPerl in a number of physical and digital venues, including but not limited to:
YAPC::NA http://www.yapcna.org
Austin Perl Mongers http://www.austin.pm
RPerl Website http://www.rperl.org
Github https://github.com/wbraswell/rperl
Facebook Page https://www.facebook.com/rperlcompiler
Facebook Groups https://www.facebook.com/groups/perlprogrammers https://www.facebook.com/groups/perlmonger
Twitter https://twitter.com/rperlcompiler
PerlMonks http://perlmonks.org
Perl Blogs http://blogs.perl.org
IRC #perl11 http://irc.perl.org
As of Q2 2016, RPerl v1.7 (codename Tycho) has been publicly released and is in use by a number of early adopters around the world.
RPerl development is proceeding with financial support from both Kickstarter crowdfunding and official grant monies from The Perl Foundation.
The RPerl community is beginning to grow, and there are a number of exciting RPerl projects currently in the works. More info coming soon!
RPerl is a general-purpose programming language, which means you can use RPerl to efficiently and effectively implement virtually any kind of software you can imagine.
RPerl is especially well-suited for building software which benefits from speed, such as scientific simulations and graphical video games.
RPerl is also good for building software which utilizes Perl's strong-suit of string manipulation; RPerl currently supports basic string operators, with full regular expression support to be added in an upcoming version.
RPerl has purposefully disabled the most complex features of Perl, such as run-time code evaluation, secret operators, and punctuation variables. If you have purposefully designed your Perl software to depend on these high-magic features, or you are unconditionally committed to continue using high-magic language features, then maybe RPerl isn't for you.
Installing RPerl ranges from easy to difficult, depending on how well your operating system supports Perl and C++.
On modern operating systems with good Perl support, such as Debian or Ubuntu GNU/Linux, you should be able to install RPerl system-wide by running the following command at your terminal command prompt:
$ sudo cpan RPerl
If RPerl is properly installed, you should see a short text message displayed when you type the following command:
$ rperl -v
On operating systems with less Perl support, you may have to perform a number of steps to successfully install RPerl, with dry technical detail available in the INSTALL notes document:
https://github.com/wbraswell/rperl/blob/master/INSTALL
Unless you are an experienced programmer or system administrator, it is strongly recommended you use the Xubuntu operating system. You can download the Xubuntu ISO file at the link below, then use it to create a bootable DVD disc or USB flash drive, install Xubuntu onto any computer, and issue the $ sudo cpan RPerl command as described above.
http://xubuntu.org/getxubuntu
If you are interested in viewing the source code of RPerl itself, you may find the latest major release of RPerl (stable) on CPAN:
https://metacpan.org/author/WBRASWELL
You may find the latest development release of RPerl (possibly unstable) on Github:
https://github.com/wbraswell/rperl
CPAN is the "Comprehensive Perl Archive Network", the world's most successful and mature centralized software network.
CPAN servers are where most public Perl software is stored, including RPerl.
https://en.wikipedia.org/wiki/CPAN
http://www.cpan.org
Several other programming language communities have copied the success and implementation of CPAN, including JSAN for Javascript, CRAN for R, and CCAN for C.
Official RPerl technical support is provided through Auto-Parallel Technologies, Inc.
To request more information, please send an e-mail to the following address:
william DOT braswell AT autoparallel DOT com
Free technical support for non-commercial users is provided by the RPerl community through IRC.
The primary bug-tracking platform for RPerl is Github Issues, where you may file a new bug report ("new issue") if it is not already listed:
https://github.com/wbraswell/rperl/issues
Although Github Issues is strongly preferred, the RPerl development team also supports the legacy CPAN ticket system:
https://rt.cpan.org/Public/Dist/Display.html?Name=RPerl
Computer programs written using the RPerl language are plain text files, which means you can use any text editor to create and modify your RPerl source code. Examples of common text editors include Notepad, Pico, and Vi.
http://www.vim.org
To avoid possible file format problems, do not edit your RPerl programs using a word processor such as Wordpad, Word, OpenOffice, or LibreOffice.
Experienced RPerl developers may choose to utilize an "integrated development environment" (IDE), which is a special text editor made for writing software. Examples of common Perl IDE applications include Eclipse EPIC, Padre, and Komodo (non-free).
http://www.epic-ide.org
http://padre.perlide.org
http://komodoide.com/perl
#!/usr/bin/perl # Learning RPerl, Chapter 1, Exercise 3 # Foo Bar Arithmetic Example # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer $foo = 21 + 12; my integer $bar = 23 * 42 * 2; my number $baz = $bar / $foo; print 'have $foo = ' . to_string($foo) . "\n"; print 'have $bar = ' . to_string($bar) . "\n"; print 'have $baz = ' . to_string($baz) . "\n";
This program is separated by blank lines into 4 sections: shebang, header, critics, and operations.
Other than the shebang and critics, all lines beginning with # are comments and can be safely ignored or discarded without affecting the program.
#
The "shebang" section is required, always contains exactly 1 line, and is short for "hash bang"; referring to the two leading characters #! of this line. The "octothorpe" character # (tic-tac-toe symbol) is called a "pound sign" when used on a telephone, and is called a "hash" (or more recently and less accurately "hash tag") when used on a computer. The exclamation point character ! is called a "bang" when used on a computer. When appearing together as the first two characters in a plain text file, the hash and bang characters tell the operating system to run the immediately-following command (in this case the Perl interpreter located at /usr/bin/perl) and pass the remaining contents of the text file as input to the command. In other words, if the first line of a plain text file is #!/usr/bin/perl, then that file is a Perl program.
#!
!
#!/usr/bin/perl
The "header" section is required and always contains 4 lines for an RPerl "program" file ending in .pl, or 5 lines for an RPerl "module" ending in .pm (covered later in Chapter 11). use is recognized by Perl as a special "keyword" (AKA "built-in operator") which has 2 primary purposes: to load additional RPerl modules, and to enable RPerl "pragma" system configuration modes. The use RPerl; line is dual-purpose, it both loads the RPerl.pm module and enables the special RPerl low-magic pragma. The use strict; and use warnings; lines enable basic Perl pragmas which require decent programming practices by the human programmers. The our $VERSION = 0.001_000; line sets the version number of this RPerl program.
use
use RPerl;
use strict;
use warnings;
our $VERSION = 0.001_000;
The "critics" section is included as necessary and may contain 1 or more lines beginning with ## no critic, which disable the errors caused by the over-restrictive nature of some Perl::Critic policies. There are currently 6 critics commands enabled for normal RPerl users, the first 2 of which are given in this example. The USER DEFAULT 1 critics command allows the use of numeric values such as 21 and 12, as well as the common print command. The USER DEFAULT 2 critics command allows the printing of 'have $foo = ', where a single-quoted ' string literal value contains the the $ dollar sigil (covered later in Chapter 2).
## no critic
USER DEFAULT 1
21
12
print
USER DEFAULT 2
'have $foo = '
'
$
The "operations" section is required and contains 1 or more lines of general-purpose RPerl source code. This is the main body of your program. The 6 lines of source code in our example are used to perform some simple arithmetic and display the results. The my integer $foo = 21 + 12; line declares a new variable named $foo which will only contain non-floating-point numeric data, and which is initialized to contain the arithmetic result of numeric literal values 21 plus 12. The my integer $bar = 23 * 42 * 2; line does much the same thing, creating a new numeric variable named $bar and initialized with 23 times 42 times 2. The print 'have $foo = ' . to_string($foo) . "\n"; and following 2 lines will display on screen (not send to paper printer) the labeled values of $foo, $bar, and $baz respectively. The . dot operator is string concatenation, used in this example to create one string out of 3 parts so there is only 1 argument parameter passed to the print command. The to_string() RPerl operator converts a numeric value to an underscore-formatted string value, suitable for use via the print operator. The "n" in the "\n" double-quoted string literal values stands for "newline", which inserts a carriage return to place the next piece of printed data down on the following line.
my integer $foo = 21 + 12;
$foo
my integer $bar = 23 * 42 * 2;
$bar
23
42
2
print 'have $foo = ' . to_string($foo) . "\n";
$baz
to_string()
"\n"
Normal Perl source code is executed using a software mechanism known as "interpretation", which is to say that Perl is an "interpreted" language and the /usr/bin/perl command is called the "Perl interpreter". The primary alternative to interpretation is "compilation", so RPerl is a "compiled" subset of the Perl language and the /usr/bin/rperl command is called the "RPerl compiler".
Like the Perl interpreter, the RPerl compiler accepts 2 different input file types: Perl programs which end in .pl and Perl modules which end in .pm. Perl program files actually run and execute actions, optionally receiving some functionality from 1 or more Perl module files if specified. Perl modules do not run or execute actions themselves, they only provide functionality which must in turn be called from a Perl program, or from another Perl module which eventually gets called by a Perl program.
A list of all valid RPerl compiler options may be seen by issuing the following command:
$ rperl -?
You may find the same information by viewing the following links:
rperl
https://metacpan.org/pod/distribution/RPerl/script/rperl
To partially-compile-then-execute the preceeding RPerl example program in test mode, you may copy and paste the entire program (from shebang to second print) into a temporary file such as /tmp/foobar.pl, then execute the following command:
$ rperl -t /tmp/foobar.pl
The output of this example program should be:
have $foo = 33 have $bar = 1_932 have $baz = 58.545_454_545_454_5
Please see "CHAPTER 11: CLASSES, PACKAGES, MODULES, LIBRARIES" for more information about compiling Perl modules.
Will Braswell does more than just create Perl compiler software, he is also very active in several other areas of life, including but not limited to:
Church & Spirituality
Boy Scouts of America
Cane Juggling & Circus Performance
Linux Operating Systems
Charitable & Fraternal Organizations
Homeschooling & Higher Education
Astrophysics & Mathematics
Entrepreneuriship & Business
High-Performance Computing
Professional Space Exploration
Family Life
These areas of interest are reflected in the tone and intention of RPerl.
The RPerl project officially began as a New Year's Resolution on January 1st, 2013. Following the grand tradition of Perl creator "Saint" Larry Wall, RPerl version releases are often timed to coincide with major holidays.
After 1 year of work, RPerl v1.0beta1 was released on New Year's Day 2014, eventually followed by RPerl v1.0beta2 on Christmas 2014.
The much-anticipated RPerl v1.0 full release was made on US Independence Day 2015, and RPerl v1.2 came on Halloween 2015.
RPerl v1.3 was released on Thanksgiving 2015, followed by RPerl v1.4 on Christmas 2015, and so forth.
RPerl v1.0 was funded through a Kickstarter campaign, then RPerl v1.2 and v1.3 were funded through a second Kickstarter campaign. Work on the first 6 chapters of this book was funded, in part, by a grant from The Perl Foundation.
The question of "How fast is RPerl?" does not have one simple answer; instead there are several factors and configuration modes to be taken into consideration. A relatively detailed description of the performance and modes may be found at the following link:
http://rperl.org/performance_benchmarks.html
The most condensed answer is that "RPerl is really fast." Utilizing RPerl's fastest execution modes, we see performance very close to the highly-optimized C++ programming language, which means RPerl is now among the short list of "world's fastest languages" along with C, C++, and Fortran.
The high-magic features of Perl are primarily responsible for how slow Perl runs for general-purpose computations. The "R" in RPerl stands for "Restricted", in that we restrict ourselves to only use the low-magic features of Perl which can run really fast.
The definitive list of do's and do-nots for high-magic vs low-magic Perl programming is called The Low Magic Perl Commandments (LMPC). There are 64 total commandments split into 5 groups of Ideals, Magic, Data, Operations, and Object-Orientation. The "Thou Shalt" commandments appear in the left column, and the "Thou Shalt Nots" appear on the right.
http://rperl.org/the_low_magic_perl_commandments.html
The LMPC draw inspiration from, and (wherever possible) work together with Damian Conway's Perl Best Practices and Jeffrey Thalhammer's Perl::Critic software.
http://shop.oreilly.com/product/9780596001735.do
http://search.cpan.org/~thaljef/Perl-Critic/lib/Perl/Critic/PolicySummary.pod
Perlism is the computer religion dedicated to the use, promotion, and development of the Perl family of programming languages. (Not to be confused with a spiritual religion such as Christianity, a computer religion such as Perlism is an independent and complementary belief structure.)
A Perlite is an adherent to the Perlism religion. Perlism has a revered founder, Saint Larry (himself a devout Christian); a prophet, The Voice In The Wilderness (Will); a monastary and shrine, Perl Monks; commandments, The LMPC; proverbs from Saint Larry including TIMTOWTDI, LMFB, and HTAAOF; and canonical scriptures, including Saint Larry's Apocalypses and The Voice's The Book Of RPerl.
The Book is a description of events surrounding the creation of RPerl and the future of the Internet. It is intended to both educate and entertain.
http://rperl.org/the_book_of_rperl.html
St. Larry has given us short and powerful proverbs, some of which are meant to have a purposefully tongue-in-cheek or sarcastic interpretation.
Will has provided a corollary to each of St. Larry's official proverbs.
St. Larry's Original Proverb
Will's Corollary Proverb
3 Great Virtues Of A Programmer:
LIH (Laziness, Impatience, Hubris)
3 Greater Virtues Of A Programmer:
DPH (Diligence, Patience, Humility)
TIMTOWTDI (There Is More Than One Way To Do It)
TDNNTBMTOWTDI (There Does Not Need To Be More Than One Way To Do It)
TIOFWTDI (There Is One Fastest Way To Do It)
LMFB (Let Many Flowers Bloom)
PTBF (Pick The Best Flowers)
HTAAOF (Have The Appropriate Amount Of Fun)
DTAAOW (Do The Appropriate Amount Of Work)
In addition to St. Larry's official proverbs, there are a number of other commonly-used catch phrases and ideas in the Perl community.
Original Catch Phrase
Will's Corollary Catch Phrase
Perl 5 Is The Camel
Perl 5 Is The Raptor
Perl 6 Is The Butterfly
RPerl Is The Roadrunner
Perl Is The Onion
RPerl Is The Scallion
Perl Is The Swiss Army Chainsaw
RPerl Is The Sword
Perl Is Line-Noise
Perl Is A Write-Only Language
RPerl Is Best Practices
On a computer with RPerl already installed, create a directory named LearningRPerl containing a sub-directory named Chapter1. Using the Foo Bar example program as a template, manually type a new RPerl program into a file named exercise_1-hello_world.pl inside the LearningRPerl/Chapter1 sub-directory. The sole purpose of your first program is to use the print operator and simply display the following one line of text output, followed by one newline character:
Hello, World!
Run your new program by issuing the following command at your terminal command prompt:
$ rperl -t LearningRPerl/Chapter1/exercise_1-hello_world.pl
HINT: You only need the USER DEFAULT 1 critic line, so your resulting program should be 7 lines long, not counting comments or blank lines.
First, run the following RPerl command, and observe the output for use in 2a and 2b below:
2a. What are some RPerl command-line options with which you are already familiar?
2b. With which options are you unfamiliar?
Next, run the following 3 RPerl commands, for 2c and 2d below:
$ rperl -t -V LearningRPerl/Chapter1/exercise_1-hello_world.pl $ rperl -t -D LearningRPerl/Chapter1/exercise_1-hello_world.pl $ rperl -t -V -D LearningRPerl/Chapter1/exercise_1-hello_world.pl
2c. How do the outputs of these 3 commands differ from the output of Exercise 1?
2d. How do the outputs differ from one another?
Manually type the entire Foo Bar example program into a file named exercise_3-foo_bar_arithmetic.pl inside the LearningPerl/Chapter1 sub-directory. (Even if you have already used copy-and-paste on the Foo Bar example program, you should still use this as an opportunity to build some RPerl muscle memory and type it in by hand.)
Modify your program by adding an extra numeric variable named $baz, set its value to $bar / $foo, and use print to generate the following output:
$bar / $foo
have $foo = 33 have $bar = 966 have $baz = 29.2727272727273
Run your program thusly:
$ rperl -t LearningRPerl/Chapter1/exercise_3-foo_bar_arithmetic.pl
Most programming languages include the basic principles of using named "variables" to store data values such as numbers, text strings, and lists of multiple numbers or strings. Multiple variables may be created, each with different names such as $foo or $bar or $quux, and each potentially containing a different value.
$quux
A single piece of data, such as one number or one string, is called a "scalar". Multiple pieces of data combined into a single aggregate structure may be either an "array" or a "hash", described in chapters 3 and 6, respectively. (Although sharing the same terminology, the hash data structure is not related to the hash # tic-tac-toe character.) In normal Perl, only scalar variable names begin with the $ dollar sign "sigil", while aggregate data structures are stored in variables starting with different sigils like @ or %. In RPerl, all variable names begin the $ sigil, both scalar types and aggregate structures alike.
@
%
RPerl provides 7 scalar data types:
boolean
unsigned_integer
integer (core)
integer
gmp_integer
number (core)
number
character
string (core)
string
Of the 7 RPerl scalar data types, 3 are directly (natively) supported by the Perl 5 core: integer, number, and string. This means the Perl 5 core is capable of directly identifying and storing those 3 core types. The remaining 4 non-core types are indirectly supported by the Perl 5 interpreter: boolean and unsigned_integer can be stored within either an integer or number; character can be stored within a string; and gmp_integer is supported by the use bigint; wrapper around the Math::BigInt::GMP module.
use bigint;
Math::BigInt::GMP
When RPerl application source code is compiled from RPerl into C++, all 7 data types are natively supported by C++ for high-speed execution.
A single group of actual numeric digit(s) or quoted string character(s) is called a "literal", such as:
-21 # integer or gmp_integer or number 'howdy' # string -23.421_12 # number 1_234_567 # unsigned_integer or integer or gmp_integer or number 1_234_567_890_123_456_789_012_345_678_901_234_567_890_123_456_789_012_345 # gmp_integer 'One million, two-hundred-thirty-four thousand, five-hundred-sixty-seven' # string '1' # character or string 'a' # character or string "\n" # newline character or string q{} # empty character or string 0 # boolean or unsigned_integer or integer or gmp_integer or number
RPerl provides 5 numeric data types:
a boolean logic value, either 0 or 1
a positive whole number value, greater than or equal to 0
a whole number value, either negative, 0, or positive
a possibly-very-large whole number value, either negative, 0, or positive; may safely exceed your computer's data type limits
a floating-point decimal number value, either negative, 0, or positive
Perl 5 provides several built-in operators designed for use with numeric data, which can be organized into 6 general categories:
Arithmetic
Trigonometry
Comparison
Logic
Bitwise
Miscellaneous
Each operator in Perl 5 (and thus RPerl) is assigned 4 important characteristics: "arity" (a number), "fixity" (a placement location), "precedence" (a number) and "associativity" (a chirality or "handedness"). Operators of unary arity accept exactly 1 input operand, binary operators accept exactly 2 operands, etc. Prefix operators appear before their respective operands, postfix appear after, infix appear between, and closed operators appear both before and after their operands. Operators with a lower numeric precedence are executed before operators with a higher precedence; in the absence of parentheses, multiplication executes before addition because multiplication has a lower precedence number. Operators with equal precedence number are grouped by (and executed in order of) associativity; in the absence of parentheses, multiple subtraction operators will execute from left to right because subtraction is left-associative, whereas multiple exponent operators will execute from right to left because exponentiation is right-associative. For more information, see the Appendix:
B.3: Syntax Arity, Fixity, Precedence, Associativity
Beyond the built-in math operators in Perl 5, more advanced operators and functions are available via the MathPerl software suite, which is (perhaps unsurprisingly) optimized using the RPerl compiler.
MathPerl on CPAN
The most memory-efficient numeric literal is boolean, which represents a single "bit" (binary digit) of information. A boolean literal may only give the values of exactly 0 or 1.
0 # boolean 1 # boolean -1 # not a boolean 1.5 # not a boolean -1.5 # not a boolean
The second most efficient numeric literal is unsigned_integer, which represents a single whole (non-decimal) number greater than or equal to 0. An unsigned_integer literal may describe any positive whole number, within the data size limits of the data types supported by your operating system software and computer hardware. An unsigned_integer may not describe a negative number or a non-whole number.
23 # unsigned_integer 0 # unsigned_integer 42_230 # unsigned_integer -23 # not an unsigned_integer 42.1 # not an unsigned_integer 999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999 # bad unsigned_integer, outside data type limits
The third most efficient numeric literal is integer, which represents a single whole (non-decimal) number. An integer literal may describe any positive or negative whole number, within your operating system and hardware data type limits.
-23 # integer 0 # integer 42_230 # integer 42.1 # not an integer -999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999 # bad integer, outside data type limits
The GNU Multi-Precision (GMP) software library is utilized to provide the gmp_integer numeric literal, representing a single whole (non-decimal) number which may safely exceed the data type limits of your operating system and hardware. A gmp_integer literal may describe any positive or negative whole number, within the limits of the memory (real or virtual) available to your RPerl code.
-23 # gmp_integer 0 # gmp_integer 42_230 # gmp_integer 42.1 # not a gmp_integer -999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999 # gmp_integer
The number numeric literal represents a single floating-point (decimal) number, and may express any real number within your computer's data type limits.
-23.42 # number 0.000_001 # number 42.23 # number 42 # number -4_123.456_789_123_456_789_123_456_789_123_456_789_123_456_789_123_456_789_123_456 # bad number, outside data type limits
For unsigned_integer, integer, gmp_integer, and number literals, an "underscore" _ character must be inserted after every third digit away from the decimal point, where the underscore is used in a similar manner as a comma when writing long numbers by hand.
_
1_234_567 # integer, same as "1,234,567" in American notation -32_123 # integer, same as "-32,123" in American notation -32123 # bad integer, missing underscore 1.234_567 # number, same as "1.234567" in American notation -32_123.456_789_01 # number, same as "-32,123.45678901" in American notation -32_123.456_78901 # bad number, missing underscore
For unsigned_integer, integer, gmp_integer, and number literals, an optional + plus sign may be prepended to explicitly indicate a numeric literal is positive (greater than zero).
+
1 # positive one +1 # also positive one
When only positive numeric literals are used in one area of code, omit positive signs.
When both positive and negative literals are used in one code area, use signs for all applicable literals.
+23 # good integer, but not aligned with other un-signed literal below +55.6 # good number, but not aligned with other un-signed literal below 42 23 # best integer for all positive literals, aligned 55.6 # best number for all positive literals, aligned 42 23 # good integer, but not aligned with other signed literals below 55.6 # good number, but not aligned with other signed literals below -21 -66.5 +23 # best integer for mixed-sign literals, aligned +55.6 # best number for mixed-sign literals, aligned -21 -66.5
For unsigned_integer, integer, and number literals, very large or very small numbers may be approximated using "scientific notation", where each number is normalized to have exactly one digit to the left of the decimal point, then a lower-case e character and an appropriate integer power-of-ten is appended to the resulting normalized floating-point number. The e character stands for "exponent", as in "exponent of ten", and the Perl style of scientific notation is sometimes more accurately referred to as "scientific e notation".
e
As with normal integers, negative exponents must be prefixed with a - minus sign and positive exponents may be optionally prefixed with a + plus sign.
-
1_234_567_000 # good integer 1.234_567_000e09 # good number, same as "1_234_567_000" in scientific notation 0.001_234_567_000 # good number 1.234_567_000e-03 # good number, same as "0.001_234_567_000" in scientific notation -0.000_000_000_000_000_000_000_001_234_567 # bad number, outside data type limits -1.234_567e-24 # good number, same as "-0.000_000_000_000_000_000_000_001_234_567" in scientific notation
Use 2 digits to represent all exponents.
When only positive exponents are used, omit exponent signs.
When both positive and negative exponents are used, use signs for all exponents.
1_234_567_000 # good integer 1.234_567_000e9 # good number, but does not align with two-digit exponents below 1.234_567_000e09 # best number for all-positive exponents, aligned with two-digit exponent below 1.234_567_000e19 1.234_567_000e+09 # good number, but not aligned with positive exponents below 1.234_567_000e09 # best number for all-positive exponents, aligned with positive exponent below 1.234_567_000e19 1.234_567_000e09 # good number, but not aligned with signed exponents below 1.234_567_000e+09 # best number for mixed-sign exponents, aligned with signed exponent below 1.234_567_000e-09 +1.537_969_711_485_091_65e+21 # best number for mixed-sign exponents, accuracy may be reduced on computers with lower data type limits -2.591_931_460_998_796_41e+01 # best number for mixed-sign exponents, accuracy may be reduced on computers with lower data type limits +1.792_587_729_503_711_81e-01 # best number for mixed-sign exponents, accuracy may be reduced on computers with lower data type limits +2.680_677_724_903_893_22e-03 # best number for mixed-sign exponents, accuracy may be reduced on computers with lower data type limits +1.628_241_700_382_422_95e-03 # best number for mixed-sign exponents, accuracy may be reduced on computers with lower data type limits -9.515_922_545_197_158_70e-15 # best number for mixed-sign exponents, accuracy may be reduced on computers with lower data type limits
Name
Symbol
Arity
Fixity
Precedence
Associativity
Supported
Absolute Value
abs
Unary
Prefix
01
Left
Not Yet
Exponent AKA Power
**
Binary
Infix
04
Right
Yes
Negative with Parentheses
-( )
Closed
05
Multiply
*
07
Divide
/
Modulo AKA Modulus
Add
08
Subtract
Logarithm
log
10
Non
Square Root
sqrt
If operand is 0 or positive (greater than 0), return unchanged;
If operand is negative (less than 0), return positive number with equal magnitude (distance from 0)
abs 0 # 0 abs 1 # 1 abs -1 # 1 abs 2_112.23 # 2_112.23 abs -2_112.23 # 2_112.23
Raise first operand to the power of second operand, return result
0 ** 0 # 1 0 ** 1 # 0 0 ** 2 # 0 0 ** 3 # 0 1 ** 0 # 1 1 ** 1 # 1 1 ** 2 # 1 1 ** 3 # 1 2 ** 0 # 1 2 ** 1 # 2 2 ** 2 # 4 2 ** 3 # 8 (-1) ** 0 # 1 (-1) ** 1 # -1 (-1) ** 2 # 1 (-1) ** 3 # -1 (-2) ** 0 # 1 (-2) ** 1 # -2 (-2) ** 2 # 4 (-2) ** 3 # -8 0 ** -1 # inf 0 ** -2 # inf 0 ** -3 # inf 1 ** -1 # 1 1 ** -2 # 1 1 ** -3 # 1 2 ** -1 # 0.5 2 ** -2 # 0.25 2 ** -3 # 0.125 (-1) ** -1 # -1 (-1) ** -2 # 1 (-1) ** -3 # -1 (-2) ** -1 # -0.5 (-2) ** -2 # 0.25 (-2) ** -3 # -0.125
START HERE: add plain-English descriptions and examples to numeric operator sections below START HERE: add plain-English descriptions and examples to numeric operator sections below START HERE: add plain-English descriptions and examples to numeric operator sections below
Arctangent-Divide
atan2
Sine
sin
Cosine
cos
Less-Than
<
11
Greater-Than
>
Less-Than-Or-Equal
<=
Greater-Than-Or-Equal
>=
Equal
==
Not-Equal
!=
Logical Negation
Logical And
&&
15
Logical Or
||
16
not
22
and
or
24
Logical Xor
xor
Bitwise Shift Left
<<
09
Bitwise Shift Right
>>
Bitwise And
&
13
Bitwise Or
|
14
Bitwise Xor
^
Integer Part
int
Random Number
rand
Nullary or Unary
Random Seed
srand
RPerl provides 2 text data types:
a single text character; either a letter, number, or special character
one or more text characters; any combination of letters, numbers, and special characters
RPerl provides 3 delimiters for enclosing text data:
'single quotes'
"double quotes"
q{q quotes}
Perl 5 provides several built-in operators designed for use with text data, which can be organized into 7 general categories:
Editing
Case
Search
Formatting
Base
The most memory-efficient text literal is character, which represents exactly zero or one character of information. A character may express the value of any single numeric digit (0, 1, 2, ..., 8, 9); letter (a, b, c, ..., y, z, A, B, C, ..., Y, Z ); or special ASCII character (!, #, *, +, etc). If the character literal has length zero, it is called the "empty character" and contains no data.
'' # not a character, use q{} for empty character '0' # character 'h' # character '+' # character '\n' # not a character, too many characters, use "\n" for newline character '-1' # not a character, too many characters 'howdy23!' # not a character, too many characters "" # not a character, use q{} for empty character "0" # character "h" # character "+" # character "\n" # character, newline "-1" # not a character, too many characters & invalid use of double quotes "howdy23!" # not a character, too many characters & invalid use of double quotes q{} # character, empty q{0} # character q{h} # character q{+} # character q{\n} # not a character, too many characters, use "\n" for newline character q{-1} # not a character, too many characters q{howdy23!} # not a character, too many characters
Any text data more than 1 character in length must be represented by a string literal, which is comprised of any combination of valid character literal characters (numeric digits, letters, and special ASCII characters). Like the empty character, if a string literal has length zero then it is called the "empty string" and contains no data.
'' # not a string, use q{} for empty string '0' # string 'h' # string '+' # string '\n' # string, not a newline, use "\n" for string containing newline '-1' # string 'howdy23!' # string "" # not a string, use q{} for empty string "0" # not a string, invalid use of double quotes, must contain newline or tab character(s) "h" # not a string, invalid use of double quotes, must contain newline or tab character(s) "+" # not a string, invalid use of double quotes, must contain newline or tab character(s) "\n" # string, contains only newline character "-1" # not a string, invalid use of double quotes, must contain newline or tab character(s) "howdy23!" # not a string, invalid use of double quotes, must contain newline or tab character(s) q{} # empty string q{0} # string q{h} # string q{+} # string q{\n} # string, not a newline, use "\n" for string containing newline q{-1} # string q{howdy23!} # string
Text literals enclosed in single quotes are the simplest and most common case in RPerl.
Single-quoted text literals are not "interpolated", which means the literal's data contents are not changed by Perl or RPerl in any way. Because single quotes do not activate string interpolation, the literal '\n' is not a newline character; instead, it is simply two normal characters, a backslash followed by a lowercase letter n. Do not use single quotes to represent a newline or tab character, use "\n" or "\t" instead.
'\n'
"\t"
Do not use single quotes to represent an empty character or empty string, use q{} instead.
q{}
A single-quoted text literal begins and ends with the single quote character, therefore it cannot logically contain a single quote character as part of the literal data itself. (RPerl does not support backslash-escaped single quote characters within a single-quoted string like normal Perl does, as this can be considered a simple form of string interpolation.)
Single-quoted text literals must not contain:
' (single quote character)
\ (single backslash as final character)
\
\\\ (odd number of backslashes as final characters)
\\\
no characters
Single-quoted text literals may contain:
" (double quote character)
"
} (right curly brace character)
}
\ (single backslash as non-final character)
\\\ (odd number of backslashes as non-final characters)
any other characters
Text literals enclosed in double quotes are fully interpolated in normal Perl, and are only used for trivial interpolation of strings containing the special "\n" newline or "\t" tab characters in RPerl. All double-quoted strings must contain at least one newline or tab character.
String interpolation in normal Perl is triggered by finding either the $ dollar sign or @ "at sign" characters inside of a double-quoted string literal. Because RPerl does not support string interpolation, double-quoted string literals must not contain the $ or @ characters.
String interpolation is also triggered by finding a \ backslash character followed by one or more special "escape characters", which are mostly comprised of otherwise-normal characters such as numbers and lowercase letters. When a backslash is followed by valid escape characters, it is called an "escape sequence". Each valid escape sequence actually counts as only one character, even though it is represented by 2 or more typed characters, so a single escape sequence may be utilized as either a text literal or a string text literal. The special \n newline and \t tab characters are themselves escape sequences, and are the only allowed escape sequences in RPerl. (Thus, "\n" and "\t" may both be utilized as either a or string text literal in RPerl.) Double-quoted string literals must not contain any backslash characters, other than those used in newline and tab escape sequences.
\n
\t
Double-quoted text literals must not contain:
$ (dollar sign character)
@ (at sign character)
\ (extra backslash character, other than \n or \t)
Double-quoted text literals must contain 1 or more:
\n (newline character)
\t (tab character)
Double-quoted text literals may contain:
Use double-quoted string literals to contain newline and tab characters only, not other normal characters.
To represent a mixture of normal characters with newline and/or tab characters, enclose the normal characters in single quotes, enclose the newline and tab characters in double quotes, and use the . dot "string concatenation" operator to append one string literal to the other. (Please see "Section 2.2.6: Editing Operators" for more info about string concatenation.)
Text literals enclosed in q{} "q quotes" begin with the lowercase q followed by { left curly brace characters, and end with the } right curly brace character. You must use q quotes to represent q{} empty text literals containing no characters.
{
Normal Perl support q-quoted string literals using delimiters other than curly braces, as well as "qq quotes" which provide string interpolation in the same way as double-quoted strings. RPerl's existing string quoting mechanisms cover all use cases, so RPerl does not support the additional qq quotes or non-curly-brace q quotes, because TDNNTBMTOWTDI.
[ INSERT BACKSLASHES, QUOTES ]
[ INSERT BACKSLASHES FOR SINGLE AND DOUBLE QUOTE SECTIONS ABOVE]
q-quoted text literals must not contain:
q-quoted text literals may contain:
%token OP01_NAMED_SCOLON = /(substr;)/ %token OP07_STRING_REPEAT = /(x)\s/ # precedence 07 infix: string repetition 'x' %token OP08_STRING_CAT = /(\.)/ # precedence 08 infix: string concatenate '.' %token OP10_NAMED_UNARY_SCOLON = /(length;)/
%token OP01_NAMED_SCOLON = /(fc;)/ %token OP10_NAMED_UNARY_SCOLON = /(lc;|lcfirst;|uc;|ucfirst;)/
%token OP01_NAMED_SCOLON = /(cmp;)/ %token OP11_COMPARE_LT_GT = /(le|ge|lt|gt)\s/ # precedence 11 infix: string comparison less or equal 'le', greater or equal 'ge', less than 'lt', greater than 'gt' %token OP12_COMPARE_EQ_NE = /(eq|ne)\s/ # precedence 12 infix: comparison string equal 'eq', string not equal 'ne'
%token OP01_NAMED_SCOLON = /(index;|rindex;)/
%token OP01_NAMED_SCOLON = /(sprintf;)/ %token OP10_NAMED_UNARY_SCOLON = /(quotemeta;)/
%token OP01_NAMED_SCOLON = /(chr;)/ %token OP10_NAMED_UNARY_SCOLON = /(hex;|oct;|ord;)/
%token OP01_NAMED_SCOLON = /(crypt;)/
An RPerl "expression" is any general-purpose language component which either returns a value or is a literal value itself.
An RPerl "statement" is any general-purpose language component which performs some action(s).
An RPerl "named operator" is any of the 220+ Perl named operators, although RPerl only supports the low-magic forms of each operator.
An RPerl "operation" is the equivalent of a single sentence in human language, and may be either an expression followed by a ; semicolon punctuation character, or a named operator followed by a semicolon, or a statement.
;
The = equal sign is the assignment operator, used to set the variable on its left to store the value of the expression on its right.
=
Perl's my keyword is used to declare a new variable, and optionally initialize it to a starting value when combined with the = assignment operator.
my
Normal Perl does not support specific data types, so in Perl one variable named $foo may be initialized with a numeric value, then the same $foo variable may be changed to hold a string value without any warning or error.
my $foo = 23; $foo = 'twenty-three'; # just fine in normal Perl
On the other hand, RPerl requires the use of data types for each and every variable.
my $foo = 23; # error in RPerl, all modes my number $foo = 23; $foo = 'twenty-three'; # error in RPerl, compiled (non-test) modes, assigning string literal to number variable my number $foo = 23; $foo = 42; # just fine in RPerl
Normal Perl provides a special literal value undef, along with a built-in function named defined which is used to test if a variable contains the special undef value. RPerl does not support undef or defined, because C++ does not fully support the concept of undefined values, and RPerl's high-speed components are written using C++.
undef
defined
Data types make your code much more readable and much, much faster. Learn to love data types. Now.
The most efficient data type is boolean, which stores a single "bit" (binary digit) of information. A boolean may only hold the values of exactly 0 or 1.
my boolean $foo = 0; # fine my boolean $bar = 1; # fine my boolean $baz = -1.5; # error in RPerl, compiled (non-test) modes
The next most efficient data type is integer, which stores a single whole (non-decimal) number. An integer may hold any positive or negative whole number, within the data size limits of your operating system and computer hardware.
my integer $foo = -23; # fine my integer $bar = 0; # fine my integer $baz = 42_230; # fine my integer $bax = 42.1; # error in RPerl, compiled (non-test) modes my integer $quux = -999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999_999; # likely error or data corruption, outside limits
The number data type stores a single floating-point (decimal) number, and may hold any real number within your computer's limits.
my number $foo = -23.42; # fine my number $bar = 0.000_001; # fine my number $baz = 42.23; # fine my number $bax = 42; # fine my number $quux = -4_123.456_789_123_456_789_123_456_789_123_456_789_123_456_789_123_456_789_123_456; # likely error or data loss, outside limits
To convert from one data type to another, we use the RPerl type conversion subroutines, shown below for numeric types only:
boolean_to_integer()
boolean_to_number()
integer_to_boolean()
integer_to_number()
number_to_boolean()
number_to_integer()
my integer $foo = 23; my number $bar = $foo; # error in RPerl, compiled (non-test) modes, type mismatch my integer $foo = 23; my number $bar = integer_to_number($foo); # fine, $bar is now 23.0 my number $foo = 23.42; my integer $bar = $foo; # error in RPerl, compiled (non-test) modes, type mismatch my number $foo = 23.42; my integer $bar = number_to_integer($foo); # fine, $bar is now 23
START HERE START HERE START HERE
The "scope" of a variable is either local using the my keyword, or global using the our keyword. Local variables are only usable within their own enclosing code block such as a conditional (section xxx), loop (xxx), or subroutine (chapter 4).
our
and global variables are usable within any code block accessible by the Perl interpreter or (RPerl compiler).
Except for certain special circumstances, all variables in RPerl are locally-scoped using my.
OP19_VARIABLE_ASSIGN_BY = /(\+=|-=|\*=|\/=)/ # precedence 19 infix: add assign '+=', subtract assign '-=', multiply assign '*=', divide assign '/=' %token OP19_VARIABLE_ASSIGN_BY = /(\.=)/ # precedence 19 infix: string concatenation assign '.='
Pre-Increment
++
03
Post-Increment
Postfix
Pre-Decrement
--
Post-Decrement
Add 1 to operand, then return incremented value
# NEED ADD EXAMPLES ++$i # 0
if
while
OP08_MATH_ADD_SUB = /(sse_add|sse_sub)/ # precedence 08 infix: SSE add 'sse_add', SSE subtract 'sse_sub' OP07_MATH_MULT_DIV_MOD = /(sse_mul|sse_div)/ # precedence 07 infix: SSE multiply 'sse_mul', SSE divide 'sse_div'
%token OP10_NAMED_UNARY_SCOLON = /(-A;|-B;|-C;|-M;|-O;|-R;|-S;|-T;|-W;|-X;|-b;|-c;|-d;|-e;|-f;|-g;|-k;|-l;|-o;|-p;|-r;|-s;|-t;|-u;|-w;|-x;|-z;)/
# REGEX %token OP01_NAMED_SCOLON = /(m;|pos;|qr;|s;|study;|tr;|y;
# FORMAT %token OP01_NAMED_SCOLON = /(format;|formline;|write;
# PACK / UNPACK %token OP01_NAMED_SCOLON = /(pack;|unpack;|vec;|
# UNSORTED %token OP01_NAMED_SCOLON = /(accept;|bind;|binmode;|bless;|break;|chmod;|chomp;|chop;|chown;|closedir;|connect;|continue;|dbmclose; |dbmopen;|default;|dump;|each;|endgrent;|endhostent;|endnetent;|endprotoent;|endpwent;|endservent;|eof;|evalbytes;|exec;|exp;|fcntl;|fileno;|flock;|fork; |getc;|getgrent;|getgrgid;|getgrnam;|gethostbyaddr;|gethostent;|getlogin;|getnetbyaddr;|getnetent;|getpeername;|getppid;|getpriority; |getprotobynumber;|getprotoent;|getpwent;|getpwnam;|getpwuid;|getservbyname;|getservbyport;|getservent;|getsockname;|getsockopt;|given;|grep;|ioctl;|join; |keys;|kill;|link;|listen;|local;|map;|mkdir;|msgctl;|msgget;|msgrcv;|msgsnd;|opendir;|pipe;|pop;|prototype;|push;|qx;|read;|readdir; |readline;|readpipe;|recv;|rename;|reset;|reverse;|rewinddir;|say;|seek;|seekdir;|select;|semctl;|semget;|semop;|send;|setgrent;|sethostent;|setnetent; |setpgrp;|setpriority;|setprotoent;|setpwent;|setservent;|setsockopt;|shift;|shmctl;|shmget;|shmread;|shmwrite;|shutdown;|socket;|socketpair;|sort;|splice;|split; |state;|symlink;|syscall;|sysopen;|sysread;|sysseek;|system;|syswrite;|tell;|telldir;|tie;|tied;|time;|times;|truncate;|unless;|unlink; |unshift;|untie;|until;|utime;|values;|vec;|wait;|waitpid;|wantarray;|warn;|when;)/
%token OP10_NAMED_UNARY_SCOLON = /(alarm;|caller;|chdir;|chroot;|defined;|delete;|do;|eval;|exists;|gethostbyname;|getnetbyname;|getpgrp;|getprotobyname;|glob; |gmtime;|goto;|localtime;|lock;|lstat;|readlink;|ref;|require;|rmdir;|scalar;|sleep;|stat;|umask;)/
This exercise is commonly used as the first task for new programmers, or for programmers who are learning a new language.
The goal of this exercise is to become familiar with the boilerplate (often-repeated template) RPerl HEADER and CRITICS code sections, as well as the basic print command.
HEADER
CRITICS
The first line, starting with #! and called a "shebang", tells the operating system to run this program using Perl.
The 4 lines in the HEADER section tell Perl to run this program using RPerl, and the $VERSION number may be incremented for each update to this file.
$VERSION
The line in the CRITICS section, starting with ##, tells RPerl to allow hard-coded numeric values (not used in this program), as well as the print operator.
##
The last line, in the OPERATIONS section, calls the print operator to simply display the text "Hello, world!" followed by a newline character, combined into a single string value by the . (single dot) string concatenation operator.
OPERATIONS
All other lines beginning with # are comments and, along with blank lines, may be safely ignored or removed.
#!/usr/bin/perl # Learning RPerl, Chapter 1, Exercise 1 # Print "Hello, world!"; the classic first program for new programmers # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ OPERATIONS ]]] print 'Hello, world!' . "\n";
Example execution and output:
$ rperl -t LearningRPerl/Chapter1/exercise_1-hello_world.pl Hello, world!
The goal of this exercise is to become familiar with the rperl command.
Example execution and output for 2a and 2b:
$ rperl -? Usage: rperl [OPTIONS] input_program_0.pl [input_program_1.pl input_program_2.pl ...] rperl [OPTIONS] MyClassFoo.pm [MyClassBar.pm MyClassBat.pm ...] rperl [OPTIONS] input_program_0.pl MyClassFoo.pm [input_program_1.pl ... MyClassBar.pm ...] Options: --help _OR_ -h _OR_ -? Print a brief help message for command-line usage. --version _OR_ -v --vversion _OR_ -vv Print version number and copyright information. Repeat as 'vv' for more technical information, similar to `perl -V` configuration summary option. Lowercase 'v' not to be confused with uppercase 'V' in 'Verbose' option below. --dependencies _OR_ -d --nodependencies _OR_ -nod Follow and compile dependencies, or not. Enabled by default, equivalent to '--mode dependencies=ON' option. Lowercase 'd' not to be confused with uppercase 'D' in 'Debug' option below. WARNING: Disabling dependencies will likely cause errors or undefined behavior. --infile=MyFile.pm _OR_ -i=MyFile.pm Specify input file, may be repeated for multiple input files. Option prefix '--infile' may be entirely omitted. Option prefix MUST be omitted to specify wildcard for multiple input files. --outfile=MyFile _OR_ -o=MyFile Specify output file prefix, may be repeated for multiple output files. RPerl *.pm input file with PERL ops will create MyFile.pmc output file. RPerl *.pl input file with PERL ops will create my_file (or my_file.exe) & my_file.pmc output files. RPerl *.pm input file with CPP ops will create MyFile.pmc, MyFile.cpp, & MyFile.h output files. RPerl *.pl input file with CPP ops will create myfile (or myfile.exe on Windows), MyFile.pmc, MyFile.cpp, & MyFile.h output files. Option may be entirely omitted, 'MyFile.*' input file will default to 'MyFile.*' out. --mode ops=PERL _OR_ -m ops=PERL --mode ops=CPP _OR_ -m ops=CPP Specify operations mode, CPP by default. If set to PERL, generate Perl operations in the source code output file(s). If set to CPP, generate C++ operations in the source code output file(s). PERL ops mode forces PERL types mode & PARSE or GENERATE compile mode; PERLOPS_PERLTYPES is test mode, does not actually compile. --mode types=PERL _OR_ -m types=PERL --mode types=CPP _OR_ -m types=CPP --mode types=DUAL _OR_ -m types=DUAL Specify data types mode, CPP by default. If set to PERL, generate Perl data types in the source code output file(s). If set to CPP, generate C++ data types in the source code output file(s). If set to DUAL, generate both Perl and C++ data types in the source code output file(s). DUAL mode allows generate-once-compile-many types, selected by '#define __FOO__TYPES' in lib/rperltypes_mode.h or `gcc -D__FOO__TYPES` manual subcompile option. --mode check=OFF _OR_ -m check=OFF --mode check=ON _OR_ -m check=ON --mode check=TRACE _OR_ -m check=TRACE Specify data type checking mode, TRACE by default. If set to OFF, do not perform dynamic type checking, only built-in C++ static type checking. If set to ON, perform dynamic type checking in addition to built-in C++ static type checking. If set to TRACE, perform dynamic type checking in addition to built-in C++ static type checking, with subroutine-and-variable trace information. --mode dependencies=OFF _OR_ -m dependencies=OFF --mode dependencies=ON _OR_ -m dependencies=ON Specify dependencies mode, ON by default. If set to OFF, do not search for or compile dependencies. If set to ON, recursively search for dependencies and subdependencies, include as additional input file(s). --mode uncompile=OFF _OR_ -m uncompile=OFF --mode uncompile=SOURCE _OR_ -m uncompile=SOURCE --mode uncompile=BINARY _OR_ -m uncompile=BINARY --mode uncompile=INLINE _OR_ -m uncompile=INLINE --mode uncompile=SOURCE_BINARY _OR_ -m uncompile=SOURCE_BINARY --mode uncompile=SOURCE_BINARY_INLINE _OR_ -m uncompile=SOURCE_BINARY_INLINE Specify uncompile mode, OFF by default. If set to SOURCE, delete all generated C++ output source code (not subcompiled) files: *.cpp, *.h, *.pmc If set to BINARY, delete all generated C++ output binary (subcompiled) files: *.o, *.a, *.so, *.exe, non-suffixed executables If set to INLINE, delete all generated C++ output Inline::CPP files: _Inline/ directory If set to SOURCE_BINARY, delete both SOURCE and BINARY files. If set to SOURCE_BINARY_INLINE, delete SOURCE, BINARY, and INLINE files. For *.pm Perl module input files, BINARY and INLINE are equivalent. --mode compile=PARSE _OR_ -m compile=PARSE --mode compile=GENERATE _OR_ -m compile=GENERATE --mode compile=SUBCOMPILE _OR_ -m compile=SUBCOMPILE Specify compile mode, SUBCOMPILE by default. If set to PARSE, begin with RPerl input source code file(s), and end with RPerl abstract syntax tree output data structure. If set to GENERATE, begin with RPerl input source code file(s), and end with RPerl and/or C++ output source code file(s). If set to SUBCOMPILE, begin with RPerl input source code file(s), and end with C++ output binary file(s). --mode subcompile=OFF _OR_ -m subcompile=OFF --mode subcompile=ASSEMBLE _OR_ -m subcompile=ASSEMBLE --mode subcompile=ARCHIVE _OR_ -m subcompile=ARCHIVE --mode subcompile=SHARED _OR_ -m subcompile=SHARED --mode subcompile=STATIC _OR_ -m subcompile=STATIC --mode subcompile=DYNAMIC _OR_ -m subcompile=DYNAMIC Specify subcompile mode, DYNAMIC by default. If set to ASSEMBLE, generate *.o object binary output file(s). If set to ARCHIVE, generate *.a object archive binary output file(s). If set to SHARED, generate *.so shared object binary output file(s). If set to STATIC, generate statically-linked *.exe or non-suffixed executable binary output file(s). If set to DYNAMIC, generate dynamically-linked *.exe or non-suffixed executable binary output file(s). --mode CXX=/path/to/compiler _OR_ -m CXX=/path/to/compiler Specify path to C++ compiler for use in subcompile modes, 'g++' by default. --mode execute=OFF _OR_ -m execute=OFF --mode execute=ON _OR_ -m execute=ON Specify execute mode, ON by default. If set to OFF, do not load or run any user-supplied program(s). If set to ON with one *.pl Perl program input file, load and run the program. If set to ON with more than one *.pl Perl program input file, do not load or run any programs. --mode label=OFF _OR_ -m label=OFF --mode label=ON _OR_ -m label=ON Specify source section label mode, ON by default. If set to OFF, generate minimal output source code, may save disk space. If set to ON, generate some informative labels in output source code, may be more human-readable. --uncompile _OR_ -u --nouncompile _OR_ -nou --uuncompile _OR_ -uu --nouuncompile _OR_ -nouu --uuuncompile _OR_ -uuu --nouuncompile _OR_ -nouuu Uncompile (delete C++ source code and/or binary output files), or not. Repeat as 'uu' and 'uuu' for more thorough file removal. Do not confuse uncompile with decompile (recreate RPerl source code from C++ source code or binary output files), which does not currently exist. '-u' equivalent to '--mode uncompile=SOURCE --mode compile=OFF --mode execute=OFF' options. '-uu' equivalent to '--mode uncompile=SOURCE_BINARY --mode compile=OFF --mode execute=OFF' options. '-uuu' equivalent to '--mode uncompile=SOURCE_BINARY_INLINE --mode compile=OFF --mode execute=OFF' options. Disabled by default. --compile _OR_ -c --nocompile _OR_ -noc Generate & subcompile C++ source code, or not. Enabled by default, equivalent to '--mode compile=SUBCOMPILE' option. --execute _OR_ -e --noexecute _OR_ -noe Run input code after optional compile, or not. Enabled by default for *.pl program input files, always disabled for *.pm module input files or multiple input files. Equivalent to '--mode execute=ON' option. --Verbose _OR_ -V --noVerbose _OR_ -noV Include additional user information in output, or not. If enabled, equivalent to `export RPERL_VERBOSE=1` shell command. Disabled by default. Uppercase 'V' not to be confused with lowercase 'v' in 'version' option above. --Debug _OR_ -D --noDebug _OR_ -noD Include system diagnostic information in output, or not. If enabled, equivalent to `export RPERL_DEBUG=1` shell command. Disabled by default. Uppercase 'D' not to be confused with lowercase 'd' in 'dependencies' option above. --Warnings _OR_ -W --noWarnings _OR_ -noW Include system warnings in output, or not. Enabled by default, equivalent to `export RPERL_WARNINGS=0` shell command. --test _OR_ -t Test mode: Perl ops, Perl types, Parse & Generate (no Save or Compile) If enabled, equivalent to '--mode ops=PERL --mode types=PERL --mode compile=GENERATE' options. Disabled by default. --assemble Assemble subcompile mode, output *.o object file(s). If enabled, equivalent to '--mode subcompile=ASSEMBLE' option or `gcc -c` manual subcompile option. Disabled by default. --archive Archive subcompile mode, output *.a object archive file(s). If enabled, equivalent to '--mode subcompile=ARCHIVE' option or `gcc -c` followed by `ar` manual subcompile command. Disabled by default. --shared Archive subcompile mode, output *.so shared object file(s). If enabled, equivalent to '--mode subcompile=SHARED' option or `gcc -shared` manual subcompile command. Disabled by default. --static --nostatic Static subcompile mode, output *.exe or non-suffixed statically-linked executable file(s). If disabled, equivalent to '--mode subcompile=DYNAMIC' option or `gcc` manual subcompile command. If enabled, equivalent to '--mode subcompile=STATIC' option or `gcc -static` manual subcompile command. Disabled by default. --CXX=/path/to/compiler Specify path to C++ compiler, equivalent to '--mode CXX=/path/to/compiler' or 'CXX' manual Makefile option.
Example execution and output for 2c and 2d:
$ rperl -t -V LearningRPerl/Chapter1/exercise_1-hello_world.pl Verbose Flag: 1 Debug Flag: 0 Test Flag: 1 Input File: LearningRPerl/Chapter1/exercise_1-hello_world.pl Output File(s): LearningRPerl/Chapter1/exercise_1-hello_world Modes: ops => PERL, types => PERL, check => TRACE, compile => GENERATE, execute => ON, label => ON DEPENDENCIES: Follow & find all deps... 0 found. PARSE PHASE 0: Check Perl syntax... done. PARSE PHASE 1: Criticize Perl syntax... done. PARSE PHASE 2: Parse RPerl syntax... done. GENERATE: Generate RPerl syntax... done. EXECUTE: Run code... Hello, world! $ rperl -t -D LearningRPerl/Chapter1/exercise_1-hello_world.pl in rperl, have $RPerl::DEBUG = 1 in rperl, have $RPerl::VERBOSE = 0 Hello, world! $ rperl -t -V -D LearningRPerl/Chapter1/exercise_1-hello_world.pl Verbose Flag: 1 Debug Flag: 1 Test Flag: 1 in rperl, have $RPerl::DEBUG = 1 in rperl, have $RPerl::VERBOSE = 1 Input File: LearningRPerl/Chapter1/exercise_1-hello_world.pl Output File(s): LearningRPerl/Chapter1/exercise_1-hello_world Modes: ops => PERL, types => PERL, check => TRACE, compile => GENERATE, execute => ON, label => ON DEPENDENCIES: Follow & find all deps... 0 found. PARSE PHASE 0: Check Perl syntax... done. PARSE PHASE 1: Criticize Perl syntax... done. PARSE PHASE 2: Parse RPerl syntax... done. GENERATE: Generate RPerl syntax... done. EXECUTE: Run code... Hello, world!
Of the above 3 commands executed for 2c and 2d, the first includes normal output plus additional verbose output; the second includes normal output plus additional debugging output (minimal in this simple case); and the third includes normal output plus both verbose and debugging output.
The goal of this exercise is to become familiar with basic variables and arithmetic.
The shebang line, HEADER section, and first line in the CRITICS section are the same boilerplate as the previous exercise.
The second line in the CRITICS section tells RPerl to allow the $ (dollar) character, among others, to be displayed using the print operator.
The first 3 lines in the OPERATIONS section each declare a new variable; $foo and $bar each hold an integer (non-floating-point) numeric value, while $baz holds a number (floating-point) value.
The $foo and $bar variables receive their values from hard-coded numeric values being operated upon by the + (plus sign) addition and * (asterisk) multiplication operators, respectively; the $baz variable receives its value from the the $foo and $bar variables being operated upon by the / (forward slash) division operator.
The last 3 lines call the print operator to display the names of each variable; followed by each variable's respective value, converted from number to underscore-formatted string via the RPerl type conversion subroutine to_string(); followed by a newline character.
$ rperl -t LearningRPerl/Chapter1/exercise_3-foo_bar_arithmetic.pl have $foo = 33 have $bar = 1_932 have $baz = 58.545_454_545_454_5
The goal of this exercise is to become familiar with constant values.
The first line in the CRITICS section tells RPerl to allow hard-coded numeric values as well as the print operator, both of which are utilized in this program.
The second line in the CRITICS section tells RPerl to allow the use constant operation.
use constant
The line in the CONSTANTS section declares a number (floating-point) constant value named PI, automatically accessible via a subroutine named PI().
CONSTANTS
PI
PI()
The inner type variable $TYPED_PI is only used for RPerl parsing purposes; for example, if your constant is named FOO then you should declare it using the inner type variable $TYPED_FOO and you should access it by calling the subroutine FOO(), but you should never directly utilize the variable $TYPED_FOO anywhere else in your code.
$TYPED_PI
FOO
$TYPED_FOO
FOO()
The first 2 lines in the OPERATIONS section each create a new number variable, with $radius set to the hard-coded value of 12.5 and $circumference set to the well-known basic geometry formula "circumference equals 2 pi times radius".
$radius
12.5
$circumference
The last 3 lines call the print operator to display the values of PI, $radius, and $circumference, each followed by a newline character.
#!/usr/bin/perl # Learning RPerl, Chapter 2, Exercise 1 # Find the circumference of a circle with hard-coded radius of 12.5 units # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitConstantPragma ProhibitMagicNumbers) # USER DEFAULT 3: allow constants # [[[ CONSTANTS ]]] use constant PI => my number $TYPED_PI = 3.141_592_654; # [[[ OPERATIONS ]]] my number $radius = 12.5; my number $circumference = 2 * PI() * $radius; print 'Pi = ' . to_string(PI()) . "\n"; print 'Radius = ' . to_string($radius) . "\n"; print 'Circumference = 2 * Pi * Radius = 2 * ' . to_string(PI()) . ' * ' . to_string($radius) . ' = ' . to_string($circumference) . "\n";
$ rperl -t LearningRPerl/Chapter2/exercise_1-circumference_of_specific_radius.pl Pi = 3._141_592_654 Radius = 12.5 Circumference = 2 * Pi * Radius = 2 * 3._141_592_654 * 12.5 = 78.539_816_35
The goal of this exercise is to become familiar with accepting user keyboard input.
The third line in the CRITICS section tells RPerl to allow user input via the <STDIN> standard stream, a software input connection which is attached to the keyboard by default.
<STDIN>
The second line in the OPERATIONS section creates a string variable $radius_string, and assigns to it the text value typed by the user on their keyboard.
$radius_string
The third line in the OPERATIONS section creates a number variable $radius, and assigns to it the numeric value returned by calling the RPerl type conversion subroutine string_to_number() on the string variable $radius_string.
string_to_number()
This exercise is otherwise identical to the previous exercise.
#!/usr/bin/perl # Learning RPerl, Chapter 2, Exercise 2 # Find the circumference of a circle with any radius entered by the user # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitConstantPragma ProhibitMagicNumbers) # USER DEFAULT 3: allow constants ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ CONSTANTS ]]] use constant PI => my number $TYPED_PI = 3.141_592_654; # [[[ OPERATIONS ]]] print 'Please input radius: '; my string $radius_string = <STDIN>; my number $radius = string_to_number($radius_string); my number $circumference = 2 * PI() * $radius; print "\n"; print 'Pi = ' . to_string(PI()) . "\n"; print 'Radius = ' . to_string($radius) . "\n"; print 'Circumference = 2 * Pi * Radius = 2 * ' . to_string(PI()) . ' * ' . to_string($radius) . ' = ' . to_string($circumference) . "\n";
Example execution, input, and output:
$ rperl -t LearningRPerl/Chapter2/exercise_2-circumference_of_any_radius.pl Please input radius: 2 Pi = 3.141_592_654 Radius = 2 Circumference = 2 * Pi * Radius = 2 * 3.141_592_654 * 2 = 12.566_370_616
The goal of this exercise is to become familiar with conditional statements and comparison operators.
In the OPERATIONS section, the line starting with if ($radius >= 0) denotes the beginning of a conditional statement: if the numeric value of the variable $radius is greater-than-or-equal-to 0, then the normal calculation for $circumference is used; if $radius is less than 0 (physically impossible), then a warning message is printed and $circumference is set to 0.
if ($radius >= 0)
#!/usr/bin/perl # Learning RPerl, Chapter 2, Exercise 3 # Find the circumference of a circle with any positive radius entered by the user # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitConstantPragma ProhibitMagicNumbers) # USER DEFAULT 3: allow constants ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ CONSTANTS ]]] use constant PI => my number $TYPED_PI = 3.141_592_654; # [[[ OPERATIONS ]]] print 'Please input radius: '; my string $radius_string = <STDIN>; my number $radius = string_to_number($radius_string); my number $circumference; if ($radius >= 0) { $circumference = 2 * PI() * $radius; } else { print 'Negative radius detected, defaulting to zero circumference!' . "\n"; $circumference = 0; } print "\n"; print 'Pi = ' . to_string(PI()) . "\n"; print 'Radius = ' . to_string($radius) . "\n"; print 'Circumference = 2 * Pi * Radius = 2 * ' . to_string(PI()) . ' * ' . to_string($radius) . ' = ' . to_string($circumference) . "\n";
$ rperl -t LearningRPerl/Chapter2/exercise_3-circumference_of_any_positive_radius.pl Please input radius: -2 Negative radius detected, defaulting to zero circumference! Pi = 3._141_592_654 Radius = -2 Circumference = 2 * Pi * Radius = 2 * 3._141_592_654 * -2 = 0
The goal of this exercise is to gain further exposure to the <STDIN> standard stream and variable multiplication.
In the OPERATIONS section, <STDIN> is accessed to collect user input for both the $multiplicator_string and $multiplicand_string variables.
$multiplicator_string
$multiplicand_string
These 2 string variables are converted from text values to numeric values by calling string_to_number(), then multiplied via the * multiplication operator, and the results displayed by calling print.
#!/usr/bin/perl # Learning RPerl, Chapter 2, Exercise 4 # Find the product of any two numbers entered by the user # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ OPERATIONS ]]] print 'Please input multiplicator: '; my string $multiplicator_string = <STDIN>; my number $multiplicator = string_to_number($multiplicator_string); print 'Please input multiplicand: '; my string $multiplicand_string = <STDIN>; my number $multiplicand = string_to_number($multiplicand_string); my number $product = $multiplicator * $multiplicand; print "\n"; print 'Product = Multiplicator * Multiplicand = ' . to_string($multiplicator) . ' * ' . to_string($multiplicand) . ' = ' . to_string($product) . "\n";
$ rperl -t LearningRPerl/Chapter2/exercise_4-product_of_any_two_numbers.pl Please input multiplicator: 2112 Please input multiplicand: 23.42 Product = Multiplicator * Multiplicand = 2_112 * 23.42 = 49_463.04
The goal of this exercise is to become familiar with the x string repeat operator.
x
In the OPERATIONS section, <STDIN> is accessed twice to collect user input for a string variable $repeat_string to be repeated, and an integer variable $repeat_integer to specify the number of repetitions.
$repeat_string
$repeat_integer
The last line contains 2 operators, print and the x string repeat operator.
Like the . (single dot) string concatenation operator, the x operator has a higher precedence than print and is thus executed first, generating a single string value comprised of the original string repeated 0 or more times, then the resulting string is displayed by calling print.
#!/usr/bin/perl # Learning RPerl, Chapter 2, Exercise 5 # Repeat any string any number of times, both values entered by the user # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ OPERATIONS ]]] print 'Please input string to be repeated: '; my string $repeat_string = <STDIN>; print 'Please input integer (whole number) times to repeat string: '; my string $repeat_integer_string = <STDIN>; my integer $repeat_integer = string_to_integer($repeat_integer_string); print "\n"; print $repeat_string x $repeat_integer;
$ rperl -t LearningRPerl/Chapter2/exercise_5-string_repeat.pl Please input string to be repeated: howdy Please input integer (whole number) times to repeat string: 3 howdy howdy howdy
The goal of this exercise is to become familiar with the while loop control structure.
In the OPERATIONS section, <STDIN> is accessed to collect user input into the string variable $n_string, then the data type conversion subroutine string_to_integer() is called to create the corresponding integer variable $n.
$n_string
string_to_integer()
$n
The if ($n < 0) conditional control structure calls die to exit with an error message if $n is not a positive value.
if ($n < 0)
die
Two additional integer variables are created: $sum is initialized to 0 and will eventually hold the final answer value; and $i is initialized to 0 for use as the iteration counter variable (AKA "iterator") inside the following while loop control structure.
$sum
$i
The while loop iteratively executes as long as the integer value of $i is less-than-or-equal-to $n. Each iteration of the while loop causes the value of $sum to be increased by $i, after which $i itself is incremented by 1, and the loop is repeated.
The last line calls print, the to_string() conversion subroutine, and the . (single dot) string concatenation operator to display the final answer.
#!/usr/bin/perl # Learning RPerl, Chapter 2, Exercise 6 # Calculate the sum of the first n integers; 1 + 2 + 3 + ... + n = ? # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ OPERATIONS ]]] print 'Please input a positive integer: '; my string $n_string = <STDIN>; my integer $n = string_to_integer($n_string); if ($n < 0) { die 'ERROR: ' . to_string($n) . ' is not positive, dying' . "\n"; } my integer $sum = 0; my integer $i = 1; while ($i <= $n) { $sum += $i; $i++; } print 'The sum of the first ' . to_string($n) . ' integers = ' . to_string($sum) . "\n";
Example executions, input, and output:
$ rperl -t LearningRPerl/Chapter2/exercise_6-sum_of_first_n_integers.pl Please input a positive integer: 100 The sum of the first 100 integers = 5_050 $ rperl -t LearningRPerl/Chapter2/exercise_6-sum_of_first_n_integers.pl Please input a positive integer: -100 ERROR: -100 is not positive, dying
The goal of this exercise is to utilize an optimization to remove the while loop from the previous exercise.
In the OPERATIONS section, user input is collected and error-checked, as before.
By utilizing an algorithm attributed to Gauss, we are able to remove the while loop, so we also remove the $i loop iterator. The $sum variable is kept the same as the previous exercise.
Two new integer variables are also created: $n_original is initialized to hold a copy of $n which will never change; and $n_odd is initialized to 0.
$n_original
$n_odd
The code block beginning with if ($n % 2) is used to detect if the user provided a value for $n which is odd (not even), this is detected by utilizing the % modulo operator to test for a remainder of dividing $n by 2. If $n is odd, then a second copy of the original value of $n is stored in $n_odd, after which $n itself is decremented by 1. This if statement allows our algorithm to accept odd as well as even user input values for $n.
if ($n % 2)
The computational kernel (most important part) is the one line of arithmetic: $sum = (($n + 1) * ($n / 2)) + $n_odd;
$sum = (($n + 1) * ($n / 2)) + $n_odd;
It is said that as a child, Gauss was punished by his teacher by being told to mentally add together all the numbers from 1 to 100. Young Gauss realized that 1 plus 100 is 101, and 2 plus 99 is also 101, as well as 3 plus 98 and so forth. All the numbers may be paired to equal 101, and there are 50 such pairs, so the final answer is 101 multiplied by 50. Thus, with a bit more thought Gauss was able to achieve the answer of 5,050 and perplex his teacher in the process.
Our arithmetic generalizes the idea of young Gauss by using $n instead of 100, and by adding $n_odd to enable support for odd values of $n. Using Gauss' own example of $n equal to 100, which is even (not odd) so $n_odd will equal 0, we can see the algorithm becomes: $sum = ((100 + 1) * (100 / 2)) + 0;
$sum = ((100 + 1) * (100 / 2)) + 0;
One more step of arithmetic simplification shows our algorithm to be the same as Gauss': $sum = 101 * 50;
$sum = 101 * 50;
The last line calls print using the value of $n_original, because the value of $n will have been decreased by 1 if $n is odd.
It is usually faster to run an algorithm without a loop than with a loop, because you are only doing 1 thing instead of many things. However, not all problems can be easily optimized by changing to a new algorithm, because it may be prohibitively complex or there may only be 1 known algorithm. In this case, Gauss' algorithm should be faster than the while loop algorithm, expecially for very large values of $n. It may be possible to further optimize this exercise by utilizing bitwise operators to replace the modulo and division operators.
#!/usr/bin/perl # Learning RPerl, Chapter 2, Exercise 7 # Calculate the sum of the first n integers, without using a loop; 1 + 2 + 3 + ... + n = ? # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ OPERATIONS ]]] print 'Please input a positive integer: '; my string $n_string = <STDIN>; my integer $n = string_to_integer($n_string); if ($n < 0) { die 'ERROR: ' . to_string($n) . ' is not positive, dying' . "\n"; } my integer $sum = 0; my integer $n_original = $n; my integer $n_odd = 0; if ($n % 2) { $n_odd = $n; $n--; } $sum = (($n + 1) * ($n / 2)) + $n_odd; print 'The sum of the first ' . to_string($n_original) . ' integers = ' . to_string($sum) . "\n";
$ rperl -t LearningRPerl/Chapter2/exercise_7-sum_of_first_n_integers_no_loop.pl Please input a positive integer: 100 The sum of the first 100 integers = 5_050 $ rperl -t LearningRPerl/Chapter2/exercise_7-sum_of_first_n_integers_no_loop.pl Please input a positive integer: -100 ERROR: -100 is not positive, dying
The goal of this exercise is to become familiar with the foreach loop control structure, arrays, and array operators; and to become further familiarized with the while loop.
foreach
The first line in the OPERATIONS section declares a new variable $input_strings of type string_arrayref, which is capable of storing 0 or more individual string values, and $input_strings is then initialized to contain the empty set [].
$input_strings
string_arrayref
[]
The line starting with while (my string $input_string = <STDIN>) denotes the beginning of an iterative (repeating) loop statement, which continues to accept and store user input until CTRL-D is pressed to indicate the EOF (end-of-file) condition, also known as EOT (end-of-transmission).
while (my string $input_string = <STDIN>)
A new copy of the variable $input_string is created and assigned the value of collected user input by calling <STDIN> at the start of each loop iteration; the my operator is evaluated as a true condition and the loop repeats, until CTRL-D is received and the my operator returns a false condition.
$input_string
Inside the body of the while loop is 1 line calling the push operator, which appends the current iteration's value of $input_string onto the list of strings contained in $input_strings.
push
The @{...} (at-sign-curly-braces) is the array dereference operator, which exists because in Perl it is still sometimes necessary to directly access an array by value, instead of the RPerl method of indirectly accessing the array by reference, such as is required by the push operator.
@{...}
The line starting with my string_arrayref $input_strings_reversed declares another array of string values input_strings_reversed, and then assigns it the strings contained within the first array $input_strings in reversed order, as returned by calling the reverse operator.
my string_arrayref $input_strings_reversed
input_strings_reversed
reverse
As with the push operator, the reverse operator requires its argument to be dereferenced using @{...}; another dereferenced array value is returned by reverse, and an array reference is returned by enclosing reverse and its argument inside the [...] (square brackets) array reference operator.
[...]
Finally, the line starting with foreach my string $input_strings_reversed_element denotes the beginning of another loop statement, which iterates the value of $input_strings_reversed_element once for each string value contained in the $input_strings_reversed array; print is called inside the loop body to display the original input strings in reverse order.
foreach my string $input_strings_reversed_element
$input_strings_reversed_element
$input_strings_reversed
#!/usr/bin/perl # Learning RPerl, Chapter 3, Exercise 1 # Print user-supplied list of strings in reverse order # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ OPERATIONS ]]] my string_arrayref $input_strings = []; print 'Please input zero or more strings, separated by <ENTER>, ended by <CTRL-D>:' . "\n"; while (my string $input_string = <STDIN>) { push @{$input_strings}, $input_string; } print "\n"; print 'Strings in reverse order:' . "\n"; my string_arrayref $input_strings_reversed = [reverse @{$input_strings}]; foreach my string $input_strings_reversed_element (@{$input_strings_reversed}) { print $input_strings_reversed_element; }
$ rperl -t LearningRPerl/Chapter3/exercise_1-stdin_strings_reverse.pl Please input zero or more strings, separated by <ENTER>, ended by <CTRL-D>: howdy doody buffalo bob clarabell clown Strings in reverse order: clown clarabell bob buffalo doody howdy
The goal of this exercise is to become familiar with utilizing array indices.
The first line in the OPERATIONS section declares a new variable $flintstones_and_rubbles of type string_arrayref, which is then initialized to contain a qw() (quoted word) set of names including the string element 'fred' at array index 0, 'betty' at array index 1, and so on.
$flintstones_and_rubbles
qw()
The second line in OPERATIONS creates an empty array of integers $input_indices, and the following while loop uses the push operator to fill $input_indices with integers entered by the user.
$input_indices
The foreach loop iterates through all the user input integers, placing each in the variable $input_index.
$input_index
Inside the foreach loop, the variable $input_index is used to access the individual names inside the variable $flintstones_and_rubbles.
In Perl, all array indices start at 0 instead of 1, so we must first subtract 1 from $input_index before accessing the individual string elements of $flintstones_and_rubbles.
Thus, if a user inputs the integer 1, the array index will be 0, which is 'fred'; similarly, user input 5 will access array index 4 which is 'wilma'.
#!/usr/bin/perl # Learning RPerl, Chapter 3, Exercise 2 # Print string array elements indexed by user-supplied integers # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ OPERATIONS ]]] my string_arrayref $flintstones_and_rubbles = [qw(fred betty barney dino wilma pebbles bamm-bamm)]; my integer_arrayref $input_indices = []; print 'Please input zero or more integers with values ranging from 1 to 7, separated by <ENTER>, ended by <CTRL-D>:' . "\n"; while ( my string $input_string = <STDIN> ) { push @{$input_indices}, string_to_integer($input_string); } print "\n"; print 'Flintstones & Rubbles:' . "\n"; foreach my integer $input_index ( @{$input_indices} ) { print $flintstones_and_rubbles->[ ( $input_index - 1 ) ] . "\n"; }
$ rperl -t LearningRPerl/Chapter3/exercise_2-stdin_array_indices.pl Please input zero or more integers with values ranging from 1 to 7, separated by <ENTER>, ended by <CTRL-D>: 2 5 3 6 5 1 2 7 4 4 Flintstones & Rubbles: betty wilma barney pebbles wilma fred betty bamm-bamm dino dino
The goal of this exercise is to become familiar with the sort and chomp operators.
sort
chomp
In the CONSTANTS section, a constant SINGLE_LINE_OUTPUT is created and set to 0, which will cause the program's output to be displayed on multiple lines. If SINGLE_LINE_OUTPUT is instead set to 1, the program's output will be displayed on one line.
SINGLE_LINE_OUTPUT
In the OPERATIONS section, a while loop is used to fill the array variable $input_strings with data supplied by the user.
The sort operator is then called to fill the $input_strings_sorted array with the same data in ASCII alphabetical order.
$input_strings_sorted
Last, a foreach loop is used to print the sorted output by storing and accessing one string at a time in the variable $input_strings_sorted_element.
$input_strings_sorted_element
An if conditional statement inside the foreach loop tests the constant SINGLE_LINE_OUTPUT; if the condition is true, then the chomp operator is called to remove the newline character from each string element and replace it with a normal blank space character, thereby displaying all the elements on a single line of output.
#!/usr/bin/perl # Learning RPerl, Chapter 3, Exercise 3 # Print user-supplied list of strings in ASCIIbetical order, optionally on single line of output # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitConstantPragma ProhibitMagicNumbers) # USER DEFAULT 3: allow constants ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ CONSTANTS ]]] use constant SINGLE_LINE_OUTPUT => my boolean $TYPED_SINGLE_LINE_OUTPUT = 0; # [[[ OPERATIONS ]]] my string_arrayref $input_strings = []; print 'Please input zero or more strings, separated by <ENTER>, ended by <CTRL-D>:' . "\n"; while ( my string $input_string = <STDIN> ) { push @{$input_strings}, $input_string; } print "\n"; print 'Strings in ASCIIbetical order:' . "\n"; my string_arrayref $input_strings_sorted = [ sort @{$input_strings} ]; foreach my string $input_strings_sorted_element ( @{$input_strings_sorted} ) { if ( SINGLE_LINE_OUTPUT() ) { # strip trailing newline, if present chomp $input_strings_sorted_element; $input_strings_sorted_element .= q{ }; } print $input_strings_sorted_element; } print "\n";
$ rperl -t LearningRPerl/Chapter3/exercise_3-stdin_strings_sort.pl Please input zero or more strings, separated by <ENTER>, ended by <CTRL-D>: howdy doody buffalo bob clarabell clown Strings in ASCIIbetical order: bob buffalo clarabell clown doody howdy
The goal of this exercise is to become familiar with user-defined subroutines.
In the SUBROUTINES section, one subroutine total() is defined, which accepts as input one argument variable $input_numbers of type number_arrayref, and which also has a number return value stored in the variable $retval.
SUBROUTINES
total()
$input_numbers
number_arrayref
$retval
Inside the total() subroutine, the return value is initialized to 0 in $retval, then a foreach loop iteratively adds the elements of the array $input_numbers to $retval, after which the value of $retval is returned to original external caller of total().
By itself, a subroutine such as total() does not actually do anything; every subroutine must first be called either by some other subroutine or in the OPERATIONS section.
In OPERATIONS, a 5-element array is created and stored in the variable $fred, which is then passed as input to the subroutine total(), and the return value is displayed using the variable $fred_total and the print operator.
$fred
$fred_total
Next, a while loop and <STDIN> are used to collect user input strings, which are then converted to numeric data values using the string_to_number() subroutine, and stored in the array $input_numbers using the push operator.
Finally, the subroutine total() is called a second time, now with the variable $input_numbers passed as the input argument, and the return value is displayed using the variable $user_total.
$user_total
#!/usr/bin/perl # Learning RPerl, Chapter 4, Exercise 1 # Subroutine & driver to calculate the totals of arrays of stringified numbers, both hard-coded and user-supplied # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ SUBROUTINES ]]] our number $total = sub { (my number_arrayref $input_numbers) = @_; my number $retval = 0; foreach my number $input_number (@{$input_numbers}) { $retval += $input_number; } return $retval; }; # [[[ OPERATIONS ]]] my number_arrayref $fred = [1, 3, 5, 7, 9]; my number $fred_total = total($fred); print 'The total of $fred is ' . to_string($fred_total) . "\n"; print 'Please input zero or more numbers, separated by <ENTER>, ended by <CTRL-D>:' . "\n"; my number_arrayref $input_numbers = []; while (my string $input_string = <STDIN>) { push @{$input_numbers}, string_to_number($input_string); } my number $user_total = total($input_numbers); print 'The total of those numbers is ' . to_string($user_total) . "\n";
$ rperl -t LearningRPerl/Chapter4/exercise_1-subroutine_total.pl The total of $fred is 25 Please input zero or more numbers, separated by <ENTER>, ended by <CTRL-D>: 21.12 23.42 1701.877 -123.456 The total of those numbers is 1_622.961
The goal of this exercise is to become familiar with the .. range operator.
..
In the SUBROUTINES section, the same subroutine total() is defined as in the previous exercise.
In the OPERATIONS section, an array of numbers is created and stored in the variable $one_to_one_thousand.
$one_to_one_thousand
Upon execution, there will be 1,000 number elements in the array, which are automatically created by the .. range operator.
The actual elements stored the array variable $one_to_one_thousand start with [1, 2, 3, 4 and end with 997, 998, 999, 1_000].
[1, 2, 3, 4
997, 998, 999, 1_000]
Finally, the subroutine total() is called with $one_to_one_thousand passed as the input argument, and the return value is displayed using the variable $one_to_one_thousand_total.
$one_to_one_thousand_total
#!/usr/bin/perl # Learning RPerl, Chapter 4, Exercise 2 # Subroutine & driver to calculate the total of 1 to 1,000 # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ SUBROUTINES ]]] our number $total = sub { ( my number_arrayref $input_numbers ) = @_; my number $retval = 0; foreach my number $input_number ( @{$input_numbers} ) { $retval += $input_number; } return $retval; }; # [[[ OPERATIONS ]]] my number_arrayref $one_to_one_thousand = [ 1 .. 1_000 ]; my number $one_to_one_thousand_total = total($one_to_one_thousand); print 'The total of 1 to 1_000 is ' . to_string($one_to_one_thousand_total) . q{.} . "\n";
$ rperl -t LearningRPerl/Chapter4/exercise_2-subroutine_total_1000.pl The total of 1 to 1000 is 500_500.
The goal of this exercise is to become familiar with calling user-defined subroutines from one another.
In the SUBROUTINES section, 3 subroutines are defined: total() (same as previous exercises), average(), and above_average().
average()
above_average()
Inside the subroutine average() is a call to the subroutine total(); there is also a call to the scalar operator, which returns the count of elements inside the array $input_numbers. When the return value of total() is divided by that of scalar, the result is computation of the numeric mean (average) of all elements of $input_numbers.
scalar
Inside above_average() is a call to the subroutine average(), with the return value stored in the variable $average. An empty array is created in the variable $retval, then a foreach loop iterates over all elements in $input_numbers and an if conditional statement makes a copy of all elements which are greater than $average. All above-average elements are returned as an array in $retval.
$average
In the OPERATIONS section, 2 arrays are created in the $fred and $barney variables, which are then passed as input arguments to 2 calls to the subroutine above_average(), and the results are displayed.
$barney
#!/usr/bin/perl # Learning RPerl, Chapter 4, Exercise 3 # Subroutines & driver to calculate the above-average elements of hard-coded arrays # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ SUBROUTINES ]]] our number $total = sub { ( my number_arrayref $input_numbers ) = @_; my number $retval = 0; foreach my number $input_number ( @{$input_numbers} ) { $retval += $input_number; } return $retval; }; our number $average = sub { ( my number_arrayref $input_numbers ) = @_; return (total($input_numbers) / (scalar @{$input_numbers})); }; our number_arrayref $above_average = sub { ( my number_arrayref $input_numbers ) = @_; my number $average = average($input_numbers); my number_arrayref $retval = []; foreach my number $input_number (@{$input_numbers}) { if ($input_number > $average) { push @{$retval}, $input_number; } } return $retval; }; # [[[ OPERATIONS ]]] my string_arrayref $fred = [1 .. 10]; my number $fred_above_average = above_average($fred); print '$fred is ' . number_arrayref_to_string($fred) . "\n"; print 'The above-average elements of $fred are ' . number_arrayref_to_string($fred_above_average) . "\n"; print '(Should be [6, 7, 8, 9, 10])' . "\n\n"; my string_arrayref $barney = [100, 1 .. 10]; my number $barney_above_average = above_average($barney); print '$barney is ' . number_arrayref_to_string($barney) . "\n"; print 'The above-average elements of $barney are ' . number_arrayref_to_string($barney_above_average) . "\n"; print '(Should be just [100])' . "\n";
$ rperl -t LearningRPerl/Chapter4/exercise_3-subroutine_above_average.pl $fred is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] The above-average elements of $fred are [6, 7, 8, 9, 10] (Should be [6, 7, 8, 9, 10]) $barney is [100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] The above-average elements of $barney are [100] (Should be just [100])
The goal of this exercise is to become familiar with program state.
As a program executes, there may be one or more variables in the OPERATIONS section which store information that is important to the overall program; these variables are collectively known as the "state" of the program.
In the OPERATIONS section, a string variable $previous_name is created and set to the empty string q{}; this 1 variable is the state of the program.
$previous_name
Each of the 4 following lines in the OPERATIONS section utilize the program state by both reading from and writing to the variable $previous_name; the calls to the subroutine greet() read from $previous_name as the 2nd argument, and the return value of greet() is then written to $previous_name by the = assignment operator.
greet()
In the SUBROUTINES section, 1 subroutine greet() is defined which accepts 2 string arguments, the variables $name and $previous_name.
$name
Inside greet(), a personalized greeting is displayed for the virtual person represented by the variable $name, after which an if() else conditional statement checks if there is any data inside $previous_name and further displays a customized comment about the state of the program. The state variable $previous_name is used to represent the very simple state of virtual people who have already been greeted.
if() else
Finally, the string variable $name is returned from greet() and passed to the = assignment operators in the OPERATIONS section, thereby updating the program state variable $previous_name to contain the value of $name.
#!/usr/bin/perl # Learning RPerl, Chapter 4, Exercise 4 # Subroutine & driver to greet users # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ SUBROUTINES ]]] our string $greet = sub { ( my string $name, my string $previous_name ) = @_; print 'Hi ' . $name . '! '; if ($previous_name eq q{}) { print 'You are the first one here!' . "\n"; } else { print $previous_name . ' is also here!' . "\n"; } return $name; }; # [[[ OPERATIONS ]]] my string $previous_name = q{}; $previous_name = greet('Fred', $previous_name); $previous_name = greet('Barney', $previous_name); $previous_name = greet('Wilma', $previous_name); $previous_name = greet('Betty', $previous_name);
$ rperl -t LearningRPerl/Chapter4/exercise_4-subroutine_greet.pl Hi Fred! You are the first one here! Hi Barney! Fred is also here! Hi Wilma! Barney is also here! Hi Betty! Wilma is also here!
The goal of this exercise is to become further familiarized with program state.
In the OPERATIONS section, the state variable $previous_names is an array of strings representing all previous virtual people who have been greeted.
$previous_names
In the SUBROUTINES section, the subroutine greet() prints the names of previously-greeted virtual people on one line, separated by spaces, by using the join operator.
join
Finally, the current value of $name is appended as the last element of the array $previous_names by the push operator, and $previous_names is then returned by greet() to update the program state.
#!/usr/bin/perl # Learning RPerl, Chapter 4, Exercise 5 # Subroutine & driver to greet multiple users # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ SUBROUTINES ]]] our string_arrayref $greet = sub { ( my string $name, my string_arrayref $previous_names ) = @_; print 'Hi ' . $name . '! '; if ((scalar @{$previous_names}) == 0) { print 'You are the first one here!' . "\n"; } else { print q{I've seen: } . (join q{ }, @{$previous_names}) . "\n"; } push @{$previous_names}, $name; return $previous_names; }; # [[[ OPERATIONS ]]] my string_arrayref $previous_names = []; $previous_names = greet('Fred', $previous_names); $previous_names = greet('Barney', $previous_names); $previous_names = greet('Wilma', $previous_names); $previous_names = greet('Betty', $previous_names);
$ rperl -t LearningRPerl/Chapter4/exercise_5-subroutine_greet_multiple.pl Hi Fred! You are the first one here! Hi Barney! I've seen: Fred Hi Wilma! I've seen: Fred Barney Hi Betty! I've seen: Fred Barney Wilma
The goal of this exercise is to become familiar with file test operators, as well as opening, closing, and reading from a file.
In the CRITICS section, the ProhibitPostfixControls critic is disabled due to a bug in Perl::Critic and/or PPI which causes a false error.
ProhibitPostfixControls
In the SUBROUTINES section, 1 subroutine tac() is defined which accepts as input an array of file names received via the operating system's command-line arguments, and which has a void return value, meaning there is no return value for this subroutine.
tac()
void
Inside tac(), the reverse operator is called to reverse the order of the command-line arguments; for example, if the 3 file names fred barney betty are given as command-line arguments, then reverse causes the 3 strings betty barney fred to be stored in the array variable $command_line_arguments.
fred barney betty
betty barney fred
$command_line_arguments
The outer foreach loop iterates through each $file_name in the now-reversed $command_line_arguments.
$file_name
Next, 4 file test operators are called, to ensure each input file exists via -e, is readable via -r, is a regular (not special device) file via -f, and is comprised of plain text via -T.
-e
-r
-f
-T
The operator open is called with the < file input (read-only) argument, which opens each $file_name for reading via the $FILE filehandle variable.
open
$FILE
The inner while loop reads in all lines from the current $FILE filehandle using the <$FILE> syntax (similar to <STDIN>) and stores the input file's lines in the string array variable $file_lines; then, the reverse operator is called again to reverse the order of the newly-read file lines.
<$FILE>
$file_lines
Finally, the inner foreach loop displays the now-reversed $file_lines, and the close operator is called to close the $FILE filehandle so no more file access will occur for the current $file_name.
close
In the OPERATIONS section, the tac() subroutine is called with its only argument being a reference to the special @ARGV array, which is Perl's way of accessing the command-line arguments.
@ARGV
Before executing this program, the non-Perl printf program must be called to populate some test data into the 3 input files fred, barney, and betty; and after execution the rm program is called to delete the 3 input files.
printf
rm
To begin execution of this program via the rperl command, the program name and input file names must be enclosed in either 'single quotes' or "double quotes"; this tells RPerl the input file names are command-line arguments to be passed to the 1 specified program, instead of specifying additional RPerl programs.
#!/usr/bin/perl # Learning RPerl, Chapter 5, Exercise 1 # Accept one or more input files, and print their contents line-by-line in reverse order # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitPostfixControls) # SYSTEM SPECIAL 6: PERL CRITIC FILED ISSUE #639, not postfix foreach or if # [[[ SUBROUTINES ]]] our void $tac = sub { ( my string_arrayref $command_line_arguments ) = @_; $command_line_arguments = [ reverse @{$command_line_arguments} ]; foreach my string $file_name ( @{$command_line_arguments} ) { if ( not( -e $file_name ) ) { croak 'ERROR: File ' . $file_name . ' does not exist, croaking'; } if ( not( -r $file_name ) ) { croak 'ERROR: File ' . $file_name . ' is not readable, croaking'; } if ( not( -f $file_name ) ) { croak 'ERROR: File ' . $file_name . ' is not a regular file, croaking'; } if ( not( -T $file_name ) ) { croak 'ERROR: File ' . $file_name . ' is (probably) not text, croaking'; } my integer $open_success = open my filehandleref $FILE, '<', $file_name; if ( not $open_success ) { croak 'ERROR: Failed to open file ' . $file_name . ' for reading, croaking'; } my string_arrayref $file_lines = []; while ( my string $file_line = <$FILE> ) { push @{$file_lines}, $file_line; } $file_lines = [ reverse @{$file_lines} ]; foreach my string $file_line ( @{$file_lines} ) { print $file_line; } if ( not close $FILE ) { croak 'ERROR: Failed to close file ' . $file_name . ' after reading, croaking'; } } }; # [[[ OPERATIONS ]]] tac( [@ARGV] );
$ printf "fred0\nfred1\nfred2\nfred3\nfred4\n" > fred $ printf "barney0\nbarney1\nbarney2\n" > barney $ printf "betty0\nbetty1\nbetty2\nbetty3\n" > betty $ rperl -t 'LearningRPerl/Chapter5/exercise_1-tac.pl fred barney betty' betty3 betty2 betty1 betty0 barney2 barney1 barney0 fred4 fred3 fred2 fred1 fred0 $ rm fred barney betty
The goal of this exercise is to become familiar with the string length operator and basic text formatting.
length
In the SUBROUTINES section, 1 subroutine right_justify_20() is defined, which accepts no input arguments and returns no values.
right_justify_20()
Inside right_justify_20(), an empty array of strings is initialized in the $input_strings variable, which is then populated with strings in a while loop collecting user input from <STDIN>.
The x string repeat operator is called to create a 60-character-wide horizontal ruler, which is then displayed by the print operator.
Finally, a foreach loop iterates through each $input_string and calls the length operator, thereby determining the correct number of spaces to prepend in order to achieve right justification alignment to the 20th character column. The - subtraction operator is passed a hard-coded column width of 20, and the chomp operator is called to remove any trailing newline characters which may be appended when the user presses the ENTER key after each input word.
In the OPERATIONS section, the only operation is a call to the right_justify_20() subroutine.
#!/usr/bin/perl # Learning RPerl, Chapter 5, Exercise 2 # Accept one or more input lines, and print them in a right-justified 20-column format # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ SUBROUTINES ]]] our void $right_justify_20 = sub { my string_arrayref $input_strings = []; print 'Please input zero or more strings, separated by <ENTER>, ended by <CTRL-D>:' . "\n"; while ( my string $input_string = <STDIN> ) { push @{$input_strings}, $input_string; } print "\n"; print '1234567890' x 6; print "\n"; foreach my string $input_string ( @{$input_strings} ) { chomp $input_string; print q{ } x ( 20 - ( length $input_string ) ); print $input_string . "\n"; } }; # [[[ OPERATIONS ]]] right_justify_20();
$ rperl -t LearningRPerl/Chapter5/exercise_2-right_justify.pl Please input zero or more strings, separated by <ENTER>, ended by <CTRL-D>: howdy doody buffalo bob clarabell clown 123456789012345678901234567890123456789012345678901234567890 howdy doody buffalo bob clarabell clown
The goal of this exercise is to become further familiarized with basic text formatting.
In the SUBROUTINES section, 1 subroutine right_justify_variable() is defined, which accepts no input arguments and returns no values.
right_justify_variable()
Inside right_justify_variable(), the user is prompted to input a custom right justify width, stored in the integer variable $column_width.
$column_width
The integer variable $ruler_width_tens is used to determine the number of characters displayed for the ruler, by tens; the default value for $ruler_width_tens is 6, which means a ruler width of 60 characters. If the user-supplied $column_width is greater than 60, we scale it by 1/10 and add 1, thereby creating a new value for $ruler_width_tens which will always display a ruler wider than $column_width.
$ruler_width_tens
When the ruler is displayed, $ruler_width_tens is passed to the x string repeat operator instead of a hard-coded value of 6; likewise, when each $input_string is right justified, the - subtraction operator is passed $column_width instead of a hard-coded value of 20.
In the OPERATIONS section, the only operation is a call to the right_justify_variable() subroutine.
#!/usr/bin/perl # Learning RPerl, Chapter 5, Exercise 3 # Accept column width followed by one or more input lines, and print lines in a right-justified format # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ SUBROUTINES ]]] our void $right_justify_variable = sub { my string_arrayref $input_strings = []; print 'Please input integer column width, then press <ENTER>:' . "\n"; my string $column_width_string = <STDIN>; my integer $column_width = string_to_integer($column_width_string); print 'Please input zero or more strings, separated by <ENTER>, ended by <CTRL-D>:' . "\n"; while ( my string $input_string = <STDIN> ) { push @{$input_strings}, $input_string; } my integer $ruler_width_tens = 6; # default to ruler line width 60 if ( $column_width > 60 ) { $ruler_width_tens = number_to_integer( $column_width / 10 ) + 1; } print "\n"; print '1234567890' x $ruler_width_tens; print "\n"; foreach my string $input_string ( @{$input_strings} ) { chomp $input_string; print q{ } x ( $column_width - ( length $input_string ) ); print $input_string . "\n"; } }; # [[[ OPERATIONS ]]] right_justify_variable();
$ rperl -t LearningRPerl/Chapter5/exercise_3-right_justify_variable.pl Please input integer column width, then press <ENTER>: 67 Please input zero or more strings, separated by <ENTER>, ended by <CTRL-D>: howdy doody buffalo bob clarabell clown 1234567890123456789012345678901234567890123456789012345678901234567890 howdy doody buffalo bob clarabell clown
The goal of this exercise is to become familiar with hash data structures and hash operators.
In the SUBROUTINES section, 1 subroutine given_to_family_name() is defined, which accepts no input arguments and returns no values.
given_to_family_name()
Inside given_to_family_name(), a hash data structure is created in the variable $names. Another term for hash is associative array, because each hash is comprised of key-value pairs, and we say there is a value "associated" with every key. Hash keys are bare words and must be unique, while hash values may be any specified data type and need not be unique. The 3 keys in $names are fred, barney, and wilma; the value of the key fred is 'flintstone'.
$names
A first name is collected from the user by <STDIN> and stored in $given_name, then the chomp operator is called to remove the trailing newline recorded when the user presses the ENTER key.
$given_name
An if conditional statement calls the exists and defined operators to ensure the user has entered a valid hash key, and an error is returned if there is no such key in the $names hash.
exists
Finally, the thin-arrow syntax $names->{$given_name} is used to retrieve the hash value, and print is called to display the family name outputs.
$names->{$given_name}
In the OPERATIONS section, the only operation is a call to the given_to_family_name() subroutine.
#!/usr/bin/perl # Learning RPerl, Chapter 6, Exercise 1 # Accept one input given (first) name, and print the corresponding family (last) name # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ SUBROUTINES ]]] our void $given_to_family_name = sub { my string_hashref $names = { fred => 'flintstone', barney => 'rubble', wilma => 'flintstone' }; print 'Please input a given (first) name in all lowercase, then press <ENTER>:' . "\n"; my string $given_name = <STDIN>; chomp $given_name; if ((not exists $names->{$given_name}) or (not defined $names->{$given_name})) { croak 'ERROR: No family (last) name found for given (first) name ' . $given_name . ', croaking' . "\n"; } print 'The family (last) name of ' . $given_name . ' is ' . $names->{$given_name} . q{.} . "\n"; }; # [[[ OPERATIONS ]]] given_to_family_name();
$ rperl -t LearningRPerl/Chapter6/exercise_1-hash_family_names.pl Please input a given (first) name in all lowercase, then press <ENTER>: fred The family (last) name of fred is flintstone. $ rperl -t LearningRPerl/Chapter6/exercise_1-hash_family_names.pl Please input a given (first) name in all lowercase, then press <ENTER>: howdy ERROR: No family (last) name found for given (first) name howdy, croaking
The goal of this exercise is to become further familiarized with hash data structures and hash operators.
In the SUBROUTINES section, 1 subroutine unique_word_count() is defined, which accepts no input arguments and returns no values.
unique_word_count()
In unique_word_count(), an empty hash of integer values is created in the variable $word_counts; a while loop then collects user input strings, inside of which an if() else conditional statement updates the $word_counts hash. If a word is seen for the first time, then the corresponding $word_counts hash value will be set to 1, otherwise the count value will be incremented by 1.
$word_counts
Finally, a foreach loop iterates through the alphabetically-sorted keys of the $word_counts hash by calling the sort and keys operators, then the thin-arrow hash value retrieval syntax $word_counts->{$unique_word} is called, and the print operator displays the word count outputs.
keys
$word_counts->{$unique_word}
In the OPERATIONS section, the only operation is a call to the unique_word_count() subroutine.
#!/usr/bin/perl # Learning RPerl, Chapter 6, Exercise 2 # Accept a list of words, and print the count of each unique word # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ SUBROUTINES ]]] our void $unique_word_count = sub { my integer_hashref $word_counts = {}; print 'Please input zero or more words, separated by <ENTER>, ended by <CTRL-D>:' . "\n"; while (my string $input_word = <STDIN>) { chomp $input_word; if (not exists $word_counts->{$input_word}) { $word_counts->{$input_word} = 1; } else { $word_counts->{$input_word} += 1; } } print "\n" . 'Unique word count:' . "\n"; foreach my string $unique_word (sort keys %{$word_counts}) { print $unique_word . ' appeared ' . to_string($word_counts->{$unique_word}) . ' time(s)' . "\n"; } }; # [[[ OPERATIONS ]]] unique_word_count();
$ rperl -t LearningRPerl/Chapter6/exercise_2-hash_unique_word_count.pl Please input zero or more words, separated by <ENTER>, ended by <CTRL-D>: george jane judy elroy astro rosie george judy astro fred wilma barney betty pebbles bamm-bamm dino fred barney dino george fred barney astro dino jane judy betty wilma Unique word count: astro appeared 3 time(s) bamm-bamm appeared 1 time(s) barney appeared 3 time(s) betty appeared 2 time(s) dino appeared 3 time(s) elroy appeared 1 time(s) fred appeared 3 time(s) george appeared 3 time(s) jane appeared 2 time(s) judy appeared 3 time(s) pebbles appeared 1 time(s) rosie appeared 1 time(s) wilma appeared 2 time(s)
The goal of this exercise is to become further familiarized with hash data structures and basic text formatting.
In the SUBROUTINES section, 1 subroutine sort_env_vars() is defined, which accepts no input arguments and returns no values.
sort_env_vars()
Inside sort_env_vars(), a hash of strings is created in the variable $env_vars, and it is initialized to contain the values of the special %ENV system hash, which stores the current user's environmental variables.
$env_vars
%ENV
Next, 2 integer variables $env_var_length and $left_column_width are created, and $left_column_width is initialized to the value 0. A foreach loop iterates through all environmental variables, measuring the string length of each $env_var by the length operator, and using an if conditional statement to test if the current $env_var_length is greater than the existing $left_column_width. If $env_var_length is large enough, then $left_column_width is updated, thereby resulting in the value of $left_column_width being equal to the longest $env_var_length.
$env_var_length
$left_column_width
$env_var
After the foreach loop, $left_column_width is incremented by an additional 2 character widths, allowing for 2 or more spaces between the hash keys and their respective values when displayed.
Finally, there is another foreach loop below the first, again iterating through all $env_vars, and printing the keys at the beginning of each output line. The - subtraction operator is called to find the difference between the current key length and the pre-calculated $left_column_width, then the x string repeat operator is called to create the corresponding number of blank spaces as padding between the key and its value. The thin-arrow hash value retrieval syntax $env_vars->{$env_var} is called, and the print operator displays the hash value outputs.
$env_vars->{$env_var}
In the OPERATIONS section, the only operation is a call to the sort_env_vars() subroutine.
#!/usr/bin/perl # Learning RPerl, Chapter 6, Exercise 3 # Print sorted environmental variables # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ SUBROUTINES ]]] our void $sort_env_vars = sub { my string_hashref $env_vars = {%ENV}; my integer $env_var_length; my integer $left_column_width = 0; foreach my string $env_var ( sort keys %{$env_vars} ) { $env_var_length = length $env_var; if ( $env_var_length > $left_column_width ) { $left_column_width = $env_var_length; } } $left_column_width += 2; print 'Environmental variables:' . "\n"; foreach my string $env_var ( sort keys %{$env_vars} ) { print $env_var; print q{ } x ( $left_column_width - ( length $env_var ) ); print $env_vars->{$env_var} . "\n"; } }; # [[[ OPERATIONS ]]] sort_env_vars();
$ rperl -t LearningRPerl/Chapter6/exercise_3-hash_sort_env_vars.pl Environmental variables: COLORTERM xfce4-terminal DESKTOP_SESSION xubuntu DISPLAY :0.0 GDMSESSION xubuntu GDM_LANG en_US HOME /home/wbraswell LANG en_US.UTF-8 LANGUAGE en_US LOGNAME wbraswell PATH /home/wbraswell/austin_perl_mongers/rperl/rperl-latest/script:.:script:bin:/home/wbraswell/script:/home/wbraswell/bin:/home/wbraswell/perl5/bin:/home/wbraswell/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games PERL5LIB /home/wbraswell/austin_perl_mongers/rperl/rperl-latest/lib:blib/lib:lib:/home/wbraswell/perl5/lib/perl5 PERL_LOCAL_LIB_ROOT /home/wbraswell/perl5 PERL_MB_OPT --install_base "/home/wbraswell/perl5" PERL_MM_OPT INSTALL_BASE=/home/wbraswell/perl5 PWD /home/wbraswell/austin_perl_mongers/rperl/rperl-latest SHELL /bin/bash TERM xterm USER wbraswell
RPerl's grammar is written using the Eyapp computer programming language, which is a combination of normal Perl 5 and grammar expressions.
The grammar expression sections in Eyapp source code are written using an implementation of the Extended Backus-Naur Form (EBNF) language.
The file lib/RPerl/Grammar.eyp contains the uncompiled RPerl grammar, which is passed through the eyapp compiler command once to generate the output file lib/RPerl/Grammar.pm, which is then used by the rperl compiler command to parse RPerl input source code files.
eyapp
Inside the lib/RPerl/Grammar.eyp file, there are several labeled file sections, which can be grouped into 4 major categories:
Eyapp Setup & Config
At the top of the Grammar.eyp file, the [[[ EYAPP SETUP & CONFIG ]]] section contains Perl 5 code to initialize RPerl within the %{...}% delimiters, as well as the %strict and %tree Eyapp configuration directives for automatically building an abstract syntax tree (AST) from an RPerl input file.
[[[ EYAPP SETUP & CONFIG ]]]
%{...}%
%strict
%tree
Lexicon
Near the top of the Grammar.eyp file, multiple [[[ LEXICON TOKENS ... ]]] sections contain all valid RPerl tokens in the form of Perl 5 regular expressions. The lexicon of a language is synonymous with the language's alphabet and vocabulary; lexical analysis is synonymous with spell checking.
[[[ LEXICON TOKENS ... ]]]
Syntax
In the middle of the Grammar.eyp file, one [[[ SYNTAX, OPERATOR PRECEDENCE & ASSOCIATIVITY ]]] section and multiple [[[ SYNTAX PRODUCTION RULES ... ]]] sections define all relationships between all RPerl tokens, as well as all valid combinations of RPerl tokens. The syntax of a language is synonymous with the rules of constructing valid sentences, and syntactic analysis is synonymous with grammar checking.
[[[ SYNTAX, OPERATOR PRECEDENCE & ASSOCIATIVITY ]]]
[[[ SYNTAX PRODUCTION RULES ... ]]]
Semantics
Near the top of the Grammar.eyp file, the [[[ SEMANTIC ACTION, ABSTRACT SYNTAX TREE NODE CONSTRUCTOR ]]] section creates new Perl 5 objects (blessed hash references) to be used as nodes when constructing an AST; at the bottom of the file, the [[[ SEMANTIC MAP, ABSTRACT SYNTAX TREE NODES TO CLASSES ]]] section contains a 1-to-1 mapping from AST nodes to RPerl objects. The Perl module file for each of the RPerl objects contains subroutines implementing the semantic actions which actually generate and/or execute output code. The semantics of a language are synonymous with the meaning of words and sentences; in computer programming languages, semantic actions are the tasks which the computer source code performs when executed.
[[[ SEMANTIC ACTION, ABSTRACT SYNTAX TREE NODE CONSTRUCTOR ]]]
[[[ SEMANTIC MAP, ABSTRACT SYNTAX TREE NODES TO CLASSES ]]]
For more information, please view the following links:
Eyapp Documentation
Extended Backus-Naur Form on Wikipedia
RPerl Grammar.eyp Eyapp File
Following is a list of all RPerl tokens in all 4 lexicon sections, along with examples of valid matching lexeme input.
The list must be in correct order for all regexes to match; earlier declarations get tried first, thus highly-specific tokens such as RPerl keywords and built-in operators appear first, while the least-specific tokens such as user-defined words appear last. This ordering can be considered "lexical matching", and is distinct from operator precedence and associativity as covered in the next section.
[[[ LEXICON TOKENS, WHITESPACE ]]]
actual whitespace, or one or more normal comments; neither shebang #!, nor double-hash critics ##
[[[ LEXICON TOKENS, TYPES & RESERVED WORDS ]]]
SHEBANG
begin line, hash (octothorpe), bang, *NIX path to Perl; ex. #!/usr/bin/perl
USE_RPERL
USE_RPERL_AFTER
use RPerl::AfterSubclass;
USE
VERSION_NUMBER_ASSIGN
$VERSION assign v-number, only capture v-number, not a normal number; ex. $VERSION = 12.345_678; returns 12.345_678
$VERSION = 12.345_678;
12.345_678
MY
LITERAL_NUMBER
number with underscores, optional scientific notation; ex. 12_345_678.910_1
12_345_678.910_1
LITERAL_STRING
single quotes non-empty; double quotes non-empty without sigils or extra backslashes & with newline or tab; or single q-braces; ex. 'howdy $foo!' or "howdy foo!\n" or q{howdy $foo!}
'howdy $foo!'
"howdy foo!\n"
q{howdy $foo!}
SELF
$self
VARIABLE_SYMBOL
dollar sigil, scoped word with at least one lowercase in the first scope segment; ex. $foo or $Foo::Bar::baz
$Foo::Bar::baz
FHREF_SYMBOL_IN
less-than, dollar sigil, uppercase letter, uppercase letters & numbers & underscores, greater-than; ex. <$MY_FILEHANDLE_23>
<$MY_FILEHANDLE_23>
FHREF_SYMBOL_BRACES
left brace, dollar sigil, uppercase letter, uppercase letters & numbers & underscores, right brace; ex. {$MY_FILEHANDLE_23}
{$MY_FILEHANDLE_23}
FHREF_SYMBOL
dollar sigil, uppercase letter, uppercase letters & numbers & underscores; ex. $MY_FILEHANDLE_23
$MY_FILEHANDLE_23
TYPE_INTEGER
integer or unsigned_integer followed by whitespace
TYPE_FHREF
filehandleref followed by whitespace
filehandleref
TYPE_METHOD
optionally-scoped word, followed by '::method'; ex. string_arrayref::method or Foo::Bar::method
string_arrayref::method
Foo::Bar::method
CONSTANT_CALL_SCOPED
optionally-scoped constant call; ex. MY_CONST() or Foo::Bar::BAZ_CONST()
MY_CONST()
Foo::Bar::BAZ_CONST()
WORD_SCOPED
optionally-scoped word; ex. my_word or My_Word or Foo::Bar::baz_word
my_word
My_Word
Foo::Bar::baz_word
STDOUT_STDERR
{*STDOUT} or {*STDERR}
{*STDOUT}
{*STDERR}
STDIN
ARGV
ENV
[[[ LEXICON TOKENS, OPERATORS ]]]
OP24_LOGICAL_OR_XOR
precedence 24 infix: logical or and xor, equivalent to || except for precedence
OP23_LOGICAL_AND
precedence 23 infix: logical and, equivalent to && except for precedence
OP22_LOGICAL_NEG
precedence 22 prefix: logical negation not, equivalent to ! except for precedence
OP21_LIST_COMMA
precedence 21 infix: "list operators (rightward)" [1] AKA comma ,
,
OP20_HASH_FATARROW
precedence 20 infix: hash entry fat arrow AKA fat comma =>
=>
OP19_LOOP_CONTROL_SCOLON
precedence 19 prefix void: loop control next;, last;
next;
last;
OP19_LOOP_CONTROL
precedence 19 prefix void: same as above, except allows redo and requires loop label
redo
OP18_TERNARY
precedence 18 infix: ternary conditional ?
?
OP17_LIST_RANGE
precedence 17 infix: range ..
OP16_LOGICAL_OR
precedence 16 infix: logical or ||
OP15_LOGICAL_AND
precedence 15 infix: logical and &&
OP14_BITWISE_OR_XOR
precedence 14 infix: bitwise or |, bitwise xor ^
OP13_BITWISE_AND
precedence 13 infix: bitwise and &
OP12_COMPARE_EQ_NE
precedence 12 infix: comparison numeric equal ==, numeric not equal !=, string equal eq, string not equal ne
eq
ne
OP09_BITWISE_SHIFT
precedence 09 infix: bitwise shift left <<, shift right >>
OP10_NAMED_UNARY_SCOLON
precedence 10 prefix: "named unary operators" [1] and Programming Perl, Chapter 3, List of All Named Unary Operators; -A;, -B;, -C;, -M;, -O;, -R;, -S;, -T;, -W;, -X;, -b;, -c;, -d;, -e;, -f;, -g;, -k;, -l;, -o;, -p;, -r;, -s;, -t;, -u;, -w;, -x;, -z;, alarm;, caller;, chdir;, chroot;, cos;, defined;, delete;, do;, eval;, exists;, gethostbyname;, getnetbyname;, getpgrp;, getprotobyname;, glob;, gmtime;, goto;, hex;, int;, lc;, lcfirst;, length;, localtime;, lock;, log;, lstat;, oct;, ord;, quotemeta;, rand;, readlink;, ref;, require;, rmdir;, scalar;, sin;, sleep;, sqrt;, srand;, stat;, uc;, ucfirst;, umask;
-A;
-B;
-C;
-M;
-O;
-R;
-S;
-T;
-W;
-X;
-b;
-c;
-d;
-e;
-f;
-g;
-k;
-l;
-o;
-p;
-r;
-s;
-t;
-u;
-w;
-x;
-z;
alarm;
caller;
chdir;
chroot;
cos;
defined;
delete;
do;
eval;
exists;
gethostbyname;
getnetbyname;
getpgrp;
getprotobyname;
glob;
gmtime;
goto;
hex;
int;
lc;
lcfirst;
length;
localtime;
lock;
log;
lstat;
oct;
ord;
quotemeta;
rand;
readlink;
ref;
require;
rmdir;
scalar;
sin;
sleep;
sqrt;
srand;
stat;
uc;
ucfirst;
umask;
OP10_NAMED_UNARY
same as above, except without semicolon; scalar not scalartype
scalartype
OP19_VARIABLE_ASSIGN_BY
precedence 19 infix: add assign +=, subtract assign -=, multiply assign *=, divide assign /=, string concatenation assign .=
+=
-=
*=
/=
.=
OP08_STRING_CAT
precedence 08 infix: string concatenate .
OP03_MATH_INC_DEC
precedence 03 prefix and postfix: increment ++, decrement --
OP04_MATH_POW
precedence 04 infix: arithmetic exponent AKA power **
OP07_MATH_MULT_DIV_MOD
precedence 07 infix: arithmetic multiply *, divide /, modulo %, SSE multiply sse_mul, SSE divide sse_div
sse_mul
sse_div
OP07_STRING_REPEAT
precedence 07 infix: string repetition x
OP06_REGEX_PATTERN
precedence 06 infix: regular expression pattern; ex. m/foo.*/xms or s/foo/bar/gxms
m/foo.*/xms
s/foo/bar/gxms
OP06_REGEX_MATCH
precedence 06 infix: regular expression match =~, not match !~
=~
!~
OP05_LOGICAL_NEG
precedence 05 prefix: logical negation !
OP02_HASH_THINARROW
precedence 02 infix: thin arrow, hash dereference and retrieval ->{
->{
OP02_ARRAY_THINARROW
precedence 02 infix: thin arrow, array dereference and retrieval ->[
->[
OP02_METHOD_THINARROW_NEW
precedence 02 infix: thin arrow, class constructor ->new(
->new(
OP02_METHOD_THINARROW
precedence 02 infix: thin arrow, method dereference and call; ex. ->foo or ->Bar23
->foo
->Bar23
OP05_MATH_NEG_LPAREN
precedence 05 prefix: arithmetic negative -(
-(
OP08_MATH_ADD_SUB
precedence 08 infix: arithmetic add +, subtract -, SSE add sse_add, SSE subtract sse_sub
sse_add
sse_sub
OP11_COMPARE_LT_GT
precedence 11 infix: numeric comparison less or equal <=, greater or equal >=, less than <, greater than >; string comparison less or equal le, greater or equal ge, less than lt, greater than gt
le
ge
lt
gt
OP19_VARIABLE_ASSIGN
precedence 19 infix: assign =
OP01_PRINT
precedence 01 prefix void: print or printf to STDOUT, STDERR, or filehandle
OP01_NAMED_VOID_SCOLON
precedence 01 prefix void: "terms and list operators (leftward)" [1] AKA built-ins, no return value; croak;, die;, exit;, return;
croak;
die;
exit;
return;
OP01_NAMED_VOID_LPAREN
precedence 01 prefix void: same as above, except with parentheses & without semicolon & without die; croak(, exit(, return(
croak(
exit(
return(
OP01_NAMED_VOID
precedence 01 prefix void: same as above, except accepts argument(s); croak, die, exit, return
croak
exit
return
OP01_QW
precedence 01 prefix: quoted words; ex. qw() or qw(foo bar baz) or qw(Foo23 BarBax Ba_z 123)
qw(foo bar baz)
qw(Foo23 BarBax Ba_z 123)
OP01_OPEN
precedence 01 prefix: open filehandle
OP01_CLOSE
precedence 01 prefix: close filehandle
OP01_NAMED_SCOLON
precedence 01 prefix: "terms and list operators (leftward)" [1] AKA built-ins; List Of All Operators in Perl 5 Source Code [2]; without all-uppercase Perl system built-in keywords (__DATA__, AUTOLOAD, CHECK, etc); named unary operators above (defined, exists, etc); and RPerl keywords (use, our, my, package, for, etc); abs;, accept;, atan2;, bind;, binmode;, bless;, break;, chmod;, chomp;, chop;, chown;, chr;, closedir;, cmp;, connect;, continue;, crypt;, dbmclose;, dbmopen;, default;, dump;, each;, endgrent;, endhostent;, endnetent;, endprotoent;, endpwent;, endservent;, eof;, evalbytes;, exec;, exp;, fc;, fcntl;, fileno;, flock;, fork;, format;, formline;, getc;, getgrent;, getgrgid;, getgrnam;, gethostbyaddr;, gethostent;, getlogin;, getnetbyaddr;, getnetent;, getpeername;, getppid;, getpriority;, getprotobynumber;, getprotoent;, getpwent;, getpwnam;, getpwuid;, getservbyname;, getservbyport;, getservent;, getsockname;, getsockopt;, given;, grep;, index;, ioctl;, join;, keys;, kill;, link;, listen;, local;, m;, map;, mkdir;, msgctl;, msgget;, msgrcv;, msgsnd;, opendir;, pack;, pipe;, pop;, pos;, prototype;, push;, q;, qq;, qr;, qx;, read;, readdir;, readline;, readpipe;, recv;, rename;, reset;, reverse;, rewinddir;, rindex;, s;, say;, seek;, seekdir;, select;, semctl;, semget;, semop;, send;, setgrent;, sethostent;, setnetent;, setpgrp;, setpriority;, setprotoent;, setpwent;, setservent;, setsockopt;, shift;, shmctl;, shmget;, shmread;, shmwrite;, shutdown;, socket;, socketpair;, sort;, splice;, split;, sprintf;, state;, study;, substr;, symlink;, syscall;, sysopen;, sysread;, sysseek;, system;, syswrite;, tell;, telldir;, tie;, tied;, time;, times;, tr;, truncate;, unless;, unlink;, unpack;, unshift;, untie;, until;, utime;, values;, vec;, wait;, waitpid;, wantarray;, warn;, when;, write;, y;
__DATA__
AUTOLOAD
CHECK
package
for
abs;
accept;
atan2;
bind;
binmode;
bless;
break;
chmod;
chomp;
chop;
chown;
chr;
closedir;
cmp;
connect;
continue;
crypt;
dbmclose;
dbmopen;
default;
dump;
each;
endgrent;
endhostent;
endnetent;
endprotoent;
endpwent;
endservent;
eof;
evalbytes;
exec;
exp;
fc;
fcntl;
fileno;
flock;
fork;
format;
formline;
getc;
getgrent;
getgrgid;
getgrnam;
gethostbyaddr;
gethostent;
getlogin;
getnetbyaddr;
getnetent;
getpeername;
getppid;
getpriority;
getprotobynumber;
getprotoent;
getpwent;
getpwnam;
getpwuid;
getservbyname;
getservbyport;
getservent;
getsockname;
getsockopt;
given;
grep;
index;
ioctl;
join;
keys;
kill;
link;
listen;
local;
m;
map;
mkdir;
msgctl;
msgget;
msgrcv;
msgsnd;
opendir;
pack;
pipe;
pop;
pos;
prototype;
push;
q;
qq;
qr;
qx;
read;
readdir;
readline;
readpipe;
recv;
rename;
reset;
reverse;
rewinddir;
rindex;
s;
say;
seek;
seekdir;
select;
semctl;
semget;
semop;
send;
setgrent;
sethostent;
setnetent;
setpgrp;
setpriority;
setprotoent;
setpwent;
setservent;
setsockopt;
shift;
shmctl;
shmget;
shmread;
shmwrite;
shutdown;
socket;
socketpair;
sort;
splice;
split;
sprintf;
state;
study;
substr;
symlink;
syscall;
sysopen;
sysread;
sysseek;
system;
syswrite;
tell;
telldir;
tie;
tied;
time;
times;
tr;
truncate;
unless;
unlink;
unpack;
unshift;
untie;
until;
utime;
values;
vec;
wait;
waitpid;
wantarray;
warn;
when;
write;
y;
OP01_NAMED
same as above, except without semicolon
[[[ LEXICON TOKENS, PUNCTUATION & USER-DEFINED WORDS ]]]
COLON
:
LPAREN_MY
(my
LPAREN
(
LBRACKET
[
LBRACE
WORD
lowercase letter followed by optional word characters; or uppercase letter followed by at least one lowercase letter and optional word characters; ex. foo or foo23 or Foo23
foo
foo23
Foo23
WORD_UPPERCASE
single uppercase letter, or uppercase letter followed by uppercase letters, numbers, and underscores; ex. FOO or FOOBAR_42_HOWDY
FOOBAR_42_HOWDY
Operator "arity" is a technical term which means the number of input operands accepted by a specific built-in operator, or the number of input arguments accepted by a user-defined function. An operator or function which accepts 0 input arguments is known as "nullary", 1 argument as "unary", 2 arguments as "binary", 3 arguments as "ternary", and so forth. The exit; operator may be called as nullary; the ++ increment operator is unary; the + addition operator is binary; and the substr operator may be called as ternary. Not to be confused with "a ternary operator", meaning any operator which accepts 3 operands, there is one specific operator known as "the ternary operator", which is a special kind of conditional operator accepting 3 input arguments. An operator or function which may accept more than one number of arguments is known as "variadic". Some RPerl operators are variadic, such as substr which may accept 2, 3, or 4 arguments. RPerl does not currently support variadic user-defined functions.
substr
Operator Arity on Wikipedia
Operator "fixity" is the notation form indicating the location of an operator when placed relative to its own input operands. "Prefix" operators are located before their operands, "infix" between operands, and "postfix" after operands. Additionally, operators which must be placed both before and after their operands are said to be of "closed" fixity, while operators capable of more than one placement location are called "mixfix". Prefix notation is also known as "Polish notation", and postfix is called "Reverse Polish" notation. The abs absolute value is a prefix operator; the + addition operator is infix; and the ++ increment operator can be called as postfix. The -( ) negative-with-parentheses operator is of closed fixity, because the parentheses component must appear both before and after the enclosed operand. Parentheses are always of closed fixity; in normal Perl, the - negative (without parentheses) is a prefix operator, but in RPerl we only allow the closed fixity -( ) negative-with-parentheses operator in order to avoid grammar ambiguity, because the same - dash (AKA hyphen) character is utilized for both the - negative and - subtraction operators. The ++ increment operator may also be called as prefix, so it may be classified as mixfix.
Prefix Notation on Wikipedia
Infix Notation on Wikipedia
Postfix Notation on Wikipedia
Operator "precedence", also known as "order-of-operations", is a methodology used to determine which operator is executed first when 2 or more operators are adjacent to one another and parentheses are not used to explicitly separate them. A numeric precedence from 1 to 24 is assigned to each operator, and the operator with the lowest precedence number is given priority to execute first. Low precedence number equals high priority. The * arithmetic multiplication operator has a precedence number of 7, and + addition has a precedence of 8, so a + b * c is equivalent to a + (b * c), not (a + b) * c.
a + b * c
a + (b * c)
(a + b) * c
Operator Precedence on Wikipedia
Operator "associativity" is used to further determine precedence when multiple operators of the same priority are adjacent to one another. Each operator is designated as left-associative, right-associative, or non-associative. (Wikipedia incorrectly identifies associativity as a synonym for fixity, which is different, as described above.) Normal arithmetic operators are left-associative, meaning a - b - c is equivalent to (a - b) - c, not a - (b - c). Some operators such as mathematic power (AKA exponentiation) are right-associative, meaning a ** b ** c is equivalent to a ** (b ** c). Operators which are not meant to be chained together are non-associative, such as the .. list range operator which takes scalar values as input but generates an array as output, so a .. b .. c is incorrect usage and will cause an error.
a - b - c
(a - b) - c
a - (b - c)
a ** b ** c
a ** (b ** c)
a .. b .. c
Operator Associativity on Wikipedia
In the following list of operators copied directly from Grammar.eyp, later declaration gets higher priority, so all precedence numbers appear in strictly descending order from 24 to 1. (Implementation of operator arity and fixity are a bit less straightforward, and are not easily copied-and-pasted in one succinct list directly out of Grammar.eyp.) All operator arity, fixity, precedence, and associativity are taken directly from Perl 5.
Operator Precedence & Associativity in Perl 5 Documentation [1]
%left OP24_LOGICAL_OR_XOR %left OP23_LOGICAL_AND %right OP22_LOGICAL_NEG %left OP21_LIST_COMMA %left OP20_HASH_FATARROW %right OP19_LOOP_CONTROL_SCOLON %right OP19_LOOP_CONTROL %right OP19_VARIABLE_ASSIGN_BY %right OP19_VARIABLE_ASSIGN %right OP18_TERNARY %nonassoc OP17_LIST_RANGE %left OP16_LOGICAL_OR %left OP15_LOGICAL_AND %left OP14_BITWISE_OR_XOR %left OP13_BITWISE_AND %nonassoc OP12_COMPARE_EQ_NE %nonassoc OP11_COMPARE_LT_GT %nonassoc OP10_NAMED_UNARY %nonassoc OP10_NAMED_UNARY_SCOLON %left OP09_BITWISE_SHIFT %left OP08_STRING_CAT %left OP08_MATH_ADD_SUB %left OP07_MATH_MULT_DIV_MOD %left OP07_STRING_REPEAT %left OP06_REGEX_MATCH %left OP06_REGEX_PATTERN %right OP05_MATH_NEG_LPAREN %right OP05_LOGICAL_NEG %right OP04_MATH_POW %nonassoc OP03_MATH_INC_DEC %left OP02_HASH_THINARROW %left OP02_ARRAY_THINARROW %left OP02_METHOD_THINARROW_NEW %left OP02_METHOD_THINARROW %left OP01_NAMED %left OP01_NAMED_SCOLON %left OP01_CLOSE %left OP01_OPEN %left OP01_QW %left OP01_NAMED_VOID_SCOLON %left OP01_NAMED_VOID_LPAREN %left OP01_NAMED_VOID %left OP01_PRINT
The EBNF metasyntax implemented by Eyapp is of the form:
ProductionRule: First Alternative 'foo' | Second Alternative 'bar' | ... | Last Alternative 'quux' ;
In this example, ProductionRule is a non-terminal left-hand-side (LHS) symbol, is followed by the : reduction metasymbol, and may be reduced (replaced) by any of the right-hand-side (RHS) sequences of terminal and non-terminal symbols, themselves separated by the | alternation (logical or) metasymbol. In other words, each LHS may become any of its corresponding RHS alternatives.
ProductionRule
Terminal symbols are enclosed in single quotes as with 'foo', never appear on the LHS, and are taken as literal data with no transformations applied. Eyapp treats terminal symbols as tokens which only match one hard-coded lexeme, which is the string appearing inside the single quotes, foo in this example.
'foo'
[[[ SYNTAX PRODUCTION RULES, FILE FORMATS ]]]
CompileUnit: Program | (ModuleHeader Module)+ ; Program: SHEBANG Critic? USE_RPERL Header Critic* Include* Constant* Subroutine* Operation+ ; ModuleHeader: Critic? USE_RPERL? 'package' WordScoped ';' Header ; Module: Package | Class ; Package: Critic* Include* Constant* Subroutine+ LITERAL_NUMBER ';' ; Header: 'use strict;' 'use warnings;' USE_RPERL_AFTER? 'our' VERSION_NUMBER_ASSIGN; Critic: '## no critic qw(' WORD+ ')'; Include: USE WordScoped ';' | USE WordScoped OP01_QW ';' ; Constant: 'use constant' WORD_UPPERCASE OP20_HASH_FATARROW TypeInnerConstant Literal ';' ; Subroutine: 'our' Type VARIABLE_SYMBOL '= sub {' SubroutineArguments? Operation* '}' ';' ; SubroutineArguments: LPAREN_MY Type VARIABLE_SYMBOL (OP21_LIST_COMMA MY Type VARIABLE_SYMBOL)* ')' OP19_VARIABLE_ASSIGN '@_;' ; Class: 'use parent qw(' WordScoped ')' ';' Include Critic* Include* Constant* Properties MethodOrSubroutine* LITERAL_NUMBER ';' ; Properties: 'our hashref $properties' OP19_VARIABLE_ASSIGN LBRACE HashEntryProperties (OP21_LIST_COMMA HashEntryProperties)* '}' ';' | 'our hashref $properties' OP19_VARIABLE_ASSIGN LBRACE '}' ';' ; Method: 'our' TYPE_METHOD VARIABLE_SYMBOL '= sub {' MethodArguments? Operation* '}' ';' ; MethodArguments: LPAREN_MY Type SELF (OP21_LIST_COMMA MY Type VARIABLE_SYMBOL)* ')' OP19_VARIABLE_ASSIGN '@_;' ; MethodOrSubroutine: Method | Subroutine;
Code Examples:
CompileUnit
Program
program_00_good.pl
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ OPERATIONS ]]] my integer $i = 2 + 2;
Module
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ INCLUDES ]]] use RPerl::Test::Module::Class_00_Good; # [[[ OPERATIONS ]]] my RPerl::Test::Module::Class_00_Good $foo = RPerl::Test::Module::Class_00_Good->new(); my RPerl::Test::Module::Class_00_Good $bar = RPerl::Test::Module::Class_00_Good->new(); print 'have $foo->{empty_property} = ', $foo->{empty_property}, "\n"; print 'have $bar->{empty_property} = ', $bar->{empty_property}, "\n"; print '...', "\n"; $foo->{empty_property} = 23; print 'have $foo->{empty_property} = ', $foo->{empty_property}, "\n"; print 'have $bar->{empty_property} = ', $bar->{empty_property}, "\n";
Package_00_Good.pm
# [[[ HEADER ]]] use RPerl; package RPerl::Test::Module::Package_00_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ SUBROUTINES ]]] our void $empty_sub = sub { return 2; }; 1; # end of package
Class_00_Good.pm
# [[[ HEADER ]]] use RPerl; package RPerl::Test::Module::Class_00_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ OO INHERITANCE ]]] use parent qw(RPerl::Test); use RPerl::Test; # [[[ OO PROPERTIES ]]] our hashref $properties = { empty_property => my integer $TYPED_empty_property = 2 }; # [[[ OO METHODS & SUBROUTINES ]]] our void::method $empty_method = sub { return 2; }; 1; # end of class
Header
VersionNumber
Critic
program_01_good.pl
#!/usr/bin/perl ## no critic qw(ProhibitUselessNoCritic PodSpelling ProhibitExcessMainComplexity) # DEVELOPER DEFAULT 1a: allow unreachable & POD-commented code; SYSTEM SPECIAL 4: allow complex code outside subroutines, must be on line 1 # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic) # [[[ OPERATIONS ]]] my integer $i = 2 + 2;
# [[[ HEADER ]]] use RPerl; package RPerl::Test::Critic::Package_00_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ SUBROUTINES ]]] our void $empty_sub = sub { return 2; }; 1; # end of package
# [[[ HEADER ]]] use RPerl; package RPerl::Test::Critic::Class_00_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ OO INHERITANCE ]]] use parent qw(RPerl::Test); use RPerl::Test; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ OO PROPERTIES ]]] our hashref $properties = { empty_property => my integer $TYPED_empty_property = 2 }; # [[[ OO METHODS & SUBROUTINES ]]] our void::method $empty_method = sub { return 2; }; 1; # end of class
Include
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ INCLUDES ]]] use RPerl::Test::Foo; use RPerl::Test::Bar; # [[[ OPERATIONS ]]] my integer $i = 2 + 2;
# [[[ HEADER ]]] use RPerl; package RPerl::Test::Include::Package_00_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ INCLUDES ]]] use RPerl::Test::Foo; use RPerl::Test::Bar; # [[[ SUBROUTINES ]]] our integer $empty_sub = sub { return 2; }; 1; # end of package
# [[[ HEADER ]]] use RPerl; package RPerl::Test::Include::Class_00_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ OO INHERITANCE ]]] use parent qw(RPerl::Test); use RPerl::Test; # [[[ INCLUDES ]]] use RPerl::Test::Foo; use RPerl::Test::Bar; # [[[ OO PROPERTIES ]]] our hashref $properties = { empty_property => my integer $TYPED_empty_property = 2 }; # [[[ OO METHODS & SUBROUTINES ]]] our integer::method $empty_method = sub { return 2; }; 1; # end of class
Constant
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CONSTANTS ]]] ## no critic qw(ProhibitConstantPragma ProhibitMagicNumbers) # USER DEFAULT 3: allow constants use constant PI => my number $TYPED_PI = 3.141_59; use constant PIE => my string $TYPED_PIE = 'pecan'; # [[[ OPERATIONS ]]] my integer $i = 2 + 2;
# [[[ HEADER ]]] use RPerl; package RPerl::Test::Constant::Package_00_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CONSTANTS ]]] ## no critic qw(ProhibitConstantPragma ProhibitMagicNumbers) # USER DEFAULT 3: allow constants use constant PI => my number $TYPED_PI = 3.141_59; use constant PIE => my string $TYPED_PIE = 'pecan'; # [[[ SUBROUTINES ]]] our void $empty_sub = sub { return 2; }; 1; # end of package
# [[[ HEADER ]]] use RPerl; package RPerl::Test::Constant::Class_00_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ OO INHERITANCE ]]] use parent qw(RPerl::Test); use RPerl::Test; # [[[ CONSTANTS ]]] ## no critic qw(ProhibitConstantPragma ProhibitMagicNumbers) # USER DEFAULT 3: allow constants use constant PI => my number $TYPED_PI = 3.141_59; use constant PIE => my string $TYPED_PIE = 'pecan'; # [[[ OO PROPERTIES ]]] our hashref $properties = { empty_property => my integer $TYPED_empty_property = 2 }; # [[[ OO METHODS & SUBROUTINES ]]] our void::method $empty_method = sub { return 2; }; 1; # end of class
Subroutine
SubroutineArguments
# [[[ HEADER ]]] use RPerl; package RPerl::Test::SubroutineArguments::Package_00_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ SUBROUTINES ]]] our void $empty_sub = sub { ( my number $foo ) = @_; return 1; }; 1; # end of package
Class
Properties
class_00_good_02.pl
#!/usr/bin/perl # [[[ PREPROCESSOR ]]] # <<< EXECUTE_SUCCESS: '92' >>> # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.000_010; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ INCLUDES ]]] use RPerl::Test::Properties::Class_00_Good; # [[[ OPERATIONS ]]] # set and get object property ourself # using direct access to blessed $property hashref, breaking object encapsulation my RPerl::Test::Properties::Class_00_Good $test_object = RPerl::Test::Properties::Class_00_Good->new(); $test_object->{test_property} = 4; $test_object->test_method(23); print $test_object->{test_property} . "\n";
# [[[ HEADER ]]] use RPerl; package RPerl::Test::Properties::Class_00_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ OO INHERITANCE ]]] use parent qw(RPerl::Test); use RPerl::Test; # [[[ OO PROPERTIES ]]] our hashref $properties = { test_property => my integer $TYPED_test_property = 2 }; # [[[ OO METHODS & SUBROUTINES ]]] our void::method $test_method = sub { ( my object $self, my integer $input_integer ) = @_; $self->{test_property} *= $input_integer; return $self->{test_property}; }; 1; # end of class
Method
MethodArguments
Class_01_Good.pm
# [[[ HEADER ]]] use RPerl; package RPerl::Test::MethodArguments::Class_01_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ OO INHERITANCE ]]] use parent qw(RPerl::Test); use RPerl::Test; # [[[ OO PROPERTIES ]]] our hashref $properties = { empty_property => my integer $TYPED_empty_property = 2 }; # [[[ OO METHODS & SUBROUTINES ]]] our void::method $empty_method = sub { ( my object $self, my integer $foo ) = @_; return 2; }; 1; # end of class
[[[ SYNTAX PRODUCTION RULES, OPERATIONS ]]]
Operation: Expression ';' | OP01_NAMED_SCOLON | OP10_NAMED_UNARY_SCOLON | Statement ; Operator: LPAREN OP01_PRINT FHREF_SYMBOL_BRACES ListElements ')' | OP01_NAMED SubExpression | LPAREN OP01_NAMED ListElement OP21_LIST_COMMA ListElements ')' | OP01_OPEN MY TYPE_FHREF FHREF_SYMBOL OP21_LIST_COMMA LITERAL_STRING OP21_LIST_COMMA SubExpression | OP01_CLOSE FHREF_SYMBOL | OP03_MATH_INC_DEC Variable | Variable OP03_MATH_INC_DEC | SubExpression OP04_MATH_POW SubExpression | OP05_LOGICAL_NEG SubExpression | OP05_MATH_NEG_LPAREN SubExpression ')' | SubExpression OP06_REGEX_MATCH OP06_REGEX_PATTERN | SubExpression OP07_STRING_REPEAT SubExpression | SubExpression OP07_MATH_MULT_DIV_MOD SubExpression | SubExpression OP08_MATH_ADD_SUB SubExpression | SubExpression OP08_STRING_CAT SubExpression | SubExpression OP09_BITWISE_SHIFT SubExpression | OP10_NAMED_UNARY SubExpression | OP10_NAMED_UNARY | SubExpression OP11_COMPARE_LT_GT SubExpression | SubExpression OP12_COMPARE_EQ_NE SubExpression | SubExpression OP13_BITWISE_AND SubExpression | SubExpression OP14_BITWISE_OR_XOR SubExpression | SubExpression OP15_LOGICAL_AND SubExpression | SubExpression OP16_LOGICAL_OR SubExpression | SubExpression OP17_LIST_RANGE SubExpression | SubExpression OP18_TERNARY VariableOrLiteral COLON VariableOrLiteral | OP22_LOGICAL_NEG SubExpression | SubExpression OP23_LOGICAL_AND SubExpression | SubExpression OP24_LOGICAL_OR_XOR SubExpression ; OperatorVoid: OP01_PRINT (STDOUT_STDERR)? ListElements ';' | OP01_PRINT FHREF_SYMBOL_BRACES ListElements ';' | OP01_NAMED_VOID_SCOLON | OP01_NAMED_VOID_LPAREN ListElements? ')' ';' | OP01_NAMED_VOID ListElements ';' | OP01_NAMED ListElement OP21_LIST_COMMA ListElements ';' | OP19_LOOP_CONTROL_SCOLON | OP19_LOOP_CONTROL LoopLabel ';' ; Expression: Operator | WORD_UPPERCASE LPAREN ')' | CONSTANT_CALL_SCOPED | WordScoped LPAREN ListElements? ')' | Variable OP02_METHOD_THINARROW LPAREN ListElements? ')' | WordScoped OP02_METHOD_THINARROW_NEW ')' ; SubExpression: Expression | 'undef' | Literal | Variable | ArrayReference | ArrayDereference | HashReference | HashDereference | LPAREN SubExpression ')' ; SubExpressionOrInput: SubExpression | FHREF_SYMBOL_IN | STDIN; SubExpressionOrVarMod: SubExpression | VariableModification; Statement: Conditional | (LoopLabel COLON)? Loop | OperatorVoid | VariableDeclaration | VariableModification ';' ; Conditional: 'if' LPAREN SubExpression ')' CodeBlock ('elsif' LPAREN SubExpression ')' CodeBlock)* ('else' CodeBlock)? ; Loop: LoopFor | LoopForEach | LoopWhile ; LoopFor: 'for' MY TYPE_INTEGER VARIABLE_SYMBOL LPAREN SubExpression OP17_LIST_RANGE SubExpression ')' CodeBlock | 'for' LPAREN_MY TYPE_INTEGER VARIABLE_SYMBOL OP19_VARIABLE_ASSIGN OpNamedScolonOrSubExp VARIABLE_SYMBOL OP11_COMPARE_LT_GT OpNamedScolonOrSubExp SubExpressionOrVarMod ')' CodeBlock ; LoopForEach: 'foreach' MY Type VARIABLE_SYMBOL LPAREN ListElements ')' CodeBlock ; LoopWhile: 'while' LPAREN SubExpression ')' CodeBlock | 'while' LPAREN_MY Type VARIABLE_SYMBOL OP19_VARIABLE_ASSIGN SubExpressionOrInput ')' CodeBlock; CodeBlock: LBRACE Operation+ '}' ;
Operation
Expression
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer $i = 0; $i++;
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] our integer $foo = sub { return 23; }; foo();
program_10_good.pl
#!/usr/bin/perl # [[[ PREPROCESSOR ]]] # <<< EXECUTE_SUCCESS: '1.570795' >>> # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ INCLUDES ]]] use RPerl::Test::Foo; # [[[ OPERATIONS ]]] my number $pi_over_2 = RPerl::Test::Foo::PI() / 2; print 'pi / 2 = ', $pi_over_2, "\n";
program_21_good.pl
#!/usr/bin/perl # [[[ PREPROCESSOR ]]] # <<< EXECUTE_SUCCESS: "'a' => 'twenty-threetwenty-threetwenty-threetwenty-threetwenty-threetwenty-threetwenty-three'" >>> # <<< EXECUTE_SUCCESS: "'b' => 'howdy'" >>> # <<< EXECUTE_SUCCESS: "'c' => '-23.42'" >>> # <<< EXECUTE_SUCCESS: "'a' => 'guffawguffawguffawguffawguffawguffawguffawguffawguffawguffawguffaw'" >>> # <<< EXECUTE_SUCCESS: "'b' => 'howdy'" >>> # <<< EXECUTE_SUCCESS: "'c' => '-23.42'" >>> # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ INCLUDES ]]] use RPerl::Test::Foo; # [[[ OPERATIONS ]]] my RPerl::Test::Foo $my_foo = RPerl::Test::Foo->new(); print Dumper($my_foo->qorge(7)) . "\n"; $my_foo->{xyzzy} = 'guffaw'; print Dumper($my_foo->qorge(11)) . "\n";
program_30_good.pl
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $foo = 9_123.456_789; my number_arrayref $bar = [ 21, 12, 43.23, 987.654_321 ]; my string $bat = number_to_string($foo); my string $baz = ::number_arrayref_to_string($bar); my string $bax = ::string_arrayref_to_string( [ 'abc', '123', 'lalala' ] ); print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $bat = ::integer_hashref_to_string( { foo => 23, bar => 56, quux => 123_456 } ); $baz = ::number_hashref_to_string( { foo => 23.4, bar => 56.789, quux => 123_456.789_012 } ); $bax = ::string_hashref_to_string( { foo => 'howdy', bar => 'dowdy', quux => 'doo' } ); print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator01Named
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $op_sin = sin 2; my number $op_cos = cos 2; my integer_arrayref $frob = []; my integer $frob_length = ( push @{$frob}, 21, 12, 23 ); # Operator, yes parentheses required for built-in w/ multiple arguments in non-void context print 'have $frob_length = ', $frob_length, "\n"; print 'have $frob = ', "\n", Dumper($frob), "\n"; my integer $frob_pop = pop @{$frob}; print 'have $frob_pop = ', "\n", $frob_pop, "\n"; print 'have $frob = ', "\n", Dumper($frob), "\n";
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ OPERATIONS ]]] wait;
program_02_good.pl
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ OPERATIONS ]]] my unknown $op_wait = wait;
Operator10NamedUnary
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer $foo = chdir; my integer $bar = chdir 'INVALID__DIRECTORY__NAME'; my number $bat = rand; my number $baz = rand 10; my number $bax = rand 30; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; my integer_arrayref $quux = [ 5, 6, 7, 8 ]; $foo = scalar @{$quux}; $bar = scalar @{ [] }; $bat = scalar @{ [0] }; $baz = scalar @{ [ 0, 1, 2, 3, 4, 5, 6, 7 ] }; $bax = scalar @{ [ 0 .. 22 ] }; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ OPERATIONS ]]] chdir;
Statement
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] if (1) { print 'Production rule Statement matched by Conditional', "\n"; }
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] for my integer $i ( -1 .. 1 ) { print 'Production rule Statement matched by Loop, iteration number ', $i, "\n"; }
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] foreach my number $my_number ( 11.1, 22.2, 33.3, 44.4 ) { print 'Production rule Statement matched by Loop, iteration item ', $my_number, "\n"; }
program_03_good.pl
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer $i = 10; FOOBARLOOP: while ( $i < 23 ) { print 'Production rule Statement matched by Loop, iteration number ', $i, "\n"; $i += 2; if ( $i > 18 ) { last FOOBARLOOP; } }
program_04_good.pl
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] print 'Production rule Statement matched by OperatorVoid', "\n";
program_05_good.pl
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my string $foo = 'Production rule Statement matched by VariableDeclaration';
program_06_good.pl
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my string $foo = 'Production rule Statement matched by'; $foo .= ' VariableModification';
Operator
Operator01Print
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ OPERATIONS ]]] # USER NOTE: the Operator (not OperatorVoid) production rule matched by 'print' requires parentheses and filehandle my integer $open_success = open my filehandleref $HOWDY, '>', '/tmp/howdy'; if ( not $open_success ) { croak 'ERROR: Failed to open file /tmp/howdy for writing, croaking'; } # USER NOTE: return values of Operator print are purposefully ignored here, possibly ignoring write errors ( print {$HOWDY} 2 ); ( print {$HOWDY} 2, 3, 4, "\n" ); ( print {$HOWDY} 2.31 ); ( print {$HOWDY} 2.31, 3.21, 4.23, "\n" ); ( print {$HOWDY} 'howdy' ); ( print {$HOWDY} 'howdy', 'doody', 'foobar', "\n" ); ( print {$HOWDY} 'howdy', 2.31, 'doody', 3.21, 'foobar', 4.23, "\n" ); # USER NOTE: return values of Operator print are utilized here, catching write errors # long format, 2 RPerl Operations my integer $print_success = ( print {$HOWDY} 2 ); if ( not $print_success ) { croak 'ERROR: Failed to write to file /tmp/howdy, croaking'; } # short format, 1 RPerl Operation if (not( print {$HOWDY} 'howdy', 2.31, 'doody', 3.21, 'foobar', 4.23, "\n" ) ) { croak 'ERROR: Failed to write to file /tmp/howdy, croaking'; } if ( not close $HOWDY ) { croak 'ERROR: Failed to close file /tmp/howdy after writing, croaking'; }
Operator01OpenClose
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ OPERATIONS ]]] my integer $open_success = open my filehandleref $HOWDY, '>', '/tmp/howdy'; if ( not $open_success ) { croak 'ERROR: Failed to open file /tmp/howdy for writing, croaking'; } my integer $print_success = ( print {$HOWDY} 23, 32, 42 ); if ( not $print_success ) { croak 'ERROR: Failed to write to file /tmp/howdy, croaking'; } if ( not close $HOWDY ) { croak 'ERROR: Failed to close file /tmp/howdy after writing, croaking'; }
Operator03MathIncrementDecrement
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer $foo = 2; my integer $bar = 3; $foo++; my integer $bat = $foo++; $bar--; my integer $baz = $bar--; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; ++$foo; $bat = ++$foo; --$bar; $baz = --$bar; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n";
Operator04MathPower
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer $foo = 2**3; my integer $bar = 3**2; my integer $bat = $foo**$bar; my integer $baz = $bar**$foo; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; $foo = 2; $bar = 3; $bat = ( $foo**2 )**$bar; $baz = $foo**( 2**$bar ); my integer $bax = $foo**2**$bar; # right associative print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator05LogicalNegation
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer $foo = 0; my integer $bar = 1; my integer $bat = !$foo; my integer $baz = !($bar); print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; $foo = 2; $bar = 3; $bat = !2; # returns empty string '' $baz = !( $foo - $bar ); # returns empty string '' my integer $bax = !!$foo; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator05MathNegative
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer $foo = 3; my integer $bar = -3; # Literal Number, not Operator my integer $bat = -(3); # Operator(Literal Number) my integer $baz = 5 - -($bar); print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; $foo = -(-3); # Operator(Literal Number) $bar = -( -(3) ); # Operator(Operator(Literal Number)) $bat = $foo + -($bar); $baz = $foo - -($bar); print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n";
Operator06RegularExpressionMatch
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my string $foo = 'howdy hello ahoy'; my integer $bar = $foo =~ m/owdy/msx; my integer $bat = ( $foo =~ m/Hello/msx ); my integer $baz = ( $foo =~ m/\s[Aa]hoy$/msx ); my integer $bax = ( $foo =~ s/ho/HO/gms ); print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $foo = 'Alpha Bravo Charlie 123'; $bar = ( $foo !~ m/owdy/msx ); $bat = ( $foo !~ m/ravo/msx ); $baz = ( $foo !~ m/\s[Cc]harlie\s\d*$/msx ); $bax = ( $foo !~ s/ha/HAHA/gms ); print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator07StringRepeat
#!/usr/bin/perl # [[[ PREPROCESSOR ]]] # <<< EXECUTE_SUCCESS: "have $foo = ''" >>> # <<< EXECUTE_SUCCESS: "have $bar = 'bar'" >>> # <<< EXECUTE_SUCCESS: "have $bat = 'batbat'" >>> # <<< EXECUTE_SUCCESS: "have $baz = 'bazbazbazbaz'" >>> # <<< EXECUTE_SUCCESS: "have $bax = 'baxbaxbaxbaxbaxbaxbaxbax'" >>> # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my string $foo = 'foo' x 0; my string $bar = 'bar' x 1; my string $bat = 'bat' x 2; my string $baz = 'baz' x 4; my string $bax = 'bax' x 8; print q{have $foo = '}, $foo, q{'}, "\n"; print q{have $bar = '}, $bar, q{'}, "\n"; print q{have $bat = '}, $bat, q{'}, "\n"; print q{have $baz = '}, $baz, q{'}, "\n"; print q{have $bax = '}, $bax, q{'}, "\n";
Operator07MathMultiplyDivideModulo
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $foo = 222 * -33; my number $bar = 222 / 33; my number $bat = 222 % 33; my number $baz = $foo % $bar * $bat / 4; my number $bax = $foo % ( $bar * ( $bat / 4 ) ); print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator08AddSubtract
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $foo = 222 + 33; my number $bar = 222 - 33; my number $bat = 222 + -33; my number $baz = $foo - $bar + $bat - 4; my number $bax = $foo - ( $bar + ( $bat - 4 ) ); print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator08StringCat
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my string $foo = 'howdy' . 'doody'; my string $bar = 'hello' . ('and' . 'goodbye'); my string $bat = ('ahoy' . 'matey') . 'yarr'; my string $baz = $foo . $bar . $bat; my string $bax = $foo . $foo . $foo; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator09BitwiseShift
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer $foo = 1_024; my integer $bar = 256; my integer $bat = 4_096 << 3; my integer $baz = $foo << 3; my integer $bax = $bar >> 4; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator11CompareLessThanGreaterThan
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $foo = 9_123.456_789; my string $bar = 'howdy'; my integer $bat = $foo < 22; my integer $baz = 22 < $foo; my integer $bax = 33 <= 44; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $bat = $foo > 333_333_333_333; $baz = 444 >= 222; $bax = 9_123.456_789 >= $foo; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $bat = $bar lt 'ahoy'; $baz = 'ZZZZZZZZZZZZZZ' lt $bar; $bax = '33' le '44'; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $bat = $bar gt 'abc'; $baz = '144' ge '222'; $bax = 'howdy' ge $bar; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator12CompareEqualNotEqual
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $foo = 9_123.456_789; my string $bar = 'howdy'; my integer $bat = $foo == 22; my integer $baz = 9_123.456_789 == $foo; my integer $bax = 33 != 44; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $bat = $foo == 333_333_333_333; $baz = 444 != 222; $bax = 33 != $foo; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $bat = $bar eq 'howdy'; $baz = 'ZZZZZZZZZZZZZZ' eq $bar; $bax = '33' ne '44'; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $bat = $bar eq 'abc'; $baz = '444' ne '222'; $bax = '33' ne $bar; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator13BitwiseAnd
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $foo = 9_123.456_789; my string $bar = 'howdy'; my integer $bat = $foo & 22; my integer $baz = 9_123.456_789 & $foo; my integer $bax = 33 & 44; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $bat = $bar & 'abc'; $baz = '444' & '222'; $bax = '33' & $bar; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator14BitwiseOrXor
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $foo = 9_123.456_789; my string $bar = 'howdy'; my integer $bat = $foo | 22; my integer $baz = 9_123.456_789 | $foo; my integer $bax = 33 ^ 44; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $bat = $bar | 'abc'; $baz = '444' ^ '222'; $bax = '33' ^ $bar; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator15LogicalAnd
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $foo = 9_123.456_789; my string $bar = 'howdy'; my integer $bat = $foo && 22; my integer $baz = 9_123.456_789 && $foo; my integer $bax = 33 && 0; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $bat = $bar && q{}; $baz = '444' && '222'; $bax = '33' && $bar; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator16LogicalOr
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $foo = 9_123.456_789; my string $bar = 'howdy'; my integer $bat = $foo || 22; my integer $baz = 9_123.456_789 || $foo; my integer $bax = 33 || 0; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $bat = $bar || q{}; $baz = '444' || '222'; $bax = '33' || $bar; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator17ListRange
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $foo = 123; my string $bar = 'ab'; my integer_arrayref $bat; $bat = [ $foo .. 22 ]; my integer_arrayref $baz; $baz = [ 22 .. $foo ]; my integer_arrayref $bax; $bax = [ 0 .. 23 ]; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', ::integer_arrayref_to_string($bat), "\n"; print 'have $baz = ', ::integer_arrayref_to_string($baz), "\n"; print 'have $bax = ', ::integer_arrayref_to_string($bax), "\n"; my string_arrayref $bat2; $bat2 = [ $bar .. 'ac' ]; my string_arrayref $baz2; $baz2 = [ 'a2' .. 'a4' ]; my string_arrayref $bax2; $bax2 = [ 'a' .. $bar ]; print 'have $bat2 = ', ::string_arrayref_to_string($bat2), "\n"; print 'have $baz2 = ', ::string_arrayref_to_string($baz2), "\n"; print 'have $bax2 = ', ::string_arrayref_to_string($bax2), "\n";
Operator18Ternary
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $foo = 9_123.456_789; my number $bar = 2_112; my number $bat = ( 22 > 21 ) ? $foo : $bar; my number $baz = 0 ? $foo : $bar; my number $bax = 1 ? 99.9 : 100.1; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $bat = ( 'howdy' ne 'dowdy' ) ? $foo : 22_222.33; $baz = ( 4 <= 4 ) ? 88 : $bar; $bax = ( 88 > 99 ) ? 21 : 12; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator22LogicalNegation
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer $foo = 0; my integer $bar = 1; my integer $bat = not $foo; my integer $baz = not ($bar); print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; $foo = 2; $bar = 3; $bat = not 2; # returns empty string '' $baz = not ( $foo - $bar ); # returns empty string '' my integer $bax = not not $foo; print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator23LogicalAnd
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $foo = 9_123.456_789; my string $bar = 'howdy'; my integer $bat = ($foo and 22); my integer $baz = (9_123.456_789 and $foo); my integer $bax = (33 and 0); print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $bat = ($bar and q{}); $baz = ('444' and '222'); $bax = ('33' and $bar); print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
Operator24LogicalOrXor
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $foo = 9_123.456_789; my string $bar = 'howdy'; my integer $bat = ( $foo or 22 ); my integer $baz = ( 9_123.456_789 or $foo ); my integer $bax = ( 33 xor 0 ); print 'have $foo = ', $foo, "\n"; print 'have $bar = ', $bar, "\n"; print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n"; $bat = ( $bar or q{} ); $baz = ( '444' xor '222' ); $bax = ( '33' xor $bar ); print 'have $bat = ', $bat, "\n"; print 'have $baz = ', $baz, "\n"; print 'have $bax = ', $bax, "\n";
OperatorVoid
OperatorVoid01Print
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ OPERATIONS ]]] # USER NOTE: the OperatorVoid (not Operator) production rule matched by 'print' has optional STDOUT, STDERR, or filehandle print 2; print 2, 3, 4, "\n"; print 2.31; print 2.31, 3.21, 4.23, "\n"; print 'howdy'; print 'howdy', 'doody', 'foobar', "\n"; print 'howdy', 2.31, 'doody', 3.21, 'foobar', 4.23, "\n"; print {*STDOUT} 2; print {*STDOUT} 2, 3, 4, "\n"; print {*STDOUT} 2.31; print {*STDOUT} 2.31, 3.21, 4.23, "\n"; print {*STDOUT} 'howdy'; print {*STDOUT} 'howdy', 'doody', 'foobar', "\n"; print {*STDOUT} 'howdy', 2.31, 'doody', 3.21, 'foobar', 4.23, "\n"; print {*STDERR} 2; print {*STDERR} 2, 3, 4, "\n"; print {*STDERR} 2.31; print {*STDERR} 2.31, 3.21, 4.23, "\n"; print {*STDERR} 'howdy'; print {*STDERR} 'howdy', 'doody', 'foobar', "\n"; print {*STDERR} 'howdy', 2.31, 'doody', 3.21, 'foobar', 4.23, "\n"; my integer $open_success = open my filehandleref $HOWDY, '>', '/tmp/howdy'; if ( not $open_success ) { croak 'ERROR: Failed to open file /tmp/howdy for writing, croaking'; } # USER NOTE: return values of OperatorVoid print-to-filehandle are always ignored, possibly ignoring write errors print {$HOWDY} 2; print {$HOWDY} 2, 3, 4, "\n"; print {$HOWDY} 2.31; print {$HOWDY} 2.31, 3.21, 4.23, "\n"; print {$HOWDY} 'howdy'; print {$HOWDY} 'howdy', 'doody', 'foobar', "\n"; print {$HOWDY} 'howdy', 2.31, 'doody', 3.21, 'foobar', 4.23, "\n"; if ( not close $HOWDY ) { croak 'ERROR: Failed to close file /tmp/howdy after writing, croaking'; }
OperatorVoid01Named
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer_arrayref $frob = []; push @{$frob}, 21, 12, 23; # OperatorVoid, no parentheses required for built-in w/ multiple arguments in void context print 'have $frob = ', "\n", Dumper($frob), "\n";
OperatorVoid01NamedVoidCroak
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] croak;
OperatorVoid01NamedVoidDie
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] die "\n";
OperatorVoid01NamedVoidExit
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] exit;
OperatorVoid01NamedVoidReturn
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ SUBROUTINES ]]] # DEV NOTE: DO NOT RUN PERLTIDY ON THIS FILE! the line below should read 'return();' not 'return ();' our void $foo = sub { return(); }; # [[[ OPERATIONS ]]] foo();
Operator19LoopControl
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] HOWDYLOOP: while (1) { next HOWDYLOOP; }
SubExpression
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer $foo = undef;
program_07_good.pl
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my string_hashref $foo = { puppet => 'howdy doody', host => 'buffalo bob', sidekick => 'clarabell clown' }; keys %{$foo};
LiteralNumber
Package_30_Good.pm
# [[[ HEADER ]]] use RPerl; package RPerl::Test::LiteralNumber::Package_30_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ SUBROUTINES ]]] our number $empty_sub = sub { return 0.234_567_89; }; 1; # end of package
LiteralString
Package_DoubleQuotes_10_Good.pm
# [[[ HEADER ]]] use RPerl; package RPerl::Test::LiteralString::Package_DoubleQuotes_10_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ SUBROUTINES ]]] our string $empty_sub = sub { return "{foo}\n"; }; 1; # end of package
Package_SingleQ_07_Good.pm
# [[[ HEADER ]]] use RPerl; package RPerl::Test::LiteralString::Package_SingleQ_07_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ SUBROUTINES ]]] our string $empty_sub = sub { return q{@ $}; }; 1; # end of package
Package_SingleQuotes_07_Good.pm
# [[[ HEADER ]]] use RPerl; package RPerl::Test::LiteralString::Package_SingleQuotes_07_Good; use strict; use warnings; our $VERSION = 0.001_000; # [[[ SUBROUTINES ]]] our string $empty_sub = sub { return '@ $'; }; 1; # end of package
ArrayReference
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] # homogeneous 2-dimensional array of arrays, using inner types my arrayref_arrayref $array_array = [ my integer_arrayref $TYPED_array_array_0 = [ 17, -23, 1_701 ], my integer_arrayref $TYPED_array_array_1 = [ -1, 444, 2_987 ], my integer_arrayref $TYPED_array_array_2 = [ -321, 7, 24_851 ] ]; foreach my arrayref $array ( @{$array_array} ) { print '$array = ', Dumper($array), "\n"; }
program_15_good.pl
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my string_arrayref $s_array = ['buffalo', qw(alpha beta gamma), 'howdy']; foreach my string $s ( @{$s_array} ) { print '$s = ', $s, "\n"; }
ArrayDereference
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] foreach my integer $i ( @{ [ 10, 20, 30, 40, 50 ] } ) { print '$i = ', $i, "\n"; }
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] foreach my integer $i ( @{ my integer_arrayref $TYPED_i_array = [ 10, 20, 30, 40, 50 ] } ) { print '$i = ', $i, "\n"; }
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] foreach my integer $i ( @{ [ my integer $TYPED_i0 = 10, 20, 30, 40, 50 ] } ) { print '$i = ', $i, "\n"; }
HashReference
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] # homogeneous 2-dimensional hash of hashes, using inner types my hashref_hashref $hash_hash = { key_0 => my integer_hashref $TYPED_key_0 = { a => 17, b => -23, c => 1_701 }, key_1 => my integer_hashref $TYPED_key_1 = { d => -1, e => 444, f => 2_987 }, key_2 => my integer_hashref $TYPED_key_2 = { g => -321, h => 7, i => 24_851 } }; print Dumper($hash_hash);
program_14_good.pl
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my number $key_number = 0.1; my string $key_string = '0.1'; my hashref $unknown_hash = { key0 => my integer $TYPED_key0 = -23, 0 => my number_arrayref $TYPED_zero = [ 42 / 1_701, 21.12, 2_112.23 ], 0.1 => my string_hashref $TYPED_zerodotone = { 'alpha' => 'strings are scalars, too', 12.345_678 => 'hello world', gamma => 'last one' }, '0.1' => 'replacement', "0.1\n" => 'close but not quite', $key_number => 'another replacement', $key_string => 'final replacement' }; print Dumper($unknown_hash);
HashDereference
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] foreach my string $key ( sort keys %{ { a => 10, b => 20, c => 30, d => 40, e => 50 } } ) { print '$key = ', $key, "\n"; }
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] foreach my string $key ( sort keys %{ my integer_hashref $TYPED_i_hash = { a => 10, b => 20, c => 30, d => 40, e => 50 } } ) { print '$key = ', $key, "\n"; }
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] foreach my string $key ( sort keys %{ { a => my integer $TYPED_a = 10, b => 20, c => 30, d => 40, e => 50 } } ) { print '$key = ', $key, "\n"; }
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] foreach my string $key ( sort keys %{ my integer_hashref $TYPED_i_hash = { a => my integer $TYPED_a = 10, b => 20, c => 30, d => 40, e => 50 } }) { print '$key = ', $key, "\n"; }
SubExpressionOrInput
SubExpressionOrStdin
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils ## no critic qw(ProhibitExplicitStdin) # USER DEFAULT 4: allow <STDIN> prompt # [[[ OPERATIONS ]]] my string $foo; $foo = <STDIN>;
Conditional
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] if (0) { print 'yes if', "\n"; } elsif (0) { print 'yes elsif 0', "\n"; } elsif (0) { print 'yes elsif 1', "\n"; } else { print 'yes else', "\n"; }
Loop
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] TESTFORLOOP: for my unsigned_integer $i ( 0 .. 4 ) { print 'Production rule Loop matched by LoopFor, iteration number ', $i, "\n"; if ( $i > 1 ) { last TESTFORLOOP; } }
program_23_good.pl
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] TESTFOREACHLOOP: foreach my number $my_number ( 1.1, 2.2, 3.3, 4.4 ) { if ( $my_number == 3.3 ) { next TESTFOREACHLOOP; } print 'Production rule Loop matched by LoopForEach, iteration item ', $my_number, "\n"; }
program_32_good.pl
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer $i = 0; TESTWHILELOOP: while ( $i < 7 ) { $i++; if ( $i == 3 ) { next TESTWHILELOOP; } print 'Production rule Loop matched by LoopWhile, iteration item ', $i, "\n"; }
VariableDeclaration
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my unknown $foo;
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my string_hashref $s_hash = { a => 'howdy', b => 'doody', c => 'clarabell' };
#!/usr/bin/perl # [[[ PREPROCESSOR ]]] # <<< EXECUTE_SUCCESS: 'size of $foo is 10' >>> # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer $size_max_half = 5; my number_arrayref $foo->[($size_max_half * 2) - 1] = undef; print 'size of $foo is ' . ( scalar @{$foo} ) . "\n";
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ OPERATIONS ]]] my filehandleref $FOO_FH;
program_40_good.pl
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator # [[[ INCLUDES ]]] use RPerl::Test; # [[[ OPERATIONS ]]] my RPerl::Test $foo = RPerl::Test->new();
VariableModification
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my integer_arrayref $i_array; $i_array = [ -20, -15, -10, -5, 0, 5, 10, 15, 20 ];
#!/usr/bin/perl # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my unknown $foo; $foo = [ 'strange', { weird => 'goofy', beard => [ 'goatee', -17.01 ] }, 23.42, 'heterogeneous' ];
[[[ SYNTAX PRODUCTION RULES, VARIABLE DATA ]]]
Variable: VariableSymbolOrSelf VariableRetrieval* ; VariableRetrieval: OP02_ARRAY_THINARROW SubExpression ']' | OP02_HASH_THINARROW SubExpression '}' | OP02_HASH_THINARROW WORD '}' ; VariableDeclaration: MY Type VARIABLE_SYMBOL ';' | MY Type VARIABLE_SYMBOL OP19_VARIABLE_ASSIGN OpNamedScolonOrSubExpIn | MY Type VARIABLE_SYMBOL OP02_ARRAY_THINARROW SubExpression ']' OP19_VARIABLE_ASSIGN 'undef' ';' | MY TYPE_FHREF FHREF_SYMBOL ';' ; VariableModification: Variable OP19_VARIABLE_ASSIGN SubExpressionOrInput | Variable OP19_VARIABLE_ASSIGN_BY SubExpression ; ListElements: ListElement (OP21_LIST_COMMA ListElement)* ; ListElement: SubExpression | TypeInner SubExpression | OP01_QW | ARGV; ArrayReference: LBRACKET ListElements? ']' ; ArrayDereference: '@{' Variable '}' | '@{' TypeInner? ArrayReference '}' ; HashEntry: VarOrLitOrOpStrOrWord OP20_HASH_FATARROW TypeInner? SubExpression | HashDereference | ENV ; HashEntryProperties: OpStringOrWord OP20_HASH_FATARROW TypeInnerProperties ; HashReference: LBRACE HashEntry (OP21_LIST_COMMA HashEntry)* '}' | LBRACE '}' ; HashDereference: '%{' Variable '}' | '%{' TypeInner? HashReference '}' ;
ListElement
Operator01QuoteWord
#!/usr/bin/perl # [[[ PREPROCESSOR ]]] # <<< EXECUTE_SUCCESS: 'foo' >>> # <<< EXECUTE_SUCCESS: 'quux' >>> # <<< EXECUTE_SUCCESS: 'zorg' >>> # <<< EXECUTE_SUCCESS: 'munge' >>> # <<< EXECUTE_SUCCESS: 'frob' >>> # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my string_arrayref $s_array = [qw(foo quux zorg munge frob)]; foreach my string $s ( @{$s_array} ) { print '$s = ', $s, "\n"; }
#!/usr/bin/perl # [[[ PREPROCESSOR ]]] # <<< EXECUTE_SUCCESS: '-1bar-5bat' >>> # <<< EXECUTE_SUCCESS: 'qu23ux' >>> # <<< EXECUTE_SUCCESS: 'zorg0+blop+0frun' >>> # <<< EXECUTE_SUCCESS: 'munge4/4sqap6/6cruft' >>> # <<< EXECUTE_SUCCESS: '88frob*99grul99*jick88' >>> # <<< EXECUTE_SUCCESS: '`~!@$%^&*-_=+[]{}|;:'".<>/?' >>> # [[[ HEADER ]]] use RPerl; use strict; use warnings; our $VERSION = 0.001_000; # [[[ CRITICS ]]] ## no critic qw(ProhibitUselessNoCritic ProhibitMagicNumbers RequireCheckedSyscalls) # USER DEFAULT 1: allow numeric values & print operator ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils # [[[ OPERATIONS ]]] my string_arrayref $s_array = [qw(-1bar-5bat qu23ux zorg0+blop+0frun munge4/4sqap6/6cruft 88frob*99grul99*jick88 `~!@$%^&*-_=+[]{}|;:'".<>/?)]; foreach my string $s ( @{$s_array} ) { print '$s = ', $s, "\n"; }
[[[ SYNTAX PRODUCTION RULES, USER-DEFINED WORDS ]]]
WordScoped: WORD | WORD_SCOPED ; LoopLabel: WORD_UPPERCASE ; Type: WORD | WORD_SCOPED | TYPE_INTEGER ; TypeInner: MY Type '$TYPED_' OpStringOrWord OP19_VARIABLE_ASSIGN ; TypeInnerProperties: MY Type '$TYPED_' OpStringOrWord OP19_VARIABLE_ASSIGN SubExpression | MY Type '$TYPED_' OpStringOrWord OP02_ARRAY_THINARROW SubExpression ']' OP19_VARIABLE_ASSIGN 'undef' ; TypeInnerConstant: MY Type '$TYPED_' WORD_UPPERCASE OP19_VARIABLE_ASSIGN ; VariableOrLiteral: Variable | Literal ; VarOrLitOrOpStrOrWord: Variable | Literal | OpStringOrWord ; VariableSymbolOrSelf: VARIABLE_SYMBOL | SELF ; Literal: LITERAL_NUMBER | LITERAL_STRING ; OpNamedScolonOrSubExp: OP01_NAMED_SCOLON | OP10_NAMED_UNARY_SCOLON | SubExpression ';' ; OpNamedScolonOrSubExpIn: OP01_NAMED_SCOLON | OP10_NAMED_UNARY_SCOLON | SubExpressionOrInput ';' ; OpStringOrWord: OP24_LOGICAL_OR_XOR | OP23_LOGICAL_AND | OP22_LOGICAL_NEG | OP19_LOOP_CONTROL_SCOLON | OP19_LOOP_CONTROL | OP12_COMPARE_EQ_NE | OP11_COMPARE_LT_GT | OP10_NAMED_UNARY | OP08_MATH_ADD_SUB | OP07_MATH_MULT_DIV_MOD | OP07_STRING_REPEAT | OP01_NAMED | OP01_CLOSE | OP01_OPEN | OP01_NAMED_VOID | OP01_PRINT | WORD ;
There are no additional code examples for this section, all pertinent examples are contained in the previous sections.
Intermediate RPerl
The Scallion Book
Mastering RPerl
The Sword Book
RPerl
William N. Braswell, Jr.
mailto:wbraswell@NOSPAM.cpan.org
To install RPerl, copy and paste the appropriate command in to your terminal.
cpanm
cpanm RPerl
CPAN shell
perl -MCPAN -e shell install RPerl
For more information on module installation, please visit the detailed CPAN module installation guide.