From Fedora Project Wiki

Resources

IRC Log

-!- nirik changed the topic of #fedora-classroom to: Fedora IRC Classroom - Some Perl basics with your teacher Doran Barton ( fozzmoo ) - See Classroom for class schedule today and more info. 15:00
@nirik take it away fozzmoo... 15:00
fozzmoo For those who will following along, here are some slides: http://www.xmission.com/~fozz/perl_basics.pdf 15:00
fozzmoo I'm Doran Barton, programmer, sysadmin, RHCE, geek, and Perl junkie. 15:01
fozzmoo My e-mail address is on slide #1. 15:01
fozzmoo How many people are actively here for this session as opposed to idling... idly? 15:01
Ritmo2k Active 15:02
* nirik is watching along. 15:02
mharris 3.14 people :) 15:02
* SMParrish here 15:02
fozzmoo heh. 15:02
baconfork present 15:02
fozzmoo Just wanted to make sure I'm not presenting to a bunch of cardboard cutouts. :) 15:02
mharris LOL 15:02
fozzmoo Let's move on to slide #2 15:02
heftig online 15:02
* delhage is a bit of a cardboard cutout, but here 15:03
fozzmoo This is just a summary of some learning resources I threw together. 15:03
fozzmoo The O'Reilly books are, without a doubt, the best printed material you can get on Perl. 15:03
fozzmoo That doesn't mean other publishers don't have good stuff... but, honestly, I wouldn't know. All the brains in the community seem to write or work for ORA 15:03
fozzmoo In addition to perlmonks.org as a site to use as a resource, I would also recommend perl.com (operated, again, by O'Reilly and Associates) 15:04
fozzmoo And then, there may be a local Perl users group near you - check pm.org 15:04
fozzmoo Any questions before we move to slide #3? 15:04
fozzmoo okay then. 15:05
fozzmoo Perl. What is it? 15:05
fozzmoo It's an interpreted scripting language. 15:05
fozzmoo But, like other modern languages, the definition is beginning to blur. 15:06
fozzmoo The actual mechanics of the Perl interpreter is similar to that of Java. 15:06
fozzmoo And, it will be even more so with Perl6. 15:06
fozzmoo Because of the interpreter nature, and because of a large and active open source community, the Perl interpreter is available on many platforms. 15:06
fozzmoo Linux, of course, is probably where most of the activity is at, followed by other Unix variants and then Windows. 15:07
fozzmoo The creator of the Perl language, Larry Wall, has a deep background in linguistics. 15:07
fozzmoo He proudly proclaims that Perl is a natural language. 15:08
fozzmoo I won't go into that here, but you can read about it if you're interested in what that means. 15:08
fozzmoo One thing it does mean is that Perl *can* be easy to follow and read. 15:08
fozzmoo Unfortunately, many programs test that hypothesis. :) 15:08
fozzmoo programmers, I mean. 15:08
fozzmoo It is free and Free. 15:09
fozzmoo And, as of Perl5, it is object oriented and is becoming more and more so as we approach Perl6. 15:09
fozzmoo The, mantra, if you will, of the Perl community is TMTOWDI 15:09
fozzmoo "There is more than one way to do it." 15:09
fozzmoo This is evidenced by the many, many different Perl solutions to a given problem. 15:10
fozzmoo Perl is a very flexible language and it seems there's always a more efficient way to do things. 15:10
fozzmoo The trick is balancing efficiency with readability. :) 15:10
fozzmoo Going back to slide 2 for a moment... 15:10
fozzmoo Damian Conway has written an excellent book called "Perl Best Practices" 15:11
fozzmoo If you're going to do some serious development using Perl, you *must* get this book. 15:11
fozzmoo Don't learn how to program in Perl by reading random code snippets you find online. 15:11
fozzmoo Any questions before I go to my soapbox slide (#4)? 15:11
SSlater I have heard that perl is good for manipulating data? 15:12
fozzmoo SSlater: It is. Perl's original forte was processing text data. 15:13
SSlater Great. 15:13
fozzmoo That made it ideal, later on, for doing things with HTML, XML, etc. 15:13
fozzmoo Another strength Perl brings to the table with this is it's regex support which has kind of defined regular expression syntax for the rest of the open source world. 15:14
* fozzmoo slaps himself for not phrasing that better. 15:14
fozzmoo Anyway, yeah. Perl rocks for text processing. 15:14
fozzmoo Perl was first unleashed on the world in the mid 1980s. 15:15
fozzmoo So, it's pretty old. 15:15
fozzmoo A lot has changed since then, of course. 15:15
fozzmoo The syntax borrowed heavily from C, Bourne shell, and awk. 15:15
fozzmoo The original PHP interpreter was written in Perl. 15:16
fozzmoo this possibly explains why PHP's syntax is, in many ways, similar to Perl. 15:16
fozzmoo In my opinion, Perl is technologically on a much higher plane than PHP. 15:16
fozzmoo A lot more ReallySmartPeople(tm) have worked on Perl and have made it a very sophisticated language. 15:17
fozzmoo This is somewhat of an achiles heel, though. 15:17
fozzmoo I've written about this on my blog <http://fozzolog.fozzilinymoo.org/tech/> - Perl is nowhere near as "accessible" for web developers as PHP is. 15:18
fozzmoo But PHP apps have a limit to their scalability. 15:18
fozzmoo Perl apps, not so much. 15:18
fozzmoo Python is another language you may consider. 15:19
fozzmoo I'm not well versed in Python, but I would say it also has very smart people involved. 15:19
fozzmoo The advantage Perl has over Python is the huge breadth of modules available online for re-use in your apps. 15:19
fozzmoo But Python's moving fast. 15:20
fozzmoo I only know one guy who has done a lot of Perl development and Python development. 15:20
fozzmoo Specifically, he's doing django work. 15:20
fozzmoo He likes it, but he misses the flexibility of Perl where you can swap modules in and out easily where django is more restrictive about what packages you can use for what. It's not all or nothing, but it's not easy to mix and match. 15:21
fozzmoo Java is... well... 15:21
fozzmoo Have you ever seen a Java one-liner? 15:21
fozzmoo I rest my case. 15:21
heftig no :P 15:21
delhage :) 15:22
fozzmoo Any questions before we zip to #5? 15:22
fozzmoo BTW, if you're late arriving: http://www.xmission.com/~fozz/perl_basics.pdf 15:23
fozzmoo Perl was the first language I played with that had a standard embedded documentation system. 15:23
fozzmoo It's called POD - Plain Old Documentation. 15:24
fozzmoo If you're on a Linux system, you probably have some Perl libs installed... 15:24
fozzmoo Go find one of these like LWP.pm and open it in an editor 15:24
wonderer|chatzil how do I test this the fastest way? 15:25
fozzmoo locate LWP.pm 15:25
wonderer|chatzil ok 15:25
fozzmoo Then, edit the file... 15:25
fozzmoo You should see the inline documentation right away. 15:25
fozzmoo You can read this at the command line with the 'perldoc' command. 15:26
fozzmoo i.e. 'perldoc LWP' 15:26
fozzmoo This explains what you can do with the LWP module. 15:26
fozzmoo For a table of contents, so to speak, you can type 'perldoc perl' 15:26
fozzmoo this gives you all the base POD volumes you can read. 15:26
fozzmoo If you're really cheap and you don't want to go buy a book... 15:26
fozzmoo you can learn pretty much everything from these online PODs. 15:27
fozzmoo The wit and charm of the main devs is littered throughout, just like in the books. :) 15:27
fozzmoo The example on slide #5 (perldoc -f printf) is how you bring up documentation on a specific built-in function. 15:28
fozzmoo Those are stored in the perlfunc POD. 15:28
fozzmoo All this documentation is available online as well, via many sites. perl.com is one. 15:28
fozzmoo For installed CPAN modules, you can read docs on cpan.org if you'd like 15:29
fozzmoo Now, a word about CPAN (Comprehensive Perl Archive Network) 15:29
fozzmoo it seems every other scripting language known to man has CPAN-envy. 15:29
fozzmoo PHP has PEAR. Python has... what it has. 15:30
fozzmoo Java hasn't figured out what it has... 15:30
fozzmoo CPAN is a distributed repository of modules you can install and use in your apps. 15:30
fozzmoo The Perl community has built a very nice automated testing framework and has established high standards for what constitutes a modules accepted for inclusion in CPAN. 15:31
fozzmoo it's not a matter of just uploading something. 15:31
fozzmoo It has been well documented and has to abide by certain conventions. 15:31
fozzmoo So, let's move to #6 15:31
fozzmoo I'd like to wrap up in about 10-15 minutes for Q&A. :) 15:32
fozzmoo We've got 19 slides left. 15:32
fozzmoo Slide 6... the requisite hello world demo. 15:32
fozzmoo First line is the "magic" line, just like you'd expect with shell scripting or python scripting. 15:32
fozzmoo This tells your shell what interpreter to use. 15:32
fozzmoo So, if your Perl interpreter isn't at /usr/bin/perl, replace that accordingly. 15:33
fozzmoo If you're using Fedora, RHEL, CentOS, etc. it probably is in /usr/bin 15:33
* fozzmoo beeps and moves to Slide #7 15:33
fozzmoo So, you save your script in a file. Call it what you want, but typically they're named with a .pl extension. 15:34
fozzmoo To run it, you can simply give the filename to the interpreter as a command line parameter. 15:34
fozzmoo i.e. perl filename.pl 15:34
fozzmoo If you want it to be a standalone executable, then you do what you would do with a shell script or other script. Make sure you have the necessary magic line at the top of the file, assign execute permissions to the file, and then you can run it from the command line by itself (granted it's in your path, or you use ./cruel.pl) 15:35
fozzmoo Speak up if you are wondering anything... I'm moving on. 15:36
* fozzmoo beeps and moves to Slide #8 15:36
fozzmoo Rules: Case sensitive, semicolons at the end of most statements. 15:36
fozzmoo Comments begin with # 15:36
fozzmoo (like in shell scripts) 15:36
fozzmoo Indentation isn't part of the syntax like Python, but it is highly recommended. :) 15:37
* fozzmoo beeps and moves to Slide #9 15:37
fozzmoo Now we get into some of the meat of Perl. 15:37
fozzmoo Unlike C or Java, Perl is loosely typed. 15:37
fozzmoo That will change somewhat with Perl6, but for now, Perl variables are broken down into three types: scalars, lists, and hashes. 15:38
fozzmoo Scalars contain any singular data which can be a number, a character, a string. 15:38
fozzmoo Lists are ordered collections of scalars. 15:38
fozzmoo Hashes are indexed collections of scalars. 15:39
heftig so lists have no holes? 15:39
fozzmoo holes? 15:39
fozzmoo Could you ellaborate a bit, heftig? 15:39
heftig well, empty items 15:40
fozzmoo No. They do not. 15:40
heftig okay 15:40
fozzmoo So, the way you access data in a variable is using context. 15:40
fozzmoo The $ is "scalar context" 15:40
fozzmoo So, $x is a variable that contains scalar data. 15:41
fozzmoo The @ is "list context" 15:41
fozzmoo @x is a list and $x[2] is the scalar at the 3rd element in @x 15:41
fozzmoo (starting at zero) 15:41
fozzmoo Finally, % is "hash context" 15:42
fozzmoo %x is a hash and $x{name} is the value stored in %x referenced by the key "name" 15:42
fozzmoo I think PHP calls this a dictionary. 15:42
* fozzmoo beeps and moves to Slide #10 15:43
* fozzmoo beeps and moves to Slide #11 15:43
fozzmoo Couple notes about lists... 15:43
fozzmoo the 'scalar' function, given a list variable, will return the number of elements in the list. 15:44
baconfork ? 15:44
fozzmoo Is that weird? 15:44
fozzmoo So, if you have @x 15:44
fozzmoo You can call $num = scalar @x; 15:45
fozzmoo And $num will contain the number of elements in @x 15:45
delhage ? 15:45
Ritmo2k I thought $var returned the size of @var if it exists? 15:45
fozzmoo Another way of looking at it is that the 'scalar' function forces Perl to look at the list in scalar context. 15:45
delhage Are command line arguments passed in as a list? 15:46
baconfork how does push/pop relate to shift/unshift? 15:46
fozzmoo Ritmo2k: No, $var and @var are two different variables (but $var[0] is a specific element in @var) 15:46
fozzmoo delhage: It depends on which end of the list you're working with. 15:46
delhage ok.... 15:47
fozzmoo delhage: See 'perldoc -f push' 15:47
fozzmoo push "pushes values onto the end of the list" 15:47
fozzmoo Whereas unshift pushes values onto the beginning. 15:47
fozzmoo It's handy because you can treat your list as a queue or a stack right out of the chute. 15:48
delhage fozzmoo: I think that was re baconfork's question? 15:48
fozzmoo delhage: You're right. Sorry. 15:48
Ritmo2k Yea, that was wrong, but when do you use 'scalar' and not just $size = @array ? 15:48
fozzmoo baconfork: That was for YOU! 15:48
baconfork I got now 15:48
fozzmoo Ritmo2k: For readability. :) 15:48
Ritmo2k Ahh 15:48
delhage I got really confused for a second ;) 15:48
fozzmoo delhage: To answer YOUR question... 15:48
fozzmoo delhage: Yes, command line parameters are passed in as a list. 15:48
fozzmoo I'll talk about that in a sec. 15:48
delhage ok 15:49
* fozzmoo beeps and moves to slide #12 15:49
fozzmoo Hashes are the weirdest thing for people learning Perl. 15:49
fozzmoo usually. 15:49
fozzmoo One way to imagine them is that they're lists but instead of using integers to reference data, you can use strings. 15:49
fozzmoo As such, they're not ordered. 15:49
fozzmoo They can be treated as a set. 15:50
fozzmoo On slide #12, there's an example of how we would populate a list. 15:50
fozzmoo %student = (name1 => "value1", name2 => "value2", ...); 15:50
fozzmoo And then, to access a specific value, we use the name, or "key" as a reference: 15:51
fozzmoo $student{'student_no'} 15:51
fozzmoo (and we do so in scalar context) 15:51
* fozzmoo beeps and moves to slide #13 15:51
Ritmo2k So hashes are _only_ pairs: Keys/Values, but the values can be anything? 15:51
fozzmoo Ritmo2k: yes. The values are just scalars. 15:52
fozzmoo As are the keys 15:52
fozzmoo And, if you treat a hash in list context, it's just a list of scalars where the odd scalars are keys and the even scalars are values. 15:52
fozzmoo (Sorry- that's probably weird to comprehend.) 15:53
Ritmo2k Nah, but a hash of hashes wouldnt look good that way, right? 15:53
fozzmoo You can do hashes of hashes, but what you need to do is use what's called a 'reference' 15:53
fozzmoo and I wasn't planning on talking about that this time. 15:53
fozzmoo But a reference is akin to a "pointer" in C. 15:53
fozzmoo it lets you treat a hash as a scalar. 15:54
fozzmoo Once you have a way of accessing a whole hash via a scalar, you can store it as a value in a hash. 15:54
fozzmoo Instant hash of hashes. 15:54
heftig similar to php's "variable variables"? 15:54
fozzmoo $my_hash{key} = \%otherhash; 15:54
fozzmoo heftig: I'm not that familiar, but probably. 15:54
fozzmoo Okay. I'm just going to keep going until 23:00 and I'll take question as I go. :) 15:55
fozzmoo The 'keys' function returns the keys (of the key-value pairs) of a hash. 15:56
fozzmoo it returns them as a list. 15:56
fozzmoo but you're not guaranteed any kind of order to them. 15:56
fozzmoo So: @keys = keys %hash; 15:56
fozzmoo The example of #13 shows how we can use a foreach loop to iterate over that list. 15:56
fozzmoo A little preview of a future slide. 15:56
* fozzmoo beeps and moves to Slide #14 15:57
fozzmoo Default variables are another weird thing in Perl. 15:57
fozzmoo A little bewildering for people who are unaccustomed. 15:57
fozzmoo Generally, I would advise not using default variables because they present a readability issue. 15:57
fozzmoo but you often need to. 15:57
* fengshaun wonders where the slides are 15:57
fozzmoo http://www.xmission.com/~fozz/perl_basics.pdf 15:58
fengshaun thanks! 15:58
fozzmoo fengshaun: Just wrapping up, but there you go. 15:58
fengshaun yup 15:58
fozzmoo delhage asked earlier about command line arguments. 15:58
fozzmoo They are passed in via the default list: @_ 15:58
fozzmoo So, if you look at the default list at the top of your script, you'll be looking at parameters passed in from the command line. 15:59
fozzmoo If you had a filename as a parameter, you could say: $filename = shift @_; 15:59
fozzmoo Now, you don't have to give @_ to the shift function. if you just say shift; then it will assume you want it to do the shift operation on the default list. 15:59
* fozzmoo beeps and moves to #15 16:00
fozzmoo 15-17 talk about quoting rules. 16:00
fozzmoo Fairly similar to shell, actually. 16:00
* nirik notes we are at the end of the timeslot... but you can go over if you like as this is the last class today. ;) 16:00
fozzmoo interpolation inside double quotes. 16:00
delhage is that like shift in shell scripting? 16:00
delhage right 16:00
* delhage has a bit of a lag 16:00
fozzmoo delhage: yeah, in this specific case, it is. 16:00
delhage ok 16:01
fozzmoo single quotes - no interpolation. So if you put variables inside the quotes, you'll get the literal string back without the variable value. 16:01
fozzmoo Example is shown on #17 16:01
* fozzmoo beeps and slides to #18 16:01
fozzmoo Back quotes are command substitution. 16:01
fozzmoo Just like in shell. 16:02
fozzmoo Nice way to grab the output of a command and assign to a scalar. 16:02
fozzmoo Finally, some control statements. 16:02
* fozzmoo beeps and moves to slide #19 16:02
fozzmoo if-else-elsif 16:02
fozzmoo Prior to Perl 5.10, there was nothing like a switch or case statement in Perl... 16:03
fozzmoo so nested if-elsif structures were really the only way to do what you'd normally do with a case/switch structure. 16:03
delhage oh 16:03
fozzmoo But Perl 5.10 has a given-when structure. 16:03
* fozzmoo beeps and moves to #20 16:03
delhage when was 5.10 released? 16:04
fozzmoo In the last year or so. 16:04
delhage ok 16:04
fozzmoo It's in Fedora 8 and forward, I believe... 16:04
Ritmo2k Heh, perl monks sugegst slide 21 is the wrong way to do this: http://www.perlmonks.org/?node_id=46769 16:04
fozzmoo but RHEL5 and variants are still on Perl 5.8 16:05
delhage yeah, looks wrong 16:05
fozzmoo you mean slide #22? 16:05
Ritmo2k err yeah 16:06
delhage oh 16:06
fozzmoo The for loop in Perl is really no different than foreach()\ 16:06
fozzmoo they just iterate over lists. 16:06
fozzmoo The C-style for(init; condition; increment) is supported. 16:07
fozzmoo But it's not really the "Perl way" 16:07
fozzmoo Ritmo2k: thanks for pointing out how useful PerlMonks.org can be! 16:07
Ritmo2k lol 16:07
delhage did you skip #21? 16:07
fozzmoo Kinda did, didn't we? 16:08
fozzmoo #21 just talks about comparison operators. 16:08
fozzmoo I think it's pretty self-explanatory. 16:08
fozzmoo One thing I didn't include is file tests... 16:08
fozzmoo similar to what shells provide. 16:08
delhage so it is totally the opposite of the test function in shell scripting 16:08
delhage ? 16:08
fozzmoo So you can do if(-f $file) 16:09
delhage where == is equality between strings 16:09
fozzmoo delhage: yeah. it is different. 16:09
delhage and -eq between numbers 16:09
fozzmoo It's more like... Fortran. :) 16:09
delhage damn... 16:09
delhage that's confusing 16:09
fozzmoo Shell deviated from C. :) 16:09
delhage well, fortran doesn't have -eq ;) 16:10
delhage or does it? 16:10
* delhage hasn't written fortran in 15 years 16:10
fozzmoo It's been too long. I was going to say VMS DCL, but nobody would know what I was talking about. 16:10
fozzmoo I 16:10
fozzmoo I'll end there... 16:10
delhage ok, the slide is correct then 16:11
@nirik thanks for the class fozzmoo ! 16:11
heftig thanks for the class, fozzmoo 16:11
fozzmoo and let everyone read on to the built-in functions on slide #25 16:11
fengshaun thanks fozzmoo! 16:11
Ritmo2k Cheers! 16:11
fozzmoo you're welcome! 16:11
fozzmoo I'd love to come back and talk about Perl regex. 16:11
Ritmo2k I would so be in on that! 16:11
SSlater Given I know nothing of Perl bar the last hour, can Perl be used to interrogate a USB RS232 device to aquire data? 16:11
fozzmoo if there's interest, put it in the classroom wiki. 16:11
-!- nirik changed the topic of #fedora-classroom to: Fedora IRC Classroom - Next class at 18:00 UTC 2009-03-08 - See Classroom for class schedule and more info. 16:11
heftig i still still confusing pcre with other syntaxes 16:11
fengshaun that just made me to add Perl to my language inventory 16:11
delhage thanks fozzmoo, but I have one more question if that's OK? 16:11
fozzmoo SSlater: Sure. Check CPAN. cpan.org. :) 16:11
fozzmoo delhage: Sure. 16:11
* Ritmo2k polsihing off oreilly Sed&Awk and RegEx books on desk 16:12
SSlater Thanks fozzmoo, great class. 16:12
* herlo points out that this is the last class so feel free to linger and discuss 16:12
fengshaun fozzmoo: yea, please come back for regex! 16:12
delhage fozzmoo: I'm not really sure how to pose the question though... ;) 16:12
fozzmoo delhage: Think about it and e-mail me later. :) 16:12
delhage fozzmoo: about CPAN and package based distros like fedora 16:12
fozzmoo SSlater: yw 16:12
fozzmoo delhage: Ahhh. 16:12
fozzmoo Yeah. There are a couple programs that will build RPMs for you. 16:13
fozzmoo cpan2rpm 16:13
fozzmoo and cpanspec 16:13
delhage fozzmoo: you probably see where I am going? 16:13
fozzmoo And another one called cpanflute. 16:13
fozzmoo I prefer to build all my CPAN packages as RPMs. 16:13
fozzmoo It can be a pain, but it's worth it. 16:13
fozzmoo Fortunately, for Fedora, most of the good packages are in standard repos. 16:13
delhage I know from experience that it can be a real hell to use a mix of rpms and modules installed via the CPAN shell 16:13
heftig php introduced me to regexps, so i'm most comfortable with the perl-compatible syntax. any others still confuse me :) 16:13
Ritmo2k fozzmoo, when using cpan2rpm, how do you handle the sometimes seemingly endless recursive loop of deps when building out stuff? 16:13
fozzmoo Ritmo2k: perseverance 16:14
delhage fozzmoo: thanks 16:14
Ritmo2k LOL 16:14
Ritmo2k is there no frontend to recursively check for deps? 16:14
fozzmoo That's another thing I've blogged about - it's a barrier to entry for Perl. 16:14
fozzmoo Ritmo2k: Not that I know of, but I would hope someone resolves that. 16:14
fozzmoo Some people have told me they've hacked stuff together to do it... 16:15
fozzmoo but they weren't comfortable releasing it to the world. 16:15
fozzmoo I wish they would. :) 16:15
fozzmoo Thanks all for attending. Good questions. 16:15
delhage thank you 16:15
fengshaun thanks fozzmoo 16:16
fozzmoo yw 16:16
fozzmoo yvvvvw 16:16
Ritmo2k foxx, Ill be keeping an eye out for the regex class:) 16:16
Ritmo2k fozz 16:16
fengshaun me too! 16:17

Generated by irclog2html.py 2.7 by Marius Gedminas - find it at mg.pov.lt!