Don’t use “my” again and again to declare $rock[0], $rock[1] etc. Declare the array once (@rocks) once and use it.
you need to declare my @rocks and then not use my any more when referring to $rocks[xxx].
If you don’t know how many elements are going to be in there, you can use push to add new elements into the (initially 0-sized) array.
- from StackOverflow.com
#!C:\Perl\bin\perl.exe
# activePerl 5.8 based
use strict; # enabled
use warnings;
my @rocks;
($rocks[0], $rocks[1]) = qw/Hello World/;
$rocks[2] = 'Tom';
$rocks[3] = 'Cat';
print $rocks[0];
print $rocks[1];
print $rocks[2];
print $rocks[3];
#!C:\Perl\bin\perl.exe
use strict;
use warnings;
my $text = "a line of text\n\n"; # Or the same thing from <STDIN>
chomp($text); # Gets rid of the newline character, get rid of the newline \n
chomp $text; # chomp is acturally a function, get the value 1, get rid of another newline \n
print $text;
Output
D:\learning\perl>hello.pl
a line of text
D:\learning\perl>
More test, if we commented one chomp expression
#chomp $text; # chomp is acturally a function, get the value 1, get rid of another newline \n
Output(include one newline)
D:\learning\perl>hello.pl
a line of text
(# newline)
D:\learning\perl>
More test, and if we commented both of the two chomp expressions
#chomp($text); # Gets rid of the newline character, get rid of the newline \n
#chomp $text; # chomp is acturally a function, get the value 1, get rid of another newline \n
Output (include two newlines)
D:\learning\perl>hello.pl
a line of text
(#newline0)
(#newline1)
D:\learning\perl>
#!C:\Perl\bin\perl.exe -w
use strict;
use diagnostics;
print "The answer is ";
print 6 * 7;
print ".\n";
# Give print a series of values, separated by commas:
print "The answer is ", 6 * 7, ".\n";
Output
The answer is 42.
The answer is 42.
———-
#!C:\Perl\bin\perl.exe -w
use strict;
use warnings;
#use diagnostics;
my $fred = 'hello';
print "The name is $fred" . "\n"; # pring normally
print "The name is \$fred.\n"; # prints a dollar sign
print 'The name is $fred' . "\n"; # so does this
Output
The name is hello
The name is $fred.
The name is $fred
———-
#!C:\Perl\bin\perl.exe
use strict;
use diagnostics;
my $what = "brontosaurus steak";
my $n = 3;
print "fred ate $n $whats.\n"; # not the steaks, but the value of $whats
Output
Global symbol "$whats" requires explicit package name at D:\learning\perl\hello.
pl line 7.
Execution of D:\learning\perl\hello.pl aborted due to compilation errors (#1)
(F) You've said "use strict vars", which indicates that all variables
must either be lexically scoped (using "my"), declared beforehand using
"our", or explicitly qualified to say which package the global variable
is in (using "::").
Uncaught exception from user code:
Global symbol "$whats" requires explicit package name at D:\learning\per
l\hello.pl line 7.
Execution of D:\learning\perl\hello.pl aborted due to compilation errors.
———-
#!C:\Perl\bin\perl.exe
use strict;
use warnings;
my $what = "brontosaurus steak";
my $n = 3;
print "fred ate $n ${what}s.\n"; # now uses $what
print "fred ate $n $what" . "s.\n"; # another way to do it
print 'fred ate ' . $n . ' ' . $what . "s.\n"; # an especially difficult way
Output
fred ate 3 brontosaurus steaks.
fred ate 3 brontosaurus steaks.
fred ate 3 brontosaurus steaks.
- from Learning Perl 5E
Scalar variable names begin with a dollar sign followed by what we’ll call a Perl identifier: a letter or underscore, and then possible more letters, or digits, or underscores.
Upper and lowercase letters are distinct. Can’t start with a digit.
A scalar variable can hold only one value. But other types of variables, such as arrays and hashes, may hold may values.
- from Learning Perl 5E
$perl -w my_program
#!/usr/bin/perl -w
#!perl -w
#!C:\Perl\bin\perl.exe -w
With perl 5.6 and later, you can turn on warnings with a pragma.
#!C:\Perl\bin\perl.exe -w
use strict;
use warnings; # v5.6 and later
print "\n\n";
my $i = 4 * 'hello';
print $i;
Output
D:\learning\perl>hello.pl
Argument "hello" isn't numeric in multiplication (*) at D:\learning\perl\hello.p
l line 6.
0
if you get a warning message you don’t understand, you could get a longer description of the problem with the diagnostics pragma.
instead use warnings with use diagnostics
Out put:
D:\learning\perl>hello.pl
Argument "hello" isn't numeric in multiplication (*) at
D:\learning\perl\hello.pl line 7 (#1)
(W numeric) The indicated string was fed as an argument to an operator
that expected a numeric value instead. If you're fortunate the message
will identify which operator was so unfortunate.
0
. operator
a single period. String values can be concatenated with the operator.
x operator
a single lowercase letter x. A special repetition operator.
- from Learning Perl 5E
Sampe Code
#!C:\Perl\bin\perl.exe
use strict;
use warnings;
print "hello" . "world\n"; # same as "helloworld"
print "hello" . ' ' . "world\n"; # same as 'hello world'
print 'hello world' . "\n"; # same as "hello world\n"
print "\n";
print "4 x 5";
print "\n";
print '4 x 5';
print "\n";
print "4" x 5;
print "\n";
print 4 x 5;
print "\n\n";
my $i = 4 * 5;
print $i;
Output
helloworld
hello world
hello world
4 x 5
4 x 5
44444
44444
20
Single-Quoted String Literals.
A single-quoted string literal is a sequence of characters enclosed in single quotes. The single quotes are not part of the string iteself.
‘hello\n’ # hello followed by backslash followed by n
Double-Quoted String Literals
It’s a sequence of characters, although this time enclosed in double quotes. But now the backslash takes on its full power to specify certain control characters.
“hello\n” # hello, and a newline
If you wish to use backslash escape(like \n to mean a newline character), you will need to use the double quotes.
- from Leaning Perl 5E
Sample code
#!C:\Perl\bin\perl.exe
use strict;
use warnings;
print '\'\\';
print "\n";
print 'Hello World!\n';
print "\n";
print "Hello World!\n";
Output
'\
Hello World!\n
Hello World!
ActivePerl 5.8 Winxp based.
Accroding to the oldest rule in the book, any book about a computer language that has Unix-like roots has to start with showing the “Hello, world” program.
On Unix system, if the very first two characters on the first line of a text file are #!, what follows is the name of the program that acturally executes the rest of the file.
#!/usr/bin/perl
use strict;
use warnings;
print "Hello World!\n";
# This program only runs under perl 5.10
# Perl acturally thinks about the minor version as a three-digit number
# use 5.010;
# say "Hello World";
My development environment
#!C:\Perl\bin\perl.exe
# ActivePerl 5.8 installed
# OS: winxp
At the end of that message is the shortcut \n, which menas a new line character.
- from Learning Perl 5E
CPAN is the Comprehensive Perl Archive Network, your one-stop shop for Perl. It has the source code for Perl itself, ready-to-install prots of Perl to all sorts of non-Unix system, examples, documentation, extensions to Perl, and archives of messages about Perl. In short, CPAN is comprehensive.
- from Learning Perl 5E