Fundamentals of Perl training in October, 2010, in Ramat Gan

Sun Aug 22 20:13:51 2010

Bookmark and Share

Tags: Perl, training, Israel

After a long period that I was giving only on-site classes finally I decided that it might be time to start offering the "public registration" classes as well.

It is a lot more work to organize such class as I need to be in touch with several customers and there were cases when people cancelled their participation in the last minute but recently several companies indicated that they would like to send 1-2 people to such class so I hope it won't be that hard

The syllabus of the Fundamentals of Perl training is on the web site of PTI but actually the whole material can be found on my web site.

see comments

Perl 6 subroutines and home made operators

Sun Aug 15 15:10:12 2010

Bookmark and Share

Tags: Perl, Perl 6, learning, newsletter, screencast, subroutines, Rakudo

Welcome back to the Perl 6 Tricks and Treats

This time together with a Perl 6 screencast to show the thought operator and the +- operator of Perl 6. Direct link to the Perl 6 subroutines screencast


This entry was first sent out as part of the Perl 6 Tricks and Treats. Visit here to subscribe.
It was also included the Perl 6 series of screencasts.

The Thought operator

I just tried the thought operator in Perl 6. Typed in this:

  "szabgab" .oO "Perl 6 is cool";

and got the following:

  szabgab thinks Perl 6 is cool

The +- operator

I also tried the +- operator:

  2.78  ~~ 10 +- 3 ?? 't' !! 'f'     # f
  7.5   ~~ 10 +- 3 ?? 't' !! 'f'     # t
  13    ~~ 10 +- 3 ?? 't' !! 'f'     # t
  13.1  ~~ 10 +- 3 ?? 't' !! 'f'     # f

I think you get the point here. We check if the value on the left is between 10 +- 3.

~~ is the smart match operator, ?? !! are the two parts of the ternary operator of Perl 6 and +- is, well not everyone has the +- or the thought operator but if you read on you'll see how you can create one for yourself.

Subroutines in Perl 6

Subroutines in Perl 6 are declared using the sub keyword just as in Perl 5 But in Perl 6, after the name of the subroutine one can provide a signature, describing the parameters that function accepts.

    use v6;

    sub f($a, $b) {
        return "$a - $b";
    }

    say f(4, 2);

If called with a different number of arguments then Perl will issue an error message:

    say f(4, 2, 5);

gives this message:

    Too many positional parameters passed; got 3 but expected 2
      in 'f' at line 3:script.p6
      in main program body at line 8:script.p6

or

    say f(4);

gives this message:

    Not enough positional parameters passed; got 1 but expected 2
      in 'f' at line 3:script.p6
      in main program body at line 8:script.p6

Perl 5 style in Perl 6

For people who really want the Perl 5 style subroutine declaration they can use this code:

    use v6;
    
    sub f {
        return @_.perl
    }
    
    say f(4, 2);     # [4, 2]
    say f(4);        # [4]
    say f(4, 2, 3);  # [4, 2, 3]

but that would eliminate all the nice features of Perl 6.

Optional parameter

What if we would like to allow the user to pass 1 or 2 parameters? For that case we can mark the second parameter as optional:

    use v6;
    
    sub f($a, $b?) {
        return defined $b ?? "$a - $b" !! "$a - na";
    }
    
    say f(4, 2);    # 4 - 2
    say f(4);       # 4 - na

The ?? !! is a the ternary operator of Perl 6 so what we see above is that if $b is defined the "$a - $b" is returned and when $b is not defined then 'na' will be returned instead of that value.

Parameters are read-only

In other cases you might be tempted to replace $b with some default value like this:

    use v6;
    
    sub f($a, $b?) {
        $b //= 17;
        return defined $b ?? "$a - $b" !! "$a - na";
    }
    
    say f(4, 2);    # 4 - 2
    say f(4);       # 4 - na

but this will throw an exception like this:

    Cannot modify readonly value
      in '&infix:<=>' at line 1
      in 'f' at line 4:02.p6
      in main program body at line 8:02.p6

as arguments in Perl 6 are by default read only;

Default value of a parameter

There are two solutions to this, depending on what do you really want to achieve: If you only want to give a default value to $b then the best way is to add it right in the signature:

    use v6;
    
    sub f($a, $b = 17) {
        return defined $b ?? "$a - $b" !! "$a - na";
    }
    
    say f(4, 2);    # 4 - 2
    say f(4);       # 4 - 17

