Main Content
-> , >= and => in Perl Archive - Originally posted on "The Horse's Mouth" - 2006-11-18 10:44:05 - Graham Ellis
Yes, Perl is eclectic - lots of things it can do, taken from lots of places and so, yes, it does have all three of those operators!
-> Run the method named to the right on the object named on the left
>= Return 1 (a true value) if the expression to the left is numerically greater than the expression to the right
=> Normally used to indicate a key, value pair for entry into a hash. In fact, the => operator is the equivalent of a , (a comma) which can be used instead - the=> is just provided to make the your code easier to read, and (in some circumstances) saves you the need to quote keys.
Examples:
$kgs = $current->getweight();
# Get the weight of the $current object
print "heavy\n" if ($kgs >= 100);
# Print out if $kgs is numerically 100 or greater
%info = (weight => 120, height => 1850);
# Set a hash with 2 key, value pairs
Some other articles
P218 - More Objects Changing what operators do on objects - a comparison across different programming languages Object factories in C++, Python, PHP and Perl Using object orientation for non-physical objects Perl design patterns example Building an object based on another object in Perl Perl - calls to methods that use => - what do they mean? What do I mean when I add things in Perl? Learning Object Orientation in Perl through bananas and perhaps Moose Making Perl class definitions more conventional and shorter Some more advanced Perl examples from a recent course Different perl examples - some corners I rarely explore Igloos melt in the summer, but houses do not The Multiple Inheritance Conundrum, interfaces and mixins Calculation within objects - early, last minute, or cached? Operator overloading - redefining addition and other Perl tricks Nuclear Physics comes to our web site Calling base class constructors Factory method example - Perl Example of OO in Perl Object Oriented Programming in Perl - Course Perl for Larger Projects - Object Oriented Perl What are factory and singleton classes? This article Comparison of Object Oriented Philosophy - Python, Java, C++, Perl Think about your design even if you don't use full UML NOT Gone phishing Changing @INC - where Perl loads its modules Packages in packages in Perl When to bless a Perl variable Bellringing and Programming and Objects and Perl P211 - Hashes Finding all the unique lines in a file, using Python or Perl Why would you want to use a Perl hash? $ is atomic and % and @ are molecular - Perl Buckets Finding elements common to many lists / arrays Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl Sorting - naturally, or into a different order Looking up a value by key - associative arrays / Hashes / Dictionaries Perl - the duplicate key problem explained, and solutions offered Fresh Perl Teaching Examples - part 2 of 3 Out of memory during array extend - Perl A few of my favourite things Perl - Subs, Chop v Chomp, => v , Environment variables in Perl / use Env Stable sorting - Tcl, Perl and others Perl - a list or a hash? This article (Perl) Callbacks - what are they? What is a callback? Conventional restraints removed P204 - Conditionals and Loops Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Showing what programming errors look like - web site pitfall Flowchart to program - learning to program with Well House Does a for loop evaluate its end condition once, or on every iteration? Increment operators for counting - Perl, PHP, C and others Are you learning Perl? Some more examples for you! For loop - checked once, or evety time? Ruby v Perl comparison and contrast Do not copy and paste code - there are much better ways Ternary operators alternatives - Perl and Lua lazy operators Equality and looks like tests - Perl Saying NOT in Perl, PHP, Python, Lua ... Learning to program in Perl Decisions - small ones, or big ones? Lexical v Arithemetic testing, Bash and Perl Smart English Output - via PHP and Perl ? : operator This article Wimbledon Neck