From ff362fc01fa798727c8423d20f093e25702af52b Mon Sep 17 00:00:00 2001 From: Dan Book Date: Sat, 21 Jan 2017 06:06:52 -0500 Subject: [perl/en] how to write to files (#2633) --- perl.html.markdown | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index 3cbd2801..908f300b 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -170,8 +170,11 @@ $x =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $x # You can open a file for input or output using the "open()" function. +# For reading: open(my $in, "<", "input.txt") or die "Can't open input.txt: $!"; +# For writing (clears file if it exists): open(my $out, ">", "output.txt") or die "Can't open output.txt: $!"; +# For writing (appends to end of file): open(my $log, ">>", "my.log") or die "Can't open my.log: $!"; # You can read from an open filehandle using the "<>" operator. In @@ -182,6 +185,12 @@ open(my $log, ">>", "my.log") or die "Can't open my.log: $!"; my $line = <$in>; my @lines = <$in>; +# You can write to an open filehandle using the standard "print" +# function. + +print $out @lines; +print $log $msg, "\n"; + #### Writing subroutines # Writing subroutines is easy: -- cgit v1.2.3 From 05614d0920804799aec69fddadac4356c91020a2 Mon Sep 17 00:00:00 2001 From: Dan Book Date: Sat, 21 Jan 2017 07:20:42 -0500 Subject: [perl/en] some more examples of interacting with arrays and hashes (#2632) --- perl.html.markdown | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index 908f300b..a29fdf1f 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -51,6 +51,13 @@ my @mixed = ("camel", 42, 1.23); # indicate one value will be returned. my $second = $animals[1]; +# The size of an array is retrieved by accessing the array in a scalar +# context, such as assigning it to a scalar variable or using the +# "scalar" operator. + +my $num_animals = @animals; +print "Number of numbers: ", scalar(@numbers), "\n"; + ## Hashes # A hash represents a set of key/value pairs: @@ -67,6 +74,11 @@ my %fruit_color = ( # Hash elements are accessed using curly braces, again with the $ sigil. my $color = $fruit_color{apple}; +# All of the keys or values that exist in a hash can be accessed using +# the "keys" and "values" functions. +my @fruits = keys %fruit_color; +my @colors = values %fruit_color; + # Scalars, arrays and hashes are documented more fully in perldata. # (perldoc perldata). @@ -144,6 +156,12 @@ for (@elements) { print; } +# iterating through a hash (for and foreach are equivalent) + +foreach my $key (keys %hash) { + print $key, ': ', $hash{$key}, "\n"; +} + # the Perlish post-condition way again print for @elements; -- cgit v1.2.3 From 447986c19d2e40536872c4c97c1e5f7a82c29d0d Mon Sep 17 00:00:00 2001 From: Chris C Date: Thu, 9 Feb 2017 09:31:22 -0600 Subject: [perl/en] more perlish iterations on perl5 (#2489) * more perlish iteration c-style 'for' isn't perlish * Update perl.html.markdown removed one of the array iterator loops --- perl.html.markdown | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index a29fdf1f..93eabea9 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -138,18 +138,16 @@ while (condition) { # for loops and iteration -for (my $i = 0; $i < $max; $i++) { +for my $i (0 .. $max) { print "index is $i"; } -for (my $i = 0; $i < @elements; $i++) { - print "Current element is " . $elements[$i]; -} - for my $element (@elements) { print $element; } +map {print} @elements; + # implicitly for (@elements) { -- cgit v1.2.3 From 6e3d29f036f9ede844f17e36e764b685d046adf9 Mon Sep 17 00:00:00 2001 From: Dan Book Date: Thu, 18 May 2017 06:40:15 -0400 Subject: [perl/en] Use more single quotes and explain single vs double quotes (#2710) * [perl/en] Explain single vs double quotes Explains an example of variable interpolation and escape codes in a double quoted string. * add section about interpolating arrays and email in double quotes trap --- perl.html.markdown | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index 93eabea9..17a538e3 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -37,10 +37,14 @@ use warnings; # A scalar represents a single value: my $animal = "camel"; my $answer = 42; +my $display = "You have $answer ${animal}s.\n"; # Scalar values can be strings, integers or floating point numbers, and # Perl will automatically convert between them as required. +# Strings in single quotes are literal strings. Strings in double quotes +# will interpolate variables and escape codes like "\n" for newline. + ## Arrays # An array represents a list of values: my @animals = ("camel", "llama", "owl"); @@ -58,6 +62,18 @@ my $second = $animals[1]; my $num_animals = @animals; print "Number of numbers: ", scalar(@numbers), "\n"; +# Arrays can also be interpolated into double-quoted strings, and the +# elements are separated by a space character by default. + +print "We have these numbers: @numbers\n"; + +# Be careful when using double quotes for strings containing symbols +# such as email addresses, as it will be interpreted as a variable. + +my @example = ('secret', 'array'); +my $oops_email = "foo@example.com"; # 'foosecret array.com' +my $ok_email = 'foo@example.com'; + ## Hashes # A hash represents a set of key/value pairs: -- cgit v1.2.3 From 3bc441d4666b058f116949a60977adb79224dc62 Mon Sep 17 00:00:00 2001 From: Mike Shlanta Date: Wed, 1 May 2019 12:39:29 -0500 Subject: Fixed variable not declared before use. The variable `max` was not declared before use in demonstrating how for loops work over ranges. It has been added on line 155 and set to a value of 5 --- perl.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index 17a538e3..08001ab0 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -152,7 +152,7 @@ while (condition) { ... } - +my $max = 5; # for loops and iteration for my $i (0 .. $max) { print "index is $i"; -- cgit v1.2.3 From 0ecb8264293d852f2ef586279885848b621668d1 Mon Sep 17 00:00:00 2001 From: sumanstats Date: Wed, 10 Jun 2020 16:33:01 +0545 Subject: Perl6 to Raku and many more + As the Perl 6 is renamed to raku, it is good to reflect that https://github.com/Raku/problem-solving/blob/master/solutions/language/Path-to-Raku.md + perl6.org is now raku.org + change references of perl 6 to raku + rename file perl6-pod.html.markdown to raku-pod.html.markdown + Perl refers to Perl 5, there is no ambiguity after rename of Perl6 to Raku, use Perl only to refer to Perl 5 + fix links inside raku.html.markdown --- perl.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index 08001ab0..8811dd08 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -8,9 +8,9 @@ contributors: - ["Dan Book", "http://github.com/Grinnz"] --- -Perl 5 is a highly capable, feature-rich programming language with over 25 years of development. +Perl is a highly capable, feature-rich programming language with over 25 years of development. -Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects. +Perl runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects. ```perl # Single line comments start with a number sign. -- cgit v1.2.3