In this case you don't even need the question mark '?' as the presence of a default value automatically makes the parameter optional. This still keeps $b read-only within the subroutine.

Making the parameter a copy and thus changeable

The other solution is to mark the $b variable as a 'copy' of the original value. Therefore allowing the user to make changes to it:

    use v6;
    
    sub f($a, $b? is copy) {
        $b //= 17;
        return defined $b ?? "$a - $b" !! "$a - na";
    }
    
    say f(4, 2);    # 4 - 2
    say f(4);       # 4 - 17

Type restrictions on parameters

We can also restrict the parameters of a subroutine to certain data types:

    use v6;
    
    sub f($a, Int $b) {
        return "$a - $b";
    }
    
    say f(4, 2);
    say f(4, "foo");

The first call will succeed but the second will generate an exception:

    4 - 2
    Nominal type check failed for parameter '$b'; expected Int but got Str instead
      in 'f' at line 4:03.p6
      in main program body at line 9:03.p6

Other constraints on parameters

It is also possible to provide further constraints on the parameters:

    use v6;
    
    sub f($a, Int $b where { $b < 10 }) {
        return "$a - $b";
    }
    
    say f(4, 2);
    say f(4, 11);

    4 - 2
    Constraint type check failed for parameter '$b'
      in 'f' at line 4:03.p6
      in main program body at line 9:03.p6

Creating operators in Perl 6

There are of course a lot of other aspects for subroutine definition in Perl 6 but let's now go back to our original goal, to create the +- operator.

So how can one create a new operator in Perl 6? It is quite simple as an operator is just a subroutine with a funny way of calling it.

So here is how to create the thought operator. We create a subroutine, declaring it to be an infix operator with the name .oO

    sub infix:<.oO>($name, $thought) {  
        say "$name thinks $thought"
    }



Specifically we want to make +- an infix operator that create a Range. Not something complex. Within the angle brackets we put the new operator, the sub has a signature of two scalars and within the block we put actual code that needs to be executed.

    sub infix:<+->($a, $b) { ($a-$b) .. ($a+$b) }


Then we can use

   5 +- 2

that will create the range 5-2 .. 5+2 also known as 3 .. 7

Comments and Discussion

I am always open to comments and criticism (just have a positive spin to it :-) So if you find any issue with the examples, please don't hesitate to let me know.

If you'd like to ask question about Perl 6, probably the best would be to sign up on the Perl 6 users list by sending an e-mail to

    perl6-users-subscribe@perl.org

The archive of the perl6-users list is at: Perl6.org

The even better way is to join the #perl6 IRC channel on irc.freenode.net If you have not used IRC yet, the easies way is to use the web based IRC client of Freenode

Previous issues of this newsletter can be found at Perl 6 Tricks and Treats.

see comments

More Perl events in 2010

Thu Aug 12 01:21:45 2010

Bookmark and Share

Tags: Perl, events, OSDC, Perl Workshop

Yesterday I wrote about tech events with some Perl content Perl::Staff - Upcoming events for promoting Perl. This time let me list the upcoming Perl events:

The Pittsburgh Perl Workshop 2010 will take place between 9-10 October in, well, Pittsburgh, Pennsylvania.

OSDC.fr is not strictly a Perl event but AFAIK it is organized by the French Perl Mongueurs. It will be on the same dates 9-10 October, in Paris, France.

YAPC::Asia October 14-16 in Tokyo, Japan.

The Austrian Perl Workshop will take place later this year in Vienna, Austria. (no fixed date yet).

OSDC Australia will take place betwee 24-26 November, 2010 in Melbourne.

The Russian Perl Workshop between December 18, 2010 in Saint Petersburg, Russia

The London Perl Workshop usually takes place in December. No website yet.

That's it for 2010 as far as I know.

see comments

Perl::Staff - Upcoming events for promoting Perl

Tue Aug 10 19:56:52 2010

Bookmark and Share

Tags: events, Germany, Switzerland, Spain, Holland, promotion, Perl

As I mentioned during my lightning talk in Pisa there are a number of tech events in Europe where it would be interesting to have Perl presence. The full list of events that I am aware of can be found on the events wiki page and the Perl presence is being organized for some of them already.

If you have time it would be nice to help out the organizers or if you can attend other events, then to organize something there or if you know about more events, please add them to the wiki.

