1*0Sstevel@tonic-gate=head1 NAME 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateperlintro -- a brief introduction and overview of Perl 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate=head1 DESCRIPTION 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gateThis document is intended to give you a quick overview of the Perl 8*0Sstevel@tonic-gateprogramming language, along with pointers to further documentation. It 9*0Sstevel@tonic-gateis intended as a "bootstrap" guide for those who are new to the 10*0Sstevel@tonic-gatelanguage, and provides just enough information for you to be able to 11*0Sstevel@tonic-gateread other peoples' Perl and understand roughly what it's doing, or 12*0Sstevel@tonic-gatewrite your own simple scripts. 13*0Sstevel@tonic-gate 14*0Sstevel@tonic-gateThis introductory document does not aim to be complete. It does not 15*0Sstevel@tonic-gateeven aim to be entirely accurate. In some cases perfection has been 16*0Sstevel@tonic-gatesacrificed in the goal of getting the general idea across. You are 17*0Sstevel@tonic-gateI<strongly> advised to follow this introduction with more information 18*0Sstevel@tonic-gatefrom the full Perl manual, the table of contents to which can be found 19*0Sstevel@tonic-gatein L<perltoc>. 20*0Sstevel@tonic-gate 21*0Sstevel@tonic-gateThroughout this document you'll see references to other parts of the 22*0Sstevel@tonic-gatePerl documentation. You can read that documentation using the C<perldoc> 23*0Sstevel@tonic-gatecommand or whatever method you're using to read this document. 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate=head2 What is Perl? 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gatePerl is a general-purpose programming language originally developed for 28*0Sstevel@tonic-gatetext manipulation and now used for a wide range of tasks including 29*0Sstevel@tonic-gatesystem administration, web development, network programming, GUI 30*0Sstevel@tonic-gatedevelopment, and more. 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gateThe language is intended to be practical (easy to use, efficient, 33*0Sstevel@tonic-gatecomplete) rather than beautiful (tiny, elegant, minimal). Its major 34*0Sstevel@tonic-gatefeatures are that it's easy to use, supports both procedural and 35*0Sstevel@tonic-gateobject-oriented (OO) programming, has powerful built-in support for text 36*0Sstevel@tonic-gateprocessing, and has one of the world's most impressive collections of 37*0Sstevel@tonic-gatethird-party modules. 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gateDifferent definitions of Perl are given in L<perl>, L<perlfaq1> and 40*0Sstevel@tonic-gateno doubt other places. From this we can determine that Perl is different 41*0Sstevel@tonic-gatethings to different people, but that lots of people think it's at least 42*0Sstevel@tonic-gateworth writing about. 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate=head2 Running Perl programs 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gateTo run a Perl program from the Unix command line: 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate perl progname.pl 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gateAlternatively, put this as the first line of your script: 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate #!/usr/bin/env perl 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate... and run the script as C</path/to/script.pl>. Of course, it'll need 55*0Sstevel@tonic-gateto be executable first, so C<chmod 755 script.pl> (under Unix). 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gateFor more information, including instructions for other platforms such as 58*0Sstevel@tonic-gateWindows and Mac OS, read L<perlrun>. 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate=head2 Basic syntax overview 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gateA Perl script or program consists of one or more statements. These 63*0Sstevel@tonic-gatestatements are simply written in the script in a straightforward 64*0Sstevel@tonic-gatefashion. There is no need to have a C<main()> function or anything of 65*0Sstevel@tonic-gatethat kind. 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gatePerl statements end in a semi-colon: 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate print "Hello, world"; 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gateComments start with a hash symbol and run to the end of the line 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate # This is a comment 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gateWhitespace is irrelevant: 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate print 78*0Sstevel@tonic-gate "Hello, world" 79*0Sstevel@tonic-gate ; 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate... except inside quoted strings: 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate # this would print with a linebreak in the middle 84*0Sstevel@tonic-gate print "Hello 85*0Sstevel@tonic-gate world"; 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gateDouble quotes or single quotes may be used around literal strings: 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate print "Hello, world"; 90*0Sstevel@tonic-gate print 'Hello, world'; 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gateHowever, only double quotes "interpolate" variables and special 93*0Sstevel@tonic-gatecharacters such as newlines (C<\n>): 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate print "Hello, $name\n"; # works fine 96*0Sstevel@tonic-gate print 'Hello, $name\n'; # prints $name\n literally 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gateNumbers don't need quotes around them: 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate print 42; 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gateYou can use parentheses for functions' arguments or omit them 103*0Sstevel@tonic-gateaccording to your personal taste. They are only required 104*0Sstevel@tonic-gateoccasionally to clarify issues of precedence. 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate print("Hello, world\n"); 107*0Sstevel@tonic-gate print "Hello, world\n"; 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gateMore detailed information about Perl syntax can be found in L<perlsyn>. 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate=head2 Perl variable types 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gatePerl has three main variable types: scalars, arrays, and hashes. 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate=over 4 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate=item Scalars 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gateA scalar represents a single value: 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate my $animal = "camel"; 122*0Sstevel@tonic-gate my $answer = 42; 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gateScalar values can be strings, integers or floating point numbers, and Perl 125*0Sstevel@tonic-gatewill automatically convert between them as required. There is no need 126*0Sstevel@tonic-gateto pre-declare your variable types. 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gateScalar values can be used in various ways: 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate print $animal; 131*0Sstevel@tonic-gate print "The animal is $animal\n"; 132*0Sstevel@tonic-gate print "The square of $answer is ", $answer * $answer, "\n"; 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gateThere are a number of "magic" scalars with names that look like 135*0Sstevel@tonic-gatepunctuation or line noise. These special variables are used for all 136*0Sstevel@tonic-gatekinds of purposes, and are documented in L<perlvar>. The only one you 137*0Sstevel@tonic-gateneed to know about for now is C<$_> which is the "default variable". 138*0Sstevel@tonic-gateIt's used as the default argument to a number of functions in Perl, and 139*0Sstevel@tonic-gateit's set implicitly by certain looping constructs. 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate print; # prints contents of $_ by default 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate=item Arrays 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gateAn array represents a list of values: 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate my @animals = ("camel", "llama", "owl"); 148*0Sstevel@tonic-gate my @numbers = (23, 42, 69); 149*0Sstevel@tonic-gate my @mixed = ("camel", 42, 1.23); 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gateArrays are zero-indexed. Here's how you get at elements in an array: 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate print $animals[0]; # prints "camel" 154*0Sstevel@tonic-gate print $animals[1]; # prints "llama" 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gateThe special variable C<$#array> tells you the index of the last element 157*0Sstevel@tonic-gateof an array: 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate print $mixed[$#mixed]; # last element, prints 1.23 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gateYou might be tempted to use C<$#array + 1> to tell you how many items there 162*0Sstevel@tonic-gateare in an array. Don't bother. As it happens, using C<@array> where Perl 163*0Sstevel@tonic-gateexpects to find a scalar value ("in scalar context") will give you the number 164*0Sstevel@tonic-gateof elements in the array: 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate if (@animals < 5) { ... } 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gateThe elements we're getting from the array start with a C<$> because 169*0Sstevel@tonic-gatewe're getting just a single value out of the array -- you ask for a scalar, 170*0Sstevel@tonic-gateyou get a scalar. 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gateTo get multiple values from an array: 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate @animals[0,1]; # gives ("camel", "llama"); 175*0Sstevel@tonic-gate @animals[0..2]; # gives ("camel", "llama", "owl"); 176*0Sstevel@tonic-gate @animals[1..$#animals]; # gives all except the first element 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gateThis is called an "array slice". 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gateYou can do various useful things to lists: 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate my @sorted = sort @animals; 183*0Sstevel@tonic-gate my @backwards = reverse @numbers; 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gateThere are a couple of special arrays too, such as C<@ARGV> (the command 186*0Sstevel@tonic-gateline arguments to your script) and C<@_> (the arguments passed to a 187*0Sstevel@tonic-gatesubroutine). These are documented in L<perlvar>. 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate=item Hashes 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gateA hash represents a set of key/value pairs: 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate my %fruit_color = ("apple", "red", "banana", "yellow"); 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gateYou can use whitespace and the C<< => >> operator to lay them out more 196*0Sstevel@tonic-gatenicely: 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate my %fruit_color = ( 199*0Sstevel@tonic-gate apple => "red", 200*0Sstevel@tonic-gate banana => "yellow", 201*0Sstevel@tonic-gate ); 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gateTo get at hash elements: 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate $fruit_color{"apple"}; # gives "red" 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gateYou can get at lists of keys and values with C<keys()> and 208*0Sstevel@tonic-gateC<values()>. 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate my @fruits = keys %fruit_colors; 211*0Sstevel@tonic-gate my @colors = values %fruit_colors; 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gateHashes have no particular internal order, though you can sort the keys 214*0Sstevel@tonic-gateand loop through them. 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gateJust like special scalars and arrays, there are also special hashes. 217*0Sstevel@tonic-gateThe most well known of these is C<%ENV> which contains environment 218*0Sstevel@tonic-gatevariables. Read all about it (and other special variables) in 219*0Sstevel@tonic-gateL<perlvar>. 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate=back 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gateScalars, arrays and hashes are documented more fully in L<perldata>. 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gateMore complex data types can be constructed using references, which allow 226*0Sstevel@tonic-gateyou to build lists and hashes within lists and hashes. 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gateA reference is a scalar value and can refer to any other Perl data 229*0Sstevel@tonic-gatetype. So by storing a reference as the value of an array or hash 230*0Sstevel@tonic-gateelement, you can easily create lists and hashes within lists and 231*0Sstevel@tonic-gatehashes. The following example shows a 2 level hash of hash 232*0Sstevel@tonic-gatestructure using anonymous hash references. 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate my $variables = { 235*0Sstevel@tonic-gate scalar => { 236*0Sstevel@tonic-gate description => "single item", 237*0Sstevel@tonic-gate sigil => '$', 238*0Sstevel@tonic-gate }, 239*0Sstevel@tonic-gate array => { 240*0Sstevel@tonic-gate description => "ordered list of items", 241*0Sstevel@tonic-gate sigil => '@', 242*0Sstevel@tonic-gate }, 243*0Sstevel@tonic-gate hash => { 244*0Sstevel@tonic-gate description => "key/value pairs", 245*0Sstevel@tonic-gate sigil => '%', 246*0Sstevel@tonic-gate }, 247*0Sstevel@tonic-gate }; 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gate print "Scalars begin with a $variables->{'scalar'}->{'sigil'}\n"; 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gateExhaustive information on the topic of references can be found in 252*0Sstevel@tonic-gateL<perlreftut>, L<perllol>, L<perlref> and L<perldsc>. 253*0Sstevel@tonic-gate 254*0Sstevel@tonic-gate=head2 Variable scoping 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gateThroughout the previous section all the examples have used the syntax: 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate my $var = "value"; 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gateThe C<my> is actually not required; you could just use: 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate $var = "value"; 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gateHowever, the above usage will create global variables throughout your 265*0Sstevel@tonic-gateprogram, which is bad programming practice. C<my> creates lexically 266*0Sstevel@tonic-gatescoped variables instead. The variables are scoped to the block 267*0Sstevel@tonic-gate(i.e. a bunch of statements surrounded by curly-braces) in which they 268*0Sstevel@tonic-gateare defined. 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gate my $a = "foo"; 271*0Sstevel@tonic-gate if ($some_condition) { 272*0Sstevel@tonic-gate my $b = "bar"; 273*0Sstevel@tonic-gate print $a; # prints "foo" 274*0Sstevel@tonic-gate print $b; # prints "bar" 275*0Sstevel@tonic-gate } 276*0Sstevel@tonic-gate print $a; # prints "foo" 277*0Sstevel@tonic-gate print $b; # prints nothing; $b has fallen out of scope 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gateUsing C<my> in combination with a C<use strict;> at the top of 280*0Sstevel@tonic-gateyour Perl scripts means that the interpreter will pick up certain common 281*0Sstevel@tonic-gateprogramming errors. For instance, in the example above, the final 282*0Sstevel@tonic-gateC<print $b> would cause a compile-time error and prevent you from 283*0Sstevel@tonic-gaterunning the program. Using C<strict> is highly recommended. 284*0Sstevel@tonic-gate 285*0Sstevel@tonic-gate=head2 Conditional and looping constructs 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gatePerl has most of the usual conditional and looping constructs except for 288*0Sstevel@tonic-gatecase/switch (but if you really want it, there is a Switch module in Perl 289*0Sstevel@tonic-gate5.8 and newer, and on CPAN. See the section on modules, below, for more 290*0Sstevel@tonic-gateinformation about modules and CPAN). 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gateThe conditions can be any Perl expression. See the list of operators in 293*0Sstevel@tonic-gatethe next section for information on comparison and boolean logic operators, 294*0Sstevel@tonic-gatewhich are commonly used in conditional statements. 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate=over 4 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate=item if 299*0Sstevel@tonic-gate 300*0Sstevel@tonic-gate if ( condition ) { 301*0Sstevel@tonic-gate ... 302*0Sstevel@tonic-gate } elsif ( other condition ) { 303*0Sstevel@tonic-gate ... 304*0Sstevel@tonic-gate } else { 305*0Sstevel@tonic-gate ... 306*0Sstevel@tonic-gate } 307*0Sstevel@tonic-gate 308*0Sstevel@tonic-gateThere's also a negated version of it: 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate unless ( condition ) { 311*0Sstevel@tonic-gate ... 312*0Sstevel@tonic-gate } 313*0Sstevel@tonic-gate 314*0Sstevel@tonic-gateThis is provided as a more readable version of C<if (!I<condition>)>. 315*0Sstevel@tonic-gate 316*0Sstevel@tonic-gateNote that the braces are required in Perl, even if you've only got one 317*0Sstevel@tonic-gateline in the block. However, there is a clever way of making your one-line 318*0Sstevel@tonic-gateconditional blocks more English like: 319*0Sstevel@tonic-gate 320*0Sstevel@tonic-gate # the traditional way 321*0Sstevel@tonic-gate if ($zippy) { 322*0Sstevel@tonic-gate print "Yow!"; 323*0Sstevel@tonic-gate } 324*0Sstevel@tonic-gate 325*0Sstevel@tonic-gate # the Perlish post-condition way 326*0Sstevel@tonic-gate print "Yow!" if $zippy; 327*0Sstevel@tonic-gate print "We have no bananas" unless $bananas; 328*0Sstevel@tonic-gate 329*0Sstevel@tonic-gate=item while 330*0Sstevel@tonic-gate 331*0Sstevel@tonic-gate while ( condition ) { 332*0Sstevel@tonic-gate ... 333*0Sstevel@tonic-gate } 334*0Sstevel@tonic-gate 335*0Sstevel@tonic-gateThere's also a negated version, for the same reason we have C<unless>: 336*0Sstevel@tonic-gate 337*0Sstevel@tonic-gate until ( condition ) { 338*0Sstevel@tonic-gate ... 339*0Sstevel@tonic-gate } 340*0Sstevel@tonic-gate 341*0Sstevel@tonic-gateYou can also use C<while> in a post-condition: 342*0Sstevel@tonic-gate 343*0Sstevel@tonic-gate print "LA LA LA\n" while 1; # loops forever 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate=item for 346*0Sstevel@tonic-gate 347*0Sstevel@tonic-gateExactly like C: 348*0Sstevel@tonic-gate 349*0Sstevel@tonic-gate for ($i=0; $i <= $max; $i++) { 350*0Sstevel@tonic-gate ... 351*0Sstevel@tonic-gate } 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gateThe C style for loop is rarely needed in Perl since Perl provides 354*0Sstevel@tonic-gatethe more friendly list scanning C<foreach> loop. 355*0Sstevel@tonic-gate 356*0Sstevel@tonic-gate=item foreach 357*0Sstevel@tonic-gate 358*0Sstevel@tonic-gate foreach (@array) { 359*0Sstevel@tonic-gate print "This element is $_\n"; 360*0Sstevel@tonic-gate } 361*0Sstevel@tonic-gate 362*0Sstevel@tonic-gate # you don't have to use the default $_ either... 363*0Sstevel@tonic-gate foreach my $key (keys %hash) { 364*0Sstevel@tonic-gate print "The value of $key is $hash{$key}\n"; 365*0Sstevel@tonic-gate } 366*0Sstevel@tonic-gate 367*0Sstevel@tonic-gate=back 368*0Sstevel@tonic-gate 369*0Sstevel@tonic-gateFor more detail on looping constructs (and some that weren't mentioned in 370*0Sstevel@tonic-gatethis overview) see L<perlsyn>. 371*0Sstevel@tonic-gate 372*0Sstevel@tonic-gate=head2 Builtin operators and functions 373*0Sstevel@tonic-gate 374*0Sstevel@tonic-gatePerl comes with a wide selection of builtin functions. Some of the ones 375*0Sstevel@tonic-gatewe've already seen include C<print>, C<sort> and C<reverse>. A list of 376*0Sstevel@tonic-gatethem is given at the start of L<perlfunc> and you can easily read 377*0Sstevel@tonic-gateabout any given function by using C<perldoc -f I<functionname>>. 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gatePerl operators are documented in full in L<perlop>, but here are a few 380*0Sstevel@tonic-gateof the most common ones: 381*0Sstevel@tonic-gate 382*0Sstevel@tonic-gate=over 4 383*0Sstevel@tonic-gate 384*0Sstevel@tonic-gate=item Arithmetic 385*0Sstevel@tonic-gate 386*0Sstevel@tonic-gate + addition 387*0Sstevel@tonic-gate - subtraction 388*0Sstevel@tonic-gate * multiplication 389*0Sstevel@tonic-gate / division 390*0Sstevel@tonic-gate 391*0Sstevel@tonic-gate=item Numeric comparison 392*0Sstevel@tonic-gate 393*0Sstevel@tonic-gate == equality 394*0Sstevel@tonic-gate != inequality 395*0Sstevel@tonic-gate < less than 396*0Sstevel@tonic-gate > greater than 397*0Sstevel@tonic-gate <= less than or equal 398*0Sstevel@tonic-gate >= greater than or equal 399*0Sstevel@tonic-gate 400*0Sstevel@tonic-gate=item String comparison 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate eq equality 403*0Sstevel@tonic-gate ne inequality 404*0Sstevel@tonic-gate lt less than 405*0Sstevel@tonic-gate gt greater than 406*0Sstevel@tonic-gate le less than or equal 407*0Sstevel@tonic-gate ge greater than or equal 408*0Sstevel@tonic-gate 409*0Sstevel@tonic-gate(Why do we have separate numeric and string comparisons? Because we don't 410*0Sstevel@tonic-gatehave special variable types, and Perl needs to know whether to sort 411*0Sstevel@tonic-gatenumerically (where 99 is less than 100) or alphabetically (where 100 comes 412*0Sstevel@tonic-gatebefore 99). 413*0Sstevel@tonic-gate 414*0Sstevel@tonic-gate=item Boolean logic 415*0Sstevel@tonic-gate 416*0Sstevel@tonic-gate && and 417*0Sstevel@tonic-gate || or 418*0Sstevel@tonic-gate ! not 419*0Sstevel@tonic-gate 420*0Sstevel@tonic-gate(C<and>, C<or> and C<not> aren't just in the above table as descriptions 421*0Sstevel@tonic-gateof the operators -- they're also supported as operators in their own 422*0Sstevel@tonic-gateright. They're more readable than the C-style operators, but have 423*0Sstevel@tonic-gatedifferent precedence to C<&&> and friends. Check L<perlop> for more 424*0Sstevel@tonic-gatedetail.) 425*0Sstevel@tonic-gate 426*0Sstevel@tonic-gate=item Miscellaneous 427*0Sstevel@tonic-gate 428*0Sstevel@tonic-gate = assignment 429*0Sstevel@tonic-gate . string concatenation 430*0Sstevel@tonic-gate x string multiplication 431*0Sstevel@tonic-gate .. range operator (creates a list of numbers) 432*0Sstevel@tonic-gate 433*0Sstevel@tonic-gate=back 434*0Sstevel@tonic-gate 435*0Sstevel@tonic-gateMany operators can be combined with a C<=> as follows: 436*0Sstevel@tonic-gate 437*0Sstevel@tonic-gate $a += 1; # same as $a = $a + 1 438*0Sstevel@tonic-gate $a -= 1; # same as $a = $a - 1 439*0Sstevel@tonic-gate $a .= "\n"; # same as $a = $a . "\n"; 440*0Sstevel@tonic-gate 441*0Sstevel@tonic-gate=head2 Files and I/O 442*0Sstevel@tonic-gate 443*0Sstevel@tonic-gateYou can open a file for input or output using the C<open()> function. 444*0Sstevel@tonic-gateIt's documented in extravagant detail in L<perlfunc> and L<perlopentut>, 445*0Sstevel@tonic-gatebut in short: 446*0Sstevel@tonic-gate 447*0Sstevel@tonic-gate open(INFILE, "input.txt") or die "Can't open input.txt: $!"; 448*0Sstevel@tonic-gate open(OUTFILE, ">output.txt") or die "Can't open output.txt: $!"; 449*0Sstevel@tonic-gate open(LOGFILE, ">>my.log") or die "Can't open logfile: $!"; 450*0Sstevel@tonic-gate 451*0Sstevel@tonic-gateYou can read from an open filehandle using the C<< <> >> operator. In 452*0Sstevel@tonic-gatescalar context it reads a single line from the filehandle, and in list 453*0Sstevel@tonic-gatecontext it reads the whole file in, assigning each line to an element of 454*0Sstevel@tonic-gatethe list: 455*0Sstevel@tonic-gate 456*0Sstevel@tonic-gate my $line = <INFILE>; 457*0Sstevel@tonic-gate my @lines = <INFILE>; 458*0Sstevel@tonic-gate 459*0Sstevel@tonic-gateReading in the whole file at one time is called slurping. It can 460*0Sstevel@tonic-gatebe useful but it may be a memory hog. Most text file processing 461*0Sstevel@tonic-gatecan be done a line at a time with Perl's looping constructs. 462*0Sstevel@tonic-gate 463*0Sstevel@tonic-gateThe C<< <> >> operator is most often seen in a C<while> loop: 464*0Sstevel@tonic-gate 465*0Sstevel@tonic-gate while (<INFILE>) { # assigns each line in turn to $_ 466*0Sstevel@tonic-gate print "Just read in this line: $_"; 467*0Sstevel@tonic-gate } 468*0Sstevel@tonic-gate 469*0Sstevel@tonic-gateWe've already seen how to print to standard output using C<print()>. 470*0Sstevel@tonic-gateHowever, C<print()> can also take an optional first argument specifying 471*0Sstevel@tonic-gatewhich filehandle to print to: 472*0Sstevel@tonic-gate 473*0Sstevel@tonic-gate print STDERR "This is your final warning.\n"; 474*0Sstevel@tonic-gate print OUTFILE $record; 475*0Sstevel@tonic-gate print LOGFILE $logmessage; 476*0Sstevel@tonic-gate 477*0Sstevel@tonic-gateWhen you're done with your filehandles, you should C<close()> them 478*0Sstevel@tonic-gate(though to be honest, Perl will clean up after you if you forget): 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gate close INFILE; 481*0Sstevel@tonic-gate 482*0Sstevel@tonic-gate=head2 Regular expressions 483*0Sstevel@tonic-gate 484*0Sstevel@tonic-gatePerl's regular expression support is both broad and deep, and is the 485*0Sstevel@tonic-gatesubject of lengthy documentation in L<perlrequick>, L<perlretut>, and 486*0Sstevel@tonic-gateelsewhere. However, in short: 487*0Sstevel@tonic-gate 488*0Sstevel@tonic-gate=over 4 489*0Sstevel@tonic-gate 490*0Sstevel@tonic-gate=item Simple matching 491*0Sstevel@tonic-gate 492*0Sstevel@tonic-gate if (/foo/) { ... } # true if $_ contains "foo" 493*0Sstevel@tonic-gate if ($a =~ /foo/) { ... } # true if $a contains "foo" 494*0Sstevel@tonic-gate 495*0Sstevel@tonic-gateThe C<//> matching operator is documented in L<perlop>. It operates on 496*0Sstevel@tonic-gateC<$_> by default, or can be bound to another variable using the C<=~> 497*0Sstevel@tonic-gatebinding operator (also documented in L<perlop>). 498*0Sstevel@tonic-gate 499*0Sstevel@tonic-gate=item Simple substitution 500*0Sstevel@tonic-gate 501*0Sstevel@tonic-gate s/foo/bar/; # replaces foo with bar in $_ 502*0Sstevel@tonic-gate $a =~ s/foo/bar/; # replaces foo with bar in $a 503*0Sstevel@tonic-gate $a =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $a 504*0Sstevel@tonic-gate 505*0Sstevel@tonic-gateThe C<s///> substitution operator is documented in L<perlop>. 506*0Sstevel@tonic-gate 507*0Sstevel@tonic-gate=item More complex regular expressions 508*0Sstevel@tonic-gate 509*0Sstevel@tonic-gateYou don't just have to match on fixed strings. In fact, you can match 510*0Sstevel@tonic-gateon just about anything you could dream of by using more complex regular 511*0Sstevel@tonic-gateexpressions. These are documented at great length in L<perlre>, but for 512*0Sstevel@tonic-gatethe meantime, here's a quick cheat sheet: 513*0Sstevel@tonic-gate 514*0Sstevel@tonic-gate . a single character 515*0Sstevel@tonic-gate \s a whitespace character (space, tab, newline) 516*0Sstevel@tonic-gate \S non-whitespace character 517*0Sstevel@tonic-gate \d a digit (0-9) 518*0Sstevel@tonic-gate \D a non-digit 519*0Sstevel@tonic-gate \w a word character (a-z, A-Z, 0-9, _) 520*0Sstevel@tonic-gate \W a non-word character 521*0Sstevel@tonic-gate [aeiou] matches a single character in the given set 522*0Sstevel@tonic-gate [^aeiou] matches a single character outside the given set 523*0Sstevel@tonic-gate (foo|bar|baz) matches any of the alternatives specified 524*0Sstevel@tonic-gate 525*0Sstevel@tonic-gate ^ start of string 526*0Sstevel@tonic-gate $ end of string 527*0Sstevel@tonic-gate 528*0Sstevel@tonic-gateQuantifiers can be used to specify how many of the previous thing you 529*0Sstevel@tonic-gatewant to match on, where "thing" means either a literal character, one 530*0Sstevel@tonic-gateof the metacharacters listed above, or a group of characters or 531*0Sstevel@tonic-gatemetacharacters in parentheses. 532*0Sstevel@tonic-gate 533*0Sstevel@tonic-gate * zero or more of the previous thing 534*0Sstevel@tonic-gate + one or more of the previous thing 535*0Sstevel@tonic-gate ? zero or one of the previous thing 536*0Sstevel@tonic-gate {3} matches exactly 3 of the previous thing 537*0Sstevel@tonic-gate {3,6} matches between 3 and 6 of the previous thing 538*0Sstevel@tonic-gate {3,} matches 3 or more of the previous thing 539*0Sstevel@tonic-gate 540*0Sstevel@tonic-gateSome brief examples: 541*0Sstevel@tonic-gate 542*0Sstevel@tonic-gate /^\d+/ string starts with one or more digits 543*0Sstevel@tonic-gate /^$/ nothing in the string (start and end are adjacent) 544*0Sstevel@tonic-gate /(\d\s){3}/ a three digits, each followed by a whitespace 545*0Sstevel@tonic-gate character (eg "3 4 5 ") 546*0Sstevel@tonic-gate /(a.)+/ matches a string in which every odd-numbered letter 547*0Sstevel@tonic-gate is a (eg "abacadaf") 548*0Sstevel@tonic-gate 549*0Sstevel@tonic-gate # This loop reads from STDIN, and prints non-blank lines: 550*0Sstevel@tonic-gate while (<>) { 551*0Sstevel@tonic-gate next if /^$/; 552*0Sstevel@tonic-gate print; 553*0Sstevel@tonic-gate } 554*0Sstevel@tonic-gate 555*0Sstevel@tonic-gate=item Parentheses for capturing 556*0Sstevel@tonic-gate 557*0Sstevel@tonic-gateAs well as grouping, parentheses serve a second purpose. They can be 558*0Sstevel@tonic-gateused to capture the results of parts of the regexp match for later use. 559*0Sstevel@tonic-gateThe results end up in C<$1>, C<$2> and so on. 560*0Sstevel@tonic-gate 561*0Sstevel@tonic-gate # a cheap and nasty way to break an email address up into parts 562*0Sstevel@tonic-gate 563*0Sstevel@tonic-gate if ($email =~ /([^@])+@(.+)/) { 564*0Sstevel@tonic-gate print "Username is $1\n"; 565*0Sstevel@tonic-gate print "Hostname is $2\n"; 566*0Sstevel@tonic-gate } 567*0Sstevel@tonic-gate 568*0Sstevel@tonic-gate=item Other regexp features 569*0Sstevel@tonic-gate 570*0Sstevel@tonic-gatePerl regexps also support backreferences, lookaheads, and all kinds of 571*0Sstevel@tonic-gateother complex details. Read all about them in L<perlrequick>, 572*0Sstevel@tonic-gateL<perlretut>, and L<perlre>. 573*0Sstevel@tonic-gate 574*0Sstevel@tonic-gate=back 575*0Sstevel@tonic-gate 576*0Sstevel@tonic-gate=head2 Writing subroutines 577*0Sstevel@tonic-gate 578*0Sstevel@tonic-gateWriting subroutines is easy: 579*0Sstevel@tonic-gate 580*0Sstevel@tonic-gate sub log { 581*0Sstevel@tonic-gate my $logmessage = shift; 582*0Sstevel@tonic-gate print LOGFILE $logmessage; 583*0Sstevel@tonic-gate } 584*0Sstevel@tonic-gate 585*0Sstevel@tonic-gateWhat's that C<shift>? Well, the arguments to a subroutine are available 586*0Sstevel@tonic-gateto us as a special array called C<@_> (see L<perlvar> for more on that). 587*0Sstevel@tonic-gateThe default argument to the C<shift> function just happens to be C<@_>. 588*0Sstevel@tonic-gateSo C<my $logmessage = shift;> shifts the first item off the list of 589*0Sstevel@tonic-gatearguments and assigns it to C<$logmessage>. 590*0Sstevel@tonic-gate 591*0Sstevel@tonic-gateWe can manipulate C<@_> in other ways too: 592*0Sstevel@tonic-gate 593*0Sstevel@tonic-gate my ($logmessage, $priority) = @_; # common 594*0Sstevel@tonic-gate my $logmessage = $_[0]; # uncommon, and ugly 595*0Sstevel@tonic-gate 596*0Sstevel@tonic-gateSubroutines can also return values: 597*0Sstevel@tonic-gate 598*0Sstevel@tonic-gate sub square { 599*0Sstevel@tonic-gate my $num = shift; 600*0Sstevel@tonic-gate my $result = $num * $num; 601*0Sstevel@tonic-gate return $result; 602*0Sstevel@tonic-gate } 603*0Sstevel@tonic-gate 604*0Sstevel@tonic-gateFor more information on writing subroutines, see L<perlsub>. 605*0Sstevel@tonic-gate 606*0Sstevel@tonic-gate=head2 OO Perl 607*0Sstevel@tonic-gate 608*0Sstevel@tonic-gateOO Perl is relatively simple and is implemented using references which 609*0Sstevel@tonic-gateknow what sort of object they are based on Perl's concept of packages. 610*0Sstevel@tonic-gateHowever, OO Perl is largely beyond the scope of this document. 611*0Sstevel@tonic-gateRead L<perlboot>, L<perltoot>, L<perltooc> and L<perlobj>. 612*0Sstevel@tonic-gate 613*0Sstevel@tonic-gateAs a beginning Perl programmer, your most common use of OO Perl will be 614*0Sstevel@tonic-gatein using third-party modules, which are documented below. 615*0Sstevel@tonic-gate 616*0Sstevel@tonic-gate=head2 Using Perl modules 617*0Sstevel@tonic-gate 618*0Sstevel@tonic-gatePerl modules provide a range of features to help you avoid reinventing 619*0Sstevel@tonic-gatethe wheel, and can be downloaded from CPAN ( http://www.cpan.org/ ). A 620*0Sstevel@tonic-gatenumber of popular modules are included with the Perl distribution 621*0Sstevel@tonic-gateitself. 622*0Sstevel@tonic-gate 623*0Sstevel@tonic-gateCategories of modules range from text manipulation to network protocols 624*0Sstevel@tonic-gateto database integration to graphics. A categorized list of modules is 625*0Sstevel@tonic-gatealso available from CPAN. 626*0Sstevel@tonic-gate 627*0Sstevel@tonic-gateTo learn how to install modules you download from CPAN, read 628*0Sstevel@tonic-gateL<perlmodinstall> 629*0Sstevel@tonic-gate 630*0Sstevel@tonic-gateTo learn how to use a particular module, use C<perldoc I<Module::Name>>. 631*0Sstevel@tonic-gateTypically you will want to C<use I<Module::Name>>, which will then give 632*0Sstevel@tonic-gateyou access to exported functions or an OO interface to the module. 633*0Sstevel@tonic-gate 634*0Sstevel@tonic-gateL<perlfaq> contains questions and answers related to many common 635*0Sstevel@tonic-gatetasks, and often provides suggestions for good CPAN modules to use. 636*0Sstevel@tonic-gate 637*0Sstevel@tonic-gateL<perlmod> describes Perl modules in general. L<perlmodlib> lists the 638*0Sstevel@tonic-gatemodules which came with your Perl installation. 639*0Sstevel@tonic-gate 640*0Sstevel@tonic-gateIf you feel the urge to write Perl modules, L<perlnewmod> will give you 641*0Sstevel@tonic-gategood advice. 642*0Sstevel@tonic-gate 643*0Sstevel@tonic-gate=head1 AUTHOR 644*0Sstevel@tonic-gate 645*0Sstevel@tonic-gateKirrily "Skud" Robert <skud@cpan.org> 646