|
Why am I writing Padre?
Thu Jul 2 09:24:31 2009
Tags: Padre, Perl, Perl 5, Perl 6, IDE, Rakudo, Parrot
It is probably not that interesting for most of the people but I have my excuses.
What is more interesting - at least to me - is why others are involved.
After all there were already more than 40 people who made some contribution to Padre.
So I asked them to start writing down why are they involved.
Some of them have already added themselves to the
developers page
and we started a separate page for
translators though that
one is new and has no been filled yet.
So why do I write Padre?
There is a short explanation on that page but let me bore you with a longer one
here.
I have been teaching Perl 5 for almost 10 years now. Both beginner level and
advanced courses. In the beginner courses the majority of the people use
Windows with about 20-30 percent using Linux. Most of the Windows users use Notepad++
or a similar editor. Some of them can configure their editor to run the perl script
right from the editor, others don't even know how to enable syntax highlighting
for Perl 5. Some of the people on Windows don't know what the command line is but
even those who know how to open the command prompt are either afraid of it or
just dislike it.
Mind you these are not stupid people or anything like that.
Some of them have many years of hardware design behind them.
They are just used to some kind of IDEs.
Others might use Linux/Unix but many not by choice. In many cases the company
they are working for gives them telnet access to an oldish Unix machine and
tell them to code in Perl. They hardly know vi, they don't know how to configure
syntax highlighting and they don't know how to install any other editor.
I have been using vim for many years and I can testify that it is a superb editor.
Emacs is similary strong but I have not used it since university. The problem
with both of them is that they are totally different from the editors most
people are used to and their learning curve is long and steep. It takes
several months or even years to become familiar with them. Most of the people
don't want to invest that energy and I certainly don't have the time for that
in a 4 days long Perl 5 course.
So I usually tell them to use whatever they are familiar with. I think I never had
a student who picked Eclipse or Emacs. There were a few using vi/vim
but most of the people used Notepad++ or a similar Windows based editor.
I don't think my students are too different from the average people learning and
using Perl. Most of them will never write a full blown web application. Heck most
of them won't have a need for object oriented coding as they write only
100-200 line long scripts in Perl.
They never get really familiar with Perl and they will always have to deal with
strange code written by others. With all the other tasks they are required to do
in their primary language or tool they will keep wondering what is $_ when they see
it and will be surprised when they don't.
They will wonder what is the relationship between $_ and $_[0].
So my first objective was to provide a simple Notepad++ like editor with strong
support for beginners or people with relatively little experience with Perl.
For example I'd like to make sure that Padre users will be able to highlight $_,
press F1 and get an explanation. In case they highlight $_ from a $_[0] expression
Padre should be clever enough to point this out and give an explanation on @_ and
@_[0].
I knew it is a big task though I did not think it is soo big. In any case I knew
I cannot handle this alone, especially as I am not such a good programmer.
So from the very beginning I tried to make it interesting, accessible
to experienced Perl 5 developers and in turn they made it
fun and hackable.
Unfortunatelly most of the experienced Perl developers are also hard-core vim or
emacs users and it is nearly impossible to move them away from their editor.
Luckily there were a few who got interested by the idea of having a lot
of control over their editor and that made the difference between a failing
one man project to a project with a lot of potential.
Then at one point came the idea to integrate Parrot and Rakudo and try
to provide a platform for Perl 6 development as well. With the awesome
work of Ahmad M. Zawawi
Padre has a lot to offer to anyone who wants to write Perl 6 code. With some more
work we'll be able to integrate Parrot even better and then Padre will be
able to handle any language running on top of Parrot.
While there are still lots of problems with Padre and installation can still
be difficult it already more or less has the same power as Notepad++ on both Windows
and Linux and probably on Mac too and it has some extra features that even
vim people started to steal.
see comments
Test Reporting system: Smolder wish-list
Wed Jul 1 10:24:40 2009
Tags: TAP, testing, Perl, test reporting, PostgreSQL, PHP, Smolder, TODO
I'd like to have a system that can hold all the artifacts of a test execution
and that also knows a lot about TAP.
Smolder is a good start as it knows about TAP but there are tons
of other things I'd like to have in it.
Some of them are hard to add, others are harder.
Anyway, earlier I wrote about Reporting Test Results.
Now let me write about my wish-list:
1) I'd like Smolder to collect all the artifacts of a test run.
Look at the data Buildbot collects or
what is collected by the
PostgreSQL test farm!
Each run has several stages and the actual test run is only one of them.
In a Test Reporting system I'd like to have access to all the output and error files
of all the stages.
2) The tests might create some additional log files, I'd like to be able to include those too.
3) One major issue we have is test counting. TAP
encourages you to have a plan - the number of test
units you are going to execute in a test run - so that it can check the number of
actually executed test to that number. Keeping the plan up to date is annoying,
people don't like it so many use no plan.
Some TAP producers don't even let you plan ahead (e.g. the ones I saw in PHP) but I think
generally those that are more xUnit like won't have a test plan.
In larger settings this plan does not even help as it does not provide any
tools to avoid reducing the test plan accidentally along with a shortcut in
the test script:
A developer can comment out several tests temporarily and adjust the plan
for the time while he is trying to fix a bug forgetting to revert these changes
after he is done.
Smolder could help a bit here:
On the first result report the administrator of the project should look at the
reports and mark them as having the correct number of tests. Then if Smolder
notices a test report with a different number of test for the same test file
it should raise an alert. The project administrator can examine the change
and decided if that is the new correct number of tests setting the new expected number
to this or if it was changed by mistake.
4) History of unit tests. If we would like to be able to provide a history
based on the actual test units (calls to an ok() function) then we need to
have some unique identifier for each test as we cannot assume that the test
number or the test name will remain the same. After all people sometimes insert
new tests or remove old test at the beginning of the test scripts and
the names can change.
Smolder could also help here too. It could track the test numbers and test names.
If it sees a change it can alert the project administrator who can then
use some GUI to connect the old test units to the new ones.
Both involves some manual work but I think large organizations will be
ready to invest that work in order to get the benefit of greater control
and better reports.
What would you like to have in your test reporting system?
see comments
The Ubuntu Business model and Perl
Tue Jun 30 09:58:29 2009
Tags: Perl, Ubuntu, business, economy, Canonical, training, certificate
After my little research into the Eclipse foundation (Comparing the Eclipse Foundation with The Perl Foundation and EPO) I was
told to take a look at the success of Ubuntu.
While I know there is a huge difference as Ubuntu is an operating system while
Perl is just a language but I got interested and here are the results.
Ubuntu does not have a foundation. It has a strong and very enthusiastic
community and it is backed by Canonical,
the company of Mark Shuttleworth.
That means the Perl community probably cannot learn much from the way Ubuntu and
Canonical are connected but we can still look at the larger picture of how
Ubuntu succeeded so far?
First I'll look at the business model of Canonical and later on the elements
that helped Ubuntu become one of the leading GNU/Linux distributions in only
a few years.
The Ubuntu Business model
So if we look closer I think we can safely say that Ubuntu is a product
Canonical is sponsoring in order to make money somehow. I would not say by selling
added value service as I am not exactly sure what is their business model.
I noticed a few areas where Canonical might be making money:
- Selling wearables and accessories.
- Providing support contracts at nice sums.
- Providing Training and Certification.
- Distribution mechanism for software.
Selling T-shirts can bring in nice revenue to a small company but for Canonical it is more
part of the marketing mix than the revenue mix.
Providing support can be an interesting business model, IBM makes a lot of money on it.
I guess Canonical does too. This will just increase as Ubuntu invades the corporate
infrastructure.
Training and Certification can be a lucrative business if executed well and I trust
them to do it well. If they can convince companies that they need Ubuntu Certified
Engineers to run Ubuntu smoothly then will see a huge number of people attending their
classes and taking their certificate exams.
Still the biggest business opportunity I think is the one that is not even listed in the
Canonical Store but only under
Services.
That is, they offer to be a distribution channel for ISVs.
In the Microsoft world people are used to download software and click-click-click
install it. In the GNU/Linux world almost all the important software is packaged and
distributed by the Linux distributor. It is especially true for Debian/Ubuntu but
I think it is increasingly so with Fedora and Mandriva as well. That means the most
common installation mode in these versions of Linux is via a mechanism similar
to the "Add/Remove Software" on Windows.
So if you have a software which you'd like to distribute to Linux users, one of
the best ways is to make sure each one of the Linux distributors will package your
software. Otherwise you'll need to build .deb and .rpm files for each distribution
or a stand-alone executable which is frowned upon by most of the Linux system
administrators.
If your software is not open source you don't have much chance that any of the
Linux distributors will add that package to their repository making
it almost impossible to penetrate the Linux Desktop market.
If Canonical manages to get a large chunk of the market share in the Linux desktop
market it effectively can open or close the distribution channel of ISVs making them pay
well for the right to use Ubuntu as their distribution platform.
So what will happen to Ubuntu in 5 years?
I don't know.
If you look at the background of Mark Shuttleworth, he built
Thawte and more or less
at the peak of the dot.com bubble he sold it to VeriSign the only
competitor in the digital certificates market. So he is
a clever guy who won't be afraid to sell Canonical to the
market leader with a
near monopoly in the desktop business or to the
competitor with the most money making a
few billions of dollars.
The sky isn't the limit
The Perl aspect
Perl is not an operating system and it is not the open source arm of a company.
It cannot be sold but it can provide nice revenue streams to individuals or
companies offering service with Perl. There might be other revenue sources as well
but lets look at the 4 revenue streams that Canonical has:
I'll write about the success of Ubuntu in a separate post.
for the meantime you can read the
Ubuntu Story
between 2004 and 2006. They don't keep their history up to date
either.
see comments
Perl 5 Personal Service
Sun Jun 28 12:34:05 2009
Tags: Perl, Perl 5, TODO
I thought about this on and off for some time but have not
done anything with it yet. Maybe someone else with better ideas
can do something like this.
I was sondering if it would be interesting to create a web application
that on one hand would allow people using Perl to get some personalized
services on the other hand it would allow the ongoing collection of some
statistical data about the Perl community and about the usage of Perl.
The services that would be offered could include alert when one of the modules
used by the person gets a new version. It could include a Google-alert like
service when the modules are mentioned in one of the perl related sources.
It could allow the notification about Perl related events in their city/country/continent.
It could allow people to mark their favorite modules.
It could allow to follow other people whose judgement
they trust for modules which in turn would provide a better way to search for modules
on CPAN for everyone.
It would allow an on-going survey similar to the one that was a year or two ago but
that would allow people to change the data they provide and allow the site maintainers
to provide information regarding the changes as well.
see comments
Padre 0.37 released
Thu Jun 25 10:31:17 2009
Tags: Perl, Padre, IDE
Without fanfare, v0.37 of Padre has been released.
The list of changes based on the Changes file:
- "Introduce Temporary Variable" refactoring tool (SMUELLER)
- Added a friendly icon on the toolbar to toggle comments (GARU)
- Crazy Win32::API AllowSetForegroundWindow hack to allow the
Single Instance Server to correctly foreground itself (ADAMK)
- Added Padre::Search search and replace API (ADAMK)
- Switching to last edited file is now Ctrl-Shift-p (SZABGAB)
- Be compatible with older version of File::Path (RYAN52)
- Links to Mibbit were replaced by links to our irc.html (SZABGAB)
- Merged the code of Padre::Plugin::Encode into Padre (SZABGAB)
- Update directory only when switching to new editor (SZABGAB)
Soon it should arrive to a CPAN mirror near you.
Enjoy and thanks to all the people who put effort in Padre!
see comments
Things I am missing from Iron Man
Tue Jun 23 23:26:46 2009
Tags: ironman, Perl
The main objective of the
Iron Man Blogging Challenge
is to encourage more people within the Perl community to promote the
language to the world outside the Perl echo-chamber
There are nice things in Perl but I think it is hard to promote the language itself.
Indeed, most posts were not about how awesome you can write a foreach loop in Perl...
There were posts about Moose which is ok as
it shows to the outside world that Perl has a nice object system - even if it is
an add-on but not many people will start using Perl because it has an nice object system.
There were posts about various little modules that can do interesting things. Things
that can be interesting to people not yet using Perl. E.g. about
Curses::Toolkit
and Politics::AU::Geo ).
The former while not on CPAN yet had nice demos, the
latter pointed to the actual result page.
There were also lots of posts about Catalyst that
can be interesting as a reason to learn Perl.
So while I enjoy reading the posts what I am missing is more posts on subjects
that can be intersting for people who do not yet know Perl. I am missing
both things that need some perl knowledge to actually use (such as the above 3 examples)
but it would be nice to see posts about projects/products that do not require knowledge
of Perl to start using. Projects that are written in Perl and that for advanced usage
might require Perl knowledge. These would be reasons to start learning Perl.
So for example I'd like to see posts about
Bioperl,
POE,
PDL,
Bricolage,
Krang,
SpamAssassin,
Ack,
WebGUI,
RT,
Gantry,
Webmin,
and even Bugzilla
just to name a few.
What are you missing?
see comments
When is the next release of Perl?
Mon Jun 22 16:25:22 2009
Tags: Perl, Perl 5, release, testing
In an open source project - especially one that involves Perl -
most of the people just want to write code. Nearly no one wants
to do PR. It even has a bad reputation among many open source
developers.
In Padre I constantly struggle if I should spend my time adding a new
feature or trying to recruite more developers. The former works
better in the short term, the latter works better in the long term.
Eventually there is a ballance I try to keep.
One of the things I really miss from the perl 5 porters - the people
who maintain and improve perl itself - is the communication.
In the last couple of weeks we saw lots of blog-posts and mailing lists
posts - sometimes not in the nicest way - why there is no regular release of perl 5 and how
a speed related bug that was fixed 18 months ago was not released
in an intermediate version. In these discussions several of the p5p
people got involved but unfortunatelly this is mostly deffensive talk.
Not good PR.
I also got fed-up with the lack of release and asked on the p5p list what
is stopping them from releasing the next version of perl and what is so hard
in the release process that they cannot do it more regulary?
I got several related replies. One of the them on the mailing list others
on IRC.
According to those the next perl 5 release is within a small number of weeks.
That is certainly good news. I would prefer to see a Release Candidate already
that people can take for a ride but in the meantime I can point you at the three major issues
around the release that need to be done:
- Fix a bunch of outstanding issues.
- The release must be stable.
- The actual release process should be automatic.
For item no. 1 you need to talk to the perl porters or look at the topic on the #p5p IRC channel where they point you
at the list of outstanding issues
Item no. 2 was addressed in an e-mail of
David Golden and the
thread following it. That should be summarized in a document and the test automation system should be implemented.
For item no. 3 we first need to have a written document of what needs to be
done and then we can attempt to automate it. The document can be based on the e-mail of
Nicholas Clark.
Actually I already created a pod version of that file that I hope soon will be added to the source tree of perl
and then it can be fine tuned and implemented.
The interesting issue is that 2. and 3. need to be implemented only once and they can be reused for every
future release of perl 5. So if you'd like to see more frequent releases that's where you need to help.
Luckily those, especially no. 2, are issues that any perl hacker can get involved easily.
They only need perl coding.
I know I am not a good PR person and I hardly know anything about what's going on in the perl 5 porters
but I hope this post helped a bit. At least it might trigger someone to write a better explanation of
the situation.
Oh and a last note, if you'd like to get involved, the description on how to deal with the
Git repository can be found here
see comments
Live Help - IRC channels
Sat Jun 20 14:28:15 2009
Tags: Perl, Perl 5, IRC, Padre, Mibbit, Freenode, Strawberry Perl, win32
In the last month or two some of us got really enthusiastic about the
Mibbit service and how it can be a
relatively easy way to direct more people to the IRC channels where
we can provide quick help. I got so enhusiastic that I wrote a blog post
( What is the last element of an infinite list or how to get started with Perl 6 ? ) trying to direct people interested in Perl 6 to
use that service.
Adam Kennedy wrote about how we added
"Live support" to Padre.
Two days ago I even added a icon the Strawberry Perl
distribution that will open a browser to Mibbit to join the #win32 IRC channel.
Then yesterday Freenode has
shut
down the access to its service using the
Mibbit web IRC client and offered their own webchat.
That's bad. Not only do I have to fix my blog entry - which is easy but disturbing - but
it also means all the current users of Padre 0.35 have a broken "Live Service" link in their
application. Luckyly, there are not many such people.
So we have to fix the link in Padre and
release a new version. that of course is not enough as I would like to avoid another
such event so instead of pointing to an external service we are going to point to a
web page on our site that will do the redirection automatically. It can even provide us
with another chance to explain what is waiting for the unexpecting user.
That way if the URLs change or services get shut down
we only need to update a page on our web site and the users won't need to know about the change.
So I started to build a page in JavaScript that will redirect the users to the relevant web
based IRC channel. I added a few channels that were listed on
irc.perl.org and a few that were not.
I am sure there are lots of other Perl related channels out there that could be added
to the list. Especially I am interested in locale specific channels so we will
be able to direct the Italian speaking users to the Italian IRC channel.
Please check out this page that I now call the Perl IRC Jumper.
By looking at the source you can see the list of all the channels we currently have.
If you know about other channels that you'd like to see there, either post a response here
or drop by the #padre channel and
let me know what else to add.
see comments
Perl 5 to Perl 6 - Arrays
Wed Jun 17 11:50:25 2009
Tags: Perl, Perl 6, newsletter, arrays Abstract
Continuing the Perl 6 Tricks and Treats
newsletter to help people switch from Perl 5 to Perl 6. Looking at arrays.
This entry was first sent out as part of the
Perl 6 Tricks and Treats.
Visit here to subscribe.
Welcome back to the Perl 6 Tricks and Treats
It was interesting to see the responses to the previous posting.
There were several comments regarding the Object Oriented part
or the lack of it.
On one hand I believe there are many Perl programmers who
don't write classes and they only use Object Oriented Perl
when they use a module from CPAN that is implemented that way.
Even then it is only a matter of different syntax and they
don't use any of the actual features of OOP such as inheritance.
For them saying that everything is an object in Perl 6 and you
write $name.lc might be strange and discouraging.
On the other hand there are many people in the Perl community
who are very familiar with OOP and then there are those coming
from Java/.NET/Python/Ruby who might only use Object Oriented
and would be disappointed if they saw the procedural nature of
Perl 6.
So we have to be careful when we introduce Perl 6 to make it
clear that both ways are working. You can keep writing in procedural
style
say(substr($str, 3, 2));
or use Object Oriented style:
$str.substr(3, 2).say;
or you can mix them if you really want:
say $str.substr(3, 2);
but probably I should not show that in class.
That's for teaching.
As for the language itself, maybe there should be a way to enforce
the Object Oriented style so organization can decide they want
everything to be written in OOP and then enforce it by writing
something similar to this at the top of every file:
use oop;
Food for thought.
Perl 5 to Perl 6 : arrays
In the previous posting I wrote about scalars. Remember there is
a lot more you can do with scalars, I just listed the things that
seemed to be the most important in Perl 5 and how do they translate
to Perl 6 !
This time I'll write about arrays.
Creating arrays, Data::Dumper, debug prints
Creating an array in Perl 6 can be the same as in Perl 5
For debugging prints one will use the .perl method
instead of some Data::Dumper.
use v6;
my @numbers = ("one", "two", "three");
@numbers.perl.say; # ["one", "two", "three"]
In Perl 6 there is no need for the parentheses around lists:
use v6;
my @digits = 1, 3, 6;
@digits.perl.say; # [1, 3, 6]
quote word qw() is no longer used
The Perl 5 qw() operator is replaced by the angle brackets:
use v6;
my @names = <foo bar baz>;
@names.perl.say; # ["foo", "bar", "baz"]
Array interpolation in strings
Arrays do NOT interpolate any more in double quoted strings:
use v6;
my @names = <foo bar baz>;
say "@names"; # @names
So you can safely write the following without escaping the at mark:
use v6;
my @names = <foo bar baz>;
say "joe@names.org"; # joe@names.org
If you do want to interpolate the values of an array you have
to put it inside curly braces:
use v6;
my @names = <foo bar baz>;
say "names: {@names}"; # names: foo bar baz
Elements of an array, sigil invariance
When accessing elements of an array in Perl 6 the sigil does
NOT change! This will be very strange for Perl 5 programmers but it
has some advantages in the long run.
use v6;
my @names = <foo bar baz>;
say @names[0]; # foo
Number of elements in an array
Instead of the quite ugly $#array notation used in Perl 5
(that returns the highest index in the array)
fetching the number of elements of array is done in Perl 6 by the
elems() function.
Actually I think the object oriented writing is much nicer here:
use v6;
my @names = <foo bar baz>;
say elems @names; # 3
say @names.elems; # 3
Ranges
Ranges are available in Perl 6 just as in Perl 5:
use v6;
my @d = 1..11;
@d.perl.say; # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
It works with characters as well
use v6;
my @chars = ('a' .. 'd');
@chars.perl.say; # ["a", "b", "c", "d"]
for and foreach loop
The C-style version of the Perl 5 "for" loop is now called "loop"
but I won't show it here as it is way better to use the
the iterator style for loop that most people in Perl 5 write as "foreach".
In Perl 6 it is spelled as "for" and looks like this:
use v6;
for 1..3 -> $i {
say $i; # 1 # 2 # 3 (on 3 lines)
}
The same works on arrays as well:
use v6;
my @names = <foo bar baz>;
for @names -> $n {
say $n; # foo # bar # baz (on 3 lines)
}
This is one of the cases where you don't need to declare a variable
using "my". The looping variable is automatically declared for you
and scoped to the block of the for loop.
Iterating over the indexes of an array
If you need to iterate over the indexes of an array then you could write
use v6;
my @names = <foo bar baz>;
for 0..@names.elems -1 -> $i {
say "$i {@names[$i]}"; # 0 foo # 1 bar # 2 baz (on 3 lines)
}
Or you can pick one of the suggestions I got from Moritz Lenz
on #perl6 (Thanks Moritz!):
use v6;
my @names = <foo bar baz>;
for @names.keys -> $i {
say "$i {@names[$i]}"; # 0 foo # 1 bar # 2 baz (on 3 lines)
}
The keys() method borrowed from the hashes will return all the indexes
of an array. Even if your array has "holes" in it, elements with undef
as value, keys() will still include the indexes of such elements.
split
split() acts similar to its Perl 5 version but the defaults do not
apply any more and you should read the spec anyway.
See S32-setting-library/Str.pod for more details.
use v6;
"a,b,c".split(',').perl.say; # ["a", "b", "c"]
Conclusion
That's it for today. There are more things to say about arrays but
as I also have two chapters in my regular Perl 5 training
for arrays I figure I can split that into two here as well.
Stay tuned for arrays, hashes, files, control structure
and the more advanced stuff.
This entry was first sent out as part of the
Perl 6 Tricks and Treats.
Visit here to subscribe.
see comments
Perl 5 to Perl 6 - Scalars
Tue Jun 16 18:04:56 2009
Tags: Perl, Perl 5, Perl 6, newsletter, scalars Abstract
The first entry of the Perl 6 Tricks and Treats
newsletter in our quest to switch from Perl 5 to Perl 6. Looking at the scalars.
This entry was first sent out as part of the
Perl 6 Tricks and Treats.
Visit here to subscribe.
Welcome back to the Perl 6 Tricks and Treats
I have to apologize as it has been more than 2 months since I
sent out the last newsletter.
After I came back from Oslo, where I gave a Perl 6
training class a client contacted with an urgent project
that had a tight schedule. That meant that up till yesterday I have
been working quite intensively at the client. That did not leave
time for writing either the Test Automation Tips or the
Perl 6 Tricks and Treats.
Anyway, the dead-line is now over and they are moving into a calmer
period in which I only visit them once or twice a week.
Perl 5 to Perl 6 : scalars
So today I wanted to start writing about Perl 6 again but I was
not really sure what to write about next. Hence I decided
to go over the Perl 6 training material and clean up parts that were
incorrect or that were lacking during the training in Oslo.
Actually there was a part that was quite neglected since I wrote the
first version a few years ago and it might be an easier approach for
me to clean that up first.
So I am looking at the Perl 5 to Perl 6 chapter and will go over its
sections and send them out. That might be less clear to people who don't
know Perl 5. I apologize in advance. It also means I am going to send
a number of short postings in the next few days.
I wonder if I should also write similar chapters for people coming
from other languages as well and which languages would be the most
important?
I start with scalars including the functions that seems to be the
most important to me. If you think differently, please tell me so
I can include those examples too.
print Hello World
In Perl 5 you normally use print() or starting from 5.10
you can use say() to print to the screen. The latter will
append a newline to whatever you had to say.
In Perl 6 they work the same.
use v6;
print "Hello World\n";
say "Hello World";
Variable declaration using "my"
In Perl 6 you (almost) always need to declare your variables "my".
Think as if you had always added "use strict".
There are exceptions such as one-liners and certain constructs
that auto-declare the scoped variables for you.
use v6;
my $name = "Moose";
Scalar variable interpolation
In Perl 6 scalar variables still interpolate in double quoted strings:
use v6;
my $name = "Foobar";
say "Hello $name"; # Hello Foobar
In order to make the Object Oriented people happy Perl 6 also allows this:
use v6;
my $name = "Foobar";
"Hello $name".say; # Hello Foobar
Reading from standard input
In Perl 5 you would use <STDIN>.
As reading a single line from standard input is usually preceded
by a print statement the keyword to do that in Perl 6 is called prompt().
use v6;
my $line = prompt('Please type in your name: ');
say "Hi $line, how are you?"; # Hi Foobar, how are you?
If you try this you will see that it chomps the newline off automatically.
Repeated reading without a prompt will be explained later.
Getting part of a string (substr)
The substr() function of Perl 5 is the same except that there is
no 4 parameter version. As all the other functions it can also
work in the object oriented way.
use v6;
my $line = "The brown cat climbed the green tree";
say substr $line, 4, 3; # bro
$line.substr(4, 3).say; # bro
Length of a string
In Perl 5 we used the length() function.
In order to avoid ambiguity in Perl 6 there is no length() function.
Instead there are several functions with more precise meaning of the type
of unit you are asking for.
So bytes() will return the number of bytes in the string while,
chars() will return the number of characters.
Of course in my example you won't see any difference as the string is
written in ascii where each character is one byte long.
use v6;
my $a = "This is a string";
say chars $a; # 16
say bytes $a; # 16
There are also the graphs() and codes() functions but they are not yet
implemented in Rakudo. See S32-setting-library/Str.pod for more details
( http://perlcabal.org/syn/S32/Str.html )
chomp
In Perl 5 there is a chomp() function to remove a trailing newline
after reading a line from the standard input or from a file.
In Perl 6 this function will be almost never used as reading from
the standard input or from files will automatically chomp
off the newlines. In any case, in Perl 6 chomp behaves differently
as it returns the chomped string and does NOT change the original
string.
use v6;
my $a = "abcd\n";
my $b = chomp $a; # $b is now "abcd"
defined
The defined() function is the same in Perl 6 as in Perl 5 checking if
a scalar value has any value different from undef.
String concatenation using dot (.)
In Perl 5 dot (.) is used for string concatenation. I think it is a lot
less used than one would think as in many cases we use string interpolation.
Maybe one of the most frequent use-cases is actually the short-cut string
concatenation.
In Perl 6 the string concatenation is done using the ~ operator.
I think it will be even less used than in Perl 5 as the variable
and code interpolation in Perl 6 is much stronger. Anyway here is
the example:
use v6;
my $str = "Foo" ~ "Bar";
$str.say; # FooBar
I think the only place where it will be used a lot is the short-cut
version that looks like this ~=
$str ~= " and Moo";
say $str; # FooBar and Moo
This can of course lead to some confusion but =~ is not the
regular expression operator in Perl 6 and the user will be
told so if he gets it wrong.
String repetition (x)
String repetition that was the x operator stayed the same:
use v6;
say "abc" x 3; # abcabcabc
index, rindex
The index() and rindex() functions of Perl 5 that
can locate a substring stayed the same in Perl 6 as well.
use v6;
my $s = "The brown cat climbed the green tree";
say index $s, "b"; # 4
lc, uc
lc() and uc() turning strings to all lower case and all upper case
are the same in Perl 6 as in Perl 5.
use v6;
say lc "Hello World"; # hello world
say uc "Hello World"; # HELLO WORLD
Conclusion
I know these are no big tricks here just plain code
but I hope this will help some people in learning Perl 6.
Stay tuned for arrays, hashes, files, control structure
and the more advanced stuff.
This entry was first sent out as part of the
Perl 6 Tricks and Treats.
Visit here to subscribe.
see comments
|