Here is what is planned so far:

Froscon in St Augustin, Germany will take place 21-22 August 2010. Organized by Renée Bäcker there is going to be a full track of Perl talks.

FrosCamp in Zurich, Switzerland will be 17-18 September 2010. Oranized by Renée Bäcker, as I can see there is going to be a Perl booth and maybe a couple of Perl related talks. The dead-line for talk submission is 2010-08-14. Just a few days from now.

OSWC in Malaga, Spain between 27-28 October 2010. The organizers, Salvador Fandiño, Diego Kuperman, JJ Merelo, Rodrigo de Oliveira are planning to have a Perl track.

Brandenburger Linux-Infotag in Potsdam, Germany on 6 Nov 2010. Organized by Renée Bäcker they are stll looking for more people to help out at the Perl booth and give Perl related talks.

T-Dose in Eindhoven, Holland will be between 6-7 November 2010. I've just started to organize the Perl presence there. Let me know if you can help out.

see comments

YAPC::EU 2010 Pisa

Mon Aug 9 09:54:38 2010

Bookmark and Share

Tags: Perl, YAPC, conferences, hackathons

I really enjoyed myself in Pisa. Thanks for all the organizers of YAPC::EU and the speakers and all the attendees. The event was really awesome!

After spending most of my time at YAPC::NA in the hallway track trying to talk about my grant proposal and after being discouraged by some of the negative comments there I decided to try to spend a lot more time in talks and not to initiate discussion about it. Still many people stopped me in breaks and asked me about my grant. I tried to give them a status report and in most cases they tried to encourage me to keep going with it.

I also talked to Allison Randal about the grant and as far as I understand she said the TPF will never approve my grant. She did not say what is her opinion and if she would vote for it, but she was one of the people who told me a few month ago that instead of trying to setup a separate organization for this I should do it within The Perl Foundation.

Actually I think she said that the only thing that can bring Perl back to its fame is a company doing a very interesting and high-profile project in it. Similar to how the work of 37 Signals releasing Rails made Ruby interesting.

Allison also said that instead of asking for a grant I should just organize more events and try to raise money for them from companies. I just wonder, if I take all the risk then why should that be within TPF?

After her advantures in getting to YAPC, finally Karen Pauley and me also had some time to discuss the grant. I think this was very important thought I am not sure we have managed to move forward. At least now I understand much better the varous things she needs to ballance as the president of TPF. Frankly, I don't think many people would be able to do that and I am quite afraid that she will burn out and then the TPF will stop making any progress.

I won't list here all the other nice people I met at YAPC as that would take ages but I'd like to say again thanks to the organizers and all the people who participated!

Some pictures of YAPC::EU 2010 in Pisa.

see comments

Perl 6 screencast - part 5 - hashes

Sat Jul 31 14:49:30 2010

Bookmark and Share

Tags: Perl 6, Perl, screencast, hashes

Direct link to the Perl 6 screencast about hashes

See more Perl 6 entries.

Perl 6 Code examples

Hashes in Perl 6 are denoted using % sign: Creating a hash

  my %h = "Foo" => 1, "Bar" => 2;
  

printing it out for debugging purposes:

  say %h.perl;    # {"Foo" => 1, "Bar" => 2}

printing the value of a single key:

  say %h{"Foo"};  # 1
  

The quotation marks are required around the string:

  say %h{Foo};    # Could not find sub &Foo

You can use variables without quoting them:

  my $name = "Foo";
  say %h{$name};   # 1

Recall from the previus section that you can use the angle brackets to quote a number of strings and create a list of them:

  say <Foo Bar>;   # FooBar
  

This notation can be used to eliminate the need for quotation marks:

  say %h<Foo>;     # 1

You can actually use multiple keys there fetching a list of values of a hash slice:

  say %h<Foo Bar>;  # 1 2

Using the "perl" method just further proves that you get back a list of values

  say %h<Foo Bar>.perl;  # (1, 2)

In order to fetch all the keys you can use the ".keys" method and then you can iterate over the individual keys and fetch the respective values:

  for %h.keys -> $k { say "$k %h{$k}"; }

There is also a ".values" method that will return the list of values. Even though in our case the values are unique in the general case you cannot easily get back the keys from the values:

  for %h.values -> $v { say $v; }

