~ubuntu-branches/ubuntu/precise/padre/precise

« back to all changes in this revision

Viewing changes to share/examples/absolute_beginner/05_do_it_again.pl

  • Committer: Bazaar Package Importer
  • Author(s): Damyan Ivanov
  • Date: 2009-10-29 17:40:10 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20091029174010-yu27elyiv9izv0dv
Tags: 0.48.ds2-1
* New Upstream Version
  + new dependencies:
    - libdevel-refactor-perl 0.05
    - libfile-next-perl 1.04
    - libpod-perldoc-perl 3.15
    - libversion-perl (perl 5.10)
    - pip 0.13
  + new Recommends:
    - libformat-human-bytes-perl
  + dropped dependdencies
    - libcapture-tiny-perl
    - libfile-sharedir-par-perl
    - libpar-perl
  + bump libfile-which-perl, libppi-per, libppix-editortools-perl,
    libtest-script-perl and libwx-perl-processstream-perl dependencies
  + update translators list in d/copyright
  + add copyright holders for code borrowed from ExtUtils::MakeMaker
  + rules: drop empty Padre::Wx::Dialog::OpenResource::SearchTask manpage
  + refresh disable-tcp-server.patch
  + drop patches applied upstream: fix-perl-interpreter-path.patch,
    fix-pod-errors.patch and fix-whatis.patch
* Standards-Version: 3.8.3 (no changes)
* update debian/repack.sh to remove script/padre.exe,
  share/padre-splash-ccnc.bmp and  share/doc/perlopref.pod and plug it
  into debian/watch; describe repackaging in debian/copyright
* copyright: update
* update lintian override of template/example scripts not being
  executable
* add fix-helpprovider-with-no-perldoc.patch so that the Help browser does
  not hang because of the missing perlopref.pod
* update README.debian with regard to repackaging

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
# This file assumes that you already read (and understood) earlier sessions!
 
4
 
 
5
use strict;
 
6
use warnings;
 
7
 
 
8
# Let's say you want to count from 1 to 10. Pretty easy, just write:
 
9
my $counter = 0;
 
10
$counter++;
 
11
print "$counter\n";
 
12
$counter++;
 
13
print "$counter\n";
 
14
$counter++;
 
15
print "$counter\n";
 
16
$counter++;
 
17
print "$counter\n";
 
18
$counter++;
 
19
print "$counter\n";
 
20
$counter++;
 
21
print "$counter\n";
 
22
$counter++;
 
23
print "$counter\n";
 
24
$counter++;
 
25
print "$counter\n";
 
26
$counter++;
 
27
print "$counter\n";
 
28
$counter++;
 
29
print "$counter - done!\n";
 
30
 
 
31
# Works perfectly, but there are some drawbacks:
 
32
#  - It takes 21 lines (imagine you want to count until 100!)
 
33
#  - What happens if you need to change $counter to $counter2?
 
34
 
 
35
print "--- next sample ---\n";
 
36
 
 
37
# Every time you copy/paste a line of sourcecode, stop a moment and think
 
38
# about looping. This means, let Perl repeat some lines for you:
 
39
 
 
40
my $loop_counter = 0;
 
41
for ( 1 .. 10 ) {
 
42
 
 
43
        # "for" is a special keyword which says Perl: "I'll give you a list of items
 
44
        # and for each item, please execute the following sourcecode once."
 
45
        # Sounds much more complicated than it acutally is.
 
46
        # (1..10) is a range: It has a starting number (1) and a final value (10)
 
47
        # and Perl should give you all numbers from 1 to 10.
 
48
        # The lines which belong to this "for" - loop are between the same brackets { }
 
49
        # you already know from if. Actually every logical block of sourcecode is
 
50
        # surrounded by them.
 
51
 
 
52
        # If you write more than one line of sourcecode between { and }, write this
 
53
        # sourcecode as seperate lines. Perl dosn't care, but you and others need to
 
54
        # be able to read it without scrolling thousend columns to the right.
 
55
 
 
56
        # It might look like a waste of time, but if you put spaces or tabs before each
 
57
        # line within a if/loop/block, your source will be much more readable. Start
 
58
        # now and you're used to do it in a few days and it will save you very much
 
59
        # time hunting for lines in the wrong block lateron. Perl masters always do it.
 
60
 
 
61
        ++$loop_counter;
 
62
        print "$loop_counter\n";
 
63
 
 
64
}
 
65
 
 
66
# If you ignore my big boring comments above, this loop does the same in only 5
 
67
# lines what we did earlier in 21 lines and you got three places left where you
 
68
# need to change the variable name if you're forced to.
 
69
 
 
70
print "--- next sample ---\n";
 
71
 
 
72
# Perl could also be used for cooking:
 
73
 
 
74
for my $fruit ( "Orange", "Apple", "Strawberry", "Melon", "Lemon" ) {
 
75
        print "1 $fruit\n";
 
76
}
 
77
print "Cut the fruits in not-too-small pieces and your fruitsalat is done.\n";
 
78
 
 
79
# If you add a variable just behind "for", this variable will have the item for
 
80
# the current loop run. The salat example just prints the $Fruit, but you could
 
81
# do much more inside the loop.
 
82
 
 
83
print "--- next sample ---\n";
 
84
 
 
85
# "for" - loops got one drawback: You need to know how many times your loop
 
86
# should run before the loop starts and sometimes you don't know this.
 
87
 
 
88
# This sample tries to find out how often a given number (for example 123)
 
89
# could be divided by 2 before it gets lower than two.
 
90
 
 
91
my $number    = 123;
 
92
my $two_count = 0;
 
93
 
 
94
# Two variables, one holding the number and the second for the number of times
 
95
# we divided the number.
 
96
 
 
97
while ( $number > 2 ) {
 
98
 
 
99
        # while loops run until the condition (which is the same we used for the if's)
 
100
        # is no longer true. $number is 123 at the first run which is greater than 2
 
101
        # and the following lines are executed.
 
102
 
 
103
        print "$number\n";
 
104
        $number /= 2; # Look at the math session to understand this.
 
105
        ++$two_count;
 
106
}
 
107
 
 
108
print "Divided $two_count times\n";
 
109
 
 
110
# Always beware of endless loops where the condition always stays true! Your
 
111
# program would never leave the loop.
 
112
 
 
113
# Now press F5 and Padre will execute this script.
 
114
#
 
115
# You'll see a new window on the bottom of Padre which shows you the
 
116
# output of this script.
 
117
# We have a lot of samples with a lot of output in this file, so first try to
 
118
# match each block of output to the correct source code sample and try to
 
119
# understand what happens. Play around with the source if you want and change
 
120
# things to get other results.
 
121
 
 
122
# If you understood this lesson, this is easy for you:
 
123
# Try to write a loop which shows all even numbers from 20 to 30.
 
124
# Hint: Remember that 10 * 2 is 20 and 11 * 2 is 22.
 
125
 
 
126
# Got it working? Congratulations!
 
127
# Next, try to do the same using a while loop instead of for.