






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







sub TRUE { 1 } sub FALSE { undef } if ($_[0]) { return "true "; } else { return "false"; } sub BoolText { } print BoolText($b1). " "
. BoolText($b2). " " . BoolText(! ($b1 && $b2)) . "\n"; foreach my $b2 (&FALSE, &TRUE) { } foreach my $b1 ( &FALSE, &TRUE ) { } My version Docsity.com
@a = (1,2,3); print "first item is $a[0]\n"; print "second item is $a[1]\n"; print "third item is $a[2]\n"; An array is a list of items. Note that the array is @a, but an element is of the form $a[$i]. In other words, something that starts with @ is an array, while something that starts with $ is a scalar , even if it is an array element! Arrays in Perl Docsity.com
Perl has a unique feature called parallel assignment. ($a, $b, $c)=(1,42, "hello"); One can write to set $a to 1, $b to 42, and $c to "hello". ($a,$b,@c)=(22,"hi","ho","there"); One can write to set $a to 22, $b to "hi", and @c to ("ho", "there") The last array consumes the remaining elements. Parallel assignment Docsity.com
my ($cat, $dog) = @_; print "my cat's name is $cat\n"; print "my dog's name is $dog\n"; sub identify { } Typical use of parallel assignment: writing subroutines &identify("Fred", "George"); If we then write my cat's name is Fred my dog's name is George then in the subroutine after the assignment, $cat eq "Fred" and $dog eq "George", and this will print Writing subroutines Docsity.com
&&, and: in Perl : in logic We now have two concepts of "and" We use && to denote something to do , i.e., A && B is a command that computes a value (true or false). We use to denote "a statement about the world". A B is not a command, but rather, a description of state. What is the difference? Why are logic and programming symbols different? Docsity.com
So far, logic has been a matter of manipulating symbols. map reality into symbols, and map results to reality. In fact, logic is only useful if one can One key to making logic useful: "quantification". Making logic useful Docsity.com
Every variable in a parameterized predicate has a domain : a set of reasonable values. x is in the domain of machines in our building. P(x)="machine x is working." x is in the domain of numbers. Q(x)="x>10" A "parameterized predicate" is a statement of the form P(x) where x is a variable. Parameterized predicates Docsity.com