In case you prefere to iterate over the key-value pairs for that you can use the ".kv" method:

  for %h.kv -> $k, $v { say "$k - $v"; }

To add another key with its value you can use the following code:

  %h<Moo> = 3;

  say %h.perl;    # {"Moo" => 3, "Foo" => 1, "Bar" => 2}

If the key already exists this will replace the value in the hash:

  %h<Moo> = 4;
  say %h.perl;   # {"Moo" => 4, "Foo" => 1, "Bar" => 2}

On the other hand in Perl 6 you can also use the ".push" method on a hash that will add the value to the given key creating an array as the value of that key:

  %h.push( 'Moo' => 5 );
  say %h.perl;   # {"Moo" => [4, 5], "Foo" => 1, "Bar" => 2}


see comments

Happy 2nd birthday to Padre - Get on an IRC channel

Sat Jul 24 15:55:45 2010

Bookmark and Share

Tags: Perl, Padre, screencast, IRC

The IRC redirector on the Padre website. Type in some username and select the channel.

If your perl related channel is missing from the list let us know on #padre irc.perl.org

I hope you'll come by the Padre channel and say happy birthday to the developers!

Direct link to the screencast

see comments

How to connect to the #perl6 IRC channel and try Perl 6 on-line

Fri Jul 23 17:45:44 2010

Bookmark and Share

Tags: Perl, Perl 6, screencast, tutorial, IRC


Direct link to the screencast

See more Perl 6 entries.

web interface to Freenode. Pick some username and type in the name of the channel: #perl6

see comments

Perl 6 screencast - part 4 - files

Fri Jul 23 02:05:45 2010

Bookmark and Share

Tags: Perl 6, Perl, screencast, files


Direct link to the Perl 6 files screencast

See more Perl 6 entries.

Perl 6 Code examples

Reading in all the lines of a file in a single scalar using the slurp function:

  use v6;

  my $content = slurp "text.txt";
  say $content.chars;


Reading in all the lines of a file into the first element of an array using the slurp:

  use v6;

  my @content = slurp "text.txt";
  say @content.elems;
  say @content[0].chars;

Reading in all the lines, each line in a separate element of the array using the lines function:

  use v6;

  my @content = lines "text.txt";
  say @content.elems;
  say @content[0].chars;

Iterating over the lines one by one. lines is evaluated lazily here:

  use v6;

  for lines "text.txt" -> $line {
    say $line.chars;
  }

Opening a file using the open function. Reading in one line using the get method. Iterating over the rest of the file using the lines method:

  use v6;
  my $fh = open "text.txt";

  my $first_line = $fh.get;
  say $first_line;

  for $fh.lines -> $line {
    say $line.chars;
  }


Opening a file for writing, printing a string to it using the say method and then closeing it to flush all the buffered output. Then re-reading the file just to show what happened.

  use v6;

  my $fh = open "out.txt", :w;
  $fh.say("text 4");
  $fh.close;

  say slurp "out.txt";



see comments

Perl 6 screencast - part 3 - arrays and ranges

Thu Jul 22 11:38:18 2010

Bookmark and Share

Tags: Perl 6, Perl, screencast, arrays, ranges

Direct link to the Perl 6 arrays and ranges screencast

See more Perl 6 entries.

Perl 6 Code examples

  use v6;
  my @people = ;
  for @people -> $n {
    say $n;
  }


  use v6;
  my @people = ;
  for 0 .. @people.elems/2 -> $i {
    say "@people[$i*2] -  @people[$i*2+1]";
  }


  use v6;
  my @people = ;
  for @people -> $name, $phone {
    say "$name - $phone";
  }


  use v6;
  my @names = ;
  my @phones = <123 456 789 512>;
  for @names Z @phones -> $name, $phone {
    say "$name - $phone";
  }


  use v6;
  for 1 .. 10 -> $n {
    say $n;
  }


  use v6;
  for 1,3 ... Inf -> $n {
    say $n;
    last if $n > 10;
  }


see comments
Follow szabgab on Twitter
Tags
Perl (270)
Perl 5 (94)
Padre (79)
Perl 6 (42)
IDE (41)
testing (38)
CPAN (28)
business (27)
newsletter (24)
marketing (23)
training (20)
TPF (17)
open source (17)
Windows (17)
promotion (17)
Parrot (16)
YAPC (16)
Israel (15)
grants (15)
Python (14)