Create your blog and photo album with postbit
Create your blog and photo album

Create new post

Content:

Upload a picture:
Tags (keywords separated by comma)

Save Cancel
perl:   Followers: 2 ; Following: 2

Explore perl's photo albums:
Perl images (1)
Photos from posts (2)

How to do base conversion using Perl6


How to convert a number to Base 36:

say 12176.base(36); # It's 9E8

say :36("9E8"); # It's 12176

Post by perl (2016-01-13 10:55)

From: Mentifex
Hey, what a neat Perl website! I just found this page in the Perl6 IRC log.I've been coding Perl AI for weeks now at http://ai.neocities.org/PMPJ.html and to take a break I read the Perl6 IRC logs. Thanks for this site! 2016-02-03 20:40

Post your comment:

Name: Email: Site:

Unicode Characters in Perl6

 

Perl 6 uses UTF-8 by default. Here are some references and Perl6 examples using Unicode characters and valid Unicode operators.

Question: How to print the name of an Unicode character?
Anwer: Use the "uniname" operator
say uniname("π"); # GREEK SMALL LETTER PI
or
say "π".uniname;

Question: How can I print an Unicode character using it's numerical code ?
Answer:
960.base(16)

Prints Unicode representation of hexadecimal "263A":
say "\x263a"; # prints Smiling Face ☺
or the same using base 10 number:
say chr(9786); # prints Smiling Face ☺

that's because 263A in Hexa is 9786 in Decimal, see next question.

Question: How can I convert hexa to decimal and decimal to hexa ?
Answer:
say 9786.base(16); # 263A. It will convert the number to base 16
say :16("263A"); # 9786, it's the reverse operation, back to base 10 (from base 16).
 

Question: How can I print a list of all Unicode characters?
Answer: The following script can be used to print the list of all Unicode names and characters:

say "CHARACTER (CODE NUMBER) CODE NAME";

for (80..120) -> $code {
  if ($code < 55296 || $code > 57343) { # Invalid code numbers
    say $code.chr ~ " ($code) " ~ $code.uniname;
  }
}
 

 

 

Code examples:

say "⁂".uniname; # It will print "ASTERISM"

say "\c[SKULL]"; # It will print "💀"
say "π".ord; # 960. ord method returns the codepoint number of the letter (grapheme)

say chr(960); # Prints π

say ("α".."ω").list; # Prints Greek alphabet

say π³; # same as say pi**3

say (1,2,3,4) ∩ (2,4,6,8); # intersection is set(2,4)

 

See the Unicode Table with interesting unicode characters and symbols that can be copied and used in your Perl 6 programs.

 

List of useful Unicode characters in Perl 6:


Perl 6 Unicode Constants - Pi and Tau (π , τ) greek letters:

π = 3.14159265358979: say pi

τ = 6.28318530717959: same as 2*pi or tau

 


Math operators and Superscripts:

¹ = power of 1: **1
² = power of two (Raise to the second power): **2
³ = power of three (Raise to the third power): **3

⁻¹ :Raise to negative first power: **-1
⁻²: Raise to the negative second power: **-2
⁻³: Raise to the negative third power: **-3

 

Sets:

∈ = is an element of: (elem)
∉ = is not an element of: !(elem)
∪ = union: (|)
∩ = intersection: (&)
⊆ = subset: (<=)
⊈ = not a subset: !(<=)

 

More references:

 

Post by perl (2016-01-13 10:23)

Tags: perl6 unicode

Post your comment:

Name: Email: Site:

Perl6 performance

Perl 6 Benchmark:

To test Perl 6 speed, I run the mandelbrot.p6 in a few Perl 5 and Perl 6 implementations using my computer (Intel i7-2630QM CPU, 2GHz):

The Mandelbrot Set Algorithm:

Language / Version / OS / Built Run time
(size 200 x 200 px)
Run time
(size 2000 x 2000 px)
Perl 5 (Windows cygwin Perl 5.22 and Linux Perl 5.14) 0.4 seconds 43 seconds
Perl 6, Linux
(old version: 2012.10 built on parrot 4.6.0 revision 0)
70 seconds (too slow, not tested)
Perl 6 for Windows
(Rakudo Star, version 2015.09-x86_64 JIT)
17.5 seconds (too slow, not tested)
Perl 6, Linux
(Rakudo version 2015.12-79-g4bb47d5 built on MoarVM)
14.5 seconds (not tested)
Javascript (Mandelbrot JS) 0.06 seconds
(Chrome browser, PC)
  5.5 seconds (Chrome browser, PC)
17 seconds (Chrome Mobile, Galaxy S5)
9 seconds (Chrome Mobile, Galaxy S6)

 

Perl 6 Linux command line:
time perl6 mandelbrot.p6 200 > image.pbm

Perl 6 Windows command line:
time /c/rakudo/bin/perl6.bat mandelbrot.p6 200 > image.pbm

Perl 5 command line:
time perl mandelbrot.p5.pl 200 > image.pbm

You can see the performance (run time) comparison with other programming langues in the Mandelbrot Set Benchmark.

 

Post by perl (2016-01-03 21:58)

Post your comment:

Name: Email: Site:

Cool things with Perl 6

Euler's identity in Perl 6:

> say e**(pi*i)+1
or you can use Unicode Pi (π) character:
> say e**(π * i)+1;

It prints "0+1.22464679914735e-16i", that is almost "0": 0.000000000000000122464679914735i
It is not exact 0 because the PI number is an approximation.

 

Post by perl (2016-01-03 14:58)

Post your comment:

Name: Email: Site:

Perl 6 new logo suggestions?

The information about the original Camila Logo states that Larry Wall requested a Perl 6 logo that with the following features:

    Fun
    Cool
    Cute
    Named
    Lively
    Punable
    Personal
    Concrete
    Symmetric
    Asymmetric
    Attractive
    Relational
    Metamorphic
    Decolorizable
    Shrinkable to textual icon
    Shrinkable to graphical icon
And may have attached "P6" wings on it. Should discourage misogyny. Sterility is not on the list of requirements. "I want something with gut appeal on the order of Tux". "The new logo must make Larry at least as happy as Camelia does."


This is my suggestion for a Perl 6 logo. It's a "P-V-6" butterfly (Perl Version 6):

 

(Logo suggestion created by Rodrigo de Almeida Siqueira - @rsiqueira)

The "Perl 6" font is "Adobe Caslon Pro", it's not the same as "ITC Garamond" that was used to write "Perl" in the "Programming Perl" book cover.
The butterfly wings (letter "P" and number "6") were created with a rotated number "6" using font "Charlemagne Std".

The blue color inside the wings is rgb code: #6699cc

Like it or not?
Suggestions accepted!


Other images that I like that could be used as inspiration for a Perl6 Logo:

 

stylized butterfly used as cover of the book "Modern Perl - by Chromatic", by

Post by perl (2016-01-02 21:43)

Post your comment:

Name: Email: Site:

Creating the Mandelbrot Set with Perl6


Perl 6 script to create the Mandelbrot fractal.

See also the JavaScript version of this fractal generator: http://js.do/rsiqueira/mandelbrot

 

use v6;
 
# Description: Generates the Mandelbrot Set fractal using Perl 6
# For the Benchmarks to test Perl6 speed / performance in comparison with other programming languages.
#
# Created by Rodrigo Siqueira (rsiqueira (at) gmail)
# Creation date: 2015-12-27
 
# Usage (will output a PBM image file to stdout):
# /c/rakudo/bin/perl6.bat mandelbrot.p6 > /tmp/mandelbrot.pbm
 
 
my Int $MAXITER = 50;
 
my Rat $xmin = -1.5;
my Rat $ymin = -1.0;
 
my Int $w = 1000;
my Int $h = 1000;
 
my Rat $invN = 2/$w;
 
print "P4\n$w $h\n"; # PBM image header
 
 
for (0..$h-1) -> $y {
 
  my Rat $y_coord = $y * $invN + $ymin;
 
  my @v;
 
  x:
  for 0..$w-1 -> $x {
 
    my Rat $x_coord = $x * $invN + $xmin;
    my Complex $C = $x_coord + $y_coord\i;
 
    my Complex $z_prev = 0 + 0i;
 
    for (0..$MAXITER) { # Iterate
 
      my Complex $z = $z_prev * $z_prev + $C;
 
      if ($z.abs > 4) { # Outer part of the Mandelbrot Set
        print '.';
        push @v, 0;
        next x;
      }
 
      $z_prev = $z;
    }
 
    print 'o'; # Inner part of the Mandelbrot Set
    push @v, 1;
 
  }
 
  print "\n";
 
  # print pack 'B*', pack 'C*', @v;
 
if (0) {
  my $bits = '';
  for (1..@v) -> $n {
    $bits = $bits ~ @v[$n-1];
    if ($n && !($n % 8)) {
      my $buf = Buf.new( :2($bits) );
      $*OUT.write($buf);
      $bits = '';
    }
  }
  if ($bits.chars>0) { # Trailing last bytes with less than 8 bits.
    my $fill_tail = '0' x (8 - $bits.chars) ; # add trailing zeros
    my $bits_complete = $bits ~ $fill_tail;
    my $buf = Buf.new( :2($bits_complete) );
    $*OUT.write($buf);
  }
}
 
 
}
 
### END ###
 
This is the text file (70x70 pixels size) generated by the script above. It is the ASCII art version of the Mandelbrot set:
 
......................................................................
......................................................................
......................................................................
......................................................................
......................................................................
................................................oo....................
...............................................ooo....................
.............................................oooooo...................
.............................................ooooooo..................
.............................................ooooooo..................
.............................................oooooooo.................
.............................................oooooo...................
........................................o.o..o.ooo..oo................
..................................o.....ooooooooooooooo..o............
..................................oo..oooooooooooooooooooo............
..................................ooo.oooooooooooooooooooooo.oooo.....
..................................oooooooooooooooooooooooooo.ooo......
...................................oooooooooooooooooooooooooooooo.....
................................oooooooooooooooooooooooooooooooo......
.............................oo.oooooooooooooooooooooooooooooooo......
..............................oooooooooooooooooooooooooooooooooooo....
..............................oooooooooooooooooooooooooooooooooo......
..............................ooooooooooooooooooooooooooooooooooo...o.
.............................ooooooooooooooooooooooooooooooooooooooo..
.............................oooooooooooooooooooooooooooooooooooooo...
............oo...o..........oooooooooooooooooooooooooooooooooooooo....
............oo...oooo.......oooooooooooooooooooooooooooooooooooooo....
............oooooooooooo....ooooooooooooooooooooooooooooooooooooooo...
............oooooooooooo...ooooooooooooooooooooooooooooooooooooooo....
..........ooooooooooooooo..ooooooooooooooooooooooooooooooooooooooo....
..........ooooooooooooooo..oooooooooooooooooooooooooooooooooooooooo...
.........ooooooooooooooooo.ooooooooooooooooooooooooooooooooooooooo....
.........ooooooooooooooooo.oooooooooooooooooooooooooooooooooooooo.....
......ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.....
.....ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo......
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo........
.....ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo......
......ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.....
.........ooooooooooooooooo.oooooooooooooooooooooooooooooooooooooo.....
.........ooooooooooooooooo.ooooooooooooooooooooooooooooooooooooooo....
..........ooooooooooooooo..oooooooooooooooooooooooooooooooooooooooo...
..........ooooooooooooooo..ooooooooooooooooooooooooooooooooooooooo....
............oooooooooooo...ooooooooooooooooooooooooooooooooooooooo....
............oooooooooooo....ooooooooooooooooooooooooooooooooooooooo...
............oo...oooo.......oooooooooooooooooooooooooooooooooooooo....
............oo...o..........oooooooooooooooooooooooooooooooooooooo....
.............................oooooooooooooooooooooooooooooooooooooo...
.............................ooooooooooooooooooooooooooooooooooooooo..
..............................ooooooooooooooooooooooooooooooooooo...o.
..............................oooooooooooooooooooooooooooooooooo......
..............................oooooooooooooooooooooooooooooooooooo....
.............................oo.oooooooooooooooooooooooooooooooo......
................................oooooooooooooooooooooooooooooooo......
...................................oooooooooooooooooooooooooooooo.....
..................................oooooooooooooooooooooooooo.ooo......
..................................ooo.oooooooooooooooooooooo.oooo.....
..................................oo..oooooooooooooooooooo............
..................................o.....ooooooooooooooo..o............
........................................o.o..o.ooo..oo................
.............................................oooooo...................
.............................................oooooooo.................
.............................................ooooooo..................
.............................................ooooooo..................
.............................................oooooo...................
...............................................ooo....................
................................................oo....................
......................................................................
......................................................................
......................................................................
......................................................................

 

Post by perl (2015-12-30 17:56)

Post your comment:

Name: Email: Site:

Perl6 is working! Great news!!!

Finally Perl6 is working.

It's so good and with so many "atractions" that it seems a playground!

I can't stop playing with it.

My first Perl 6 program:


sub Σ(@array_to_sum) { return [+] @array_to_sum; }

say Σ (1,2,3,4); # It will print 10, that is 1+2+3+4
 
 
It uses the sigma greek symbol to do a summation in Perl6.
The summation function is defined in the first line, using unicode sigma character, and it sums all array elements.
 

 

Post by perl (2015-12-27 23:42)

Post your comment:

Name: Email: Site:

How to read Excel files using Perl

There are two CPAN modules that you should install so you can use Perl to read Excel files for both formats ".xls extension" and ".xlsx extension.".
 
This is a sample script of how to parse xls and xlsx files using Perl:
 
#!/usr/bin/perl
 
# Script to test Spreadsheet::ParseXLSX and Spreadsheet::ParseExcel
# by Rodrigo Siqueira
# Created: 20/nov/2015
 
use strict;
 
$|=1;
 
use Data::Dumper;
use feature 'say';
 
use Spreadsheet::ParseXLSX;  # Only .xlsx files
use Spreadsheet::ParseExcel; # Only .xls  files
 
use Insite::String;
 
my $string = new String();
 
 
my $file = 'test.xlsx';
$file = 'test.xls';
 
if (!-e $file) {
  die("ERROR: Can not read file ($file): $!\n");
} else {
  say "Reading file: $file ...\n";
}
 
 
my $workbook;
 
if ($file =~ /\.xlsx/i) {
  my $parser = Spreadsheet::ParseXLSX->new;
  $workbook = $parser->parse($file);
} elsif ($file =~ /\.xls$/i) {
  $workbook = Spreadsheet::ParseExcel::Workbook->Parse($file);
}
 
say "Number of tabs: ".scalar( @{$workbook->{Worksheet}} );
 
#print Dumper($workbook);
 
foreach my $tab (@{$workbook->{Worksheet}}) {
 
  print "SHEET NAME:", $tab->{Name}, "\n";
 
  say "Number of rows: $tab->{MaxRow}";
  say "Number of columns: $tab->{MaxCol}";
  say "";
 
  for (my $iR = $tab->{MinRow} ; defined $tab->{MaxRow} && $iR <= $tab->{MaxRow} ; $iR++) { # Rows
    my $line_number = $iR;
    print "Line $line_number: ";
    for (my $iC = $tab->{MinCol} ; defined $tab->{MaxCol} && $iC <= $tab->{MaxCol} ; $iC++) { # Columns
      my $oWkC = $tab->{Cells}[$iR][$iC];
 
      if ($oWkC) {
        my $value = $oWkC->Value;
        $value = $string->iso8859_utf8($value);
 
        $value =~ s/;/\\;/gs; # Escape ';' as '\;' ??
        print "$value;"; # Column value
      }
 
      # print "($iR, $iC) = $value\n" if($oWkC);
 
    } # /Columns
 
    print "\n";
 
    # Only find 10 lines in this test, will exit
    last if $line_number==10;
 
  } # /Rows
 
}
 
 
### END ###
 

Post by perl (2015-11-20 17:02)

Post your comment:

Name: Email: Site:

Perl script to calculate PI number using Bellard's Algorithm

This is a fast way to calculate PI with Perl. It uses Bellard's PI calculation algorithm. It's a simple PERL implementation of Bellard's method.

#!/usr/bin/perl

# Fabrice Bellard's Algorithm to calculate number PI using PERL.
# Perl Script by Rodrigo Siqueira
 
# Run this to calculate PI with 1000 decimal places:
# perl -Mbignum=p,-1000 pi-bellard.pl
 
use bignum;
 
my $pi;
 
for my $k (0..340) {
  $pi += (1/2**6) * ((-1)**$k/(1024**$k)) * ( - (32)/(4*$k+1) - (1)/(4*$k+3) + (256)/(10*$k+1) - (64)/(10*$k+3) - (4)/(10*$k+5) - (4)/(10*$k+7) + 1/(10*$k+9) );
}
 
print "PI=".$pi;
 
### END ###
 
Result:

PI=3.
1415926535 8979323846 2643383279 5028841971 6939937510 5820974944 5923078164 0628620899 8628034825 3421170679
8214808651 3282306647 0938446095 5058223172 5359408128 4811174502 8410270193 8521105559 6446229489 5493038196
4428810975 6659334461 2847564823 3786783165 2712019091 4564856692 3460348610 4543266482 1339360726 0249141273
7245870066 0631558817 4881520920 9628292540 9171536436 7892590360 0113305305 4882046652 1384146951 9415116094
3305727036 5759591953 0921861173 8193261179 3105118548 0744623799 6274956735 1885752724 8912279381 8301194912
9833673362 4406566430 8602139494 6395224737 1907021798 6094370277 0539217176 2931767523 8467481846 7669405132
0005681271 4526356082 7785771342 7577896091 7363717872 1468440901 2249534301 4654958537 1050792279 6892589235
4201995611 2129021960 8640344181 5981362977 4771309960 5187072113 4999999837 2978049951 0597317328 1609631859
5024459455 3469083026 4252230825 3344685035 2619311881 7101000313 7838752886 5875332083 8142061717 7669147303
5982534904 2875546873 1159562863 8823537875 9375195778 1857780532 1712268066 1300192787 6611195909 2164201981
 
Warning: The last digit should be "9", not "1", it happens because of a round error in the last digit. So the last 10 digits (from digit place 990 to 1000) should be 2164201989.
 
The format 10 in 10 digital places can be obtained by using this format:
$pi =~ s/^3.//; # Remove "3." from the beginning.
$pi =~ s/(\d{100})/$1\n/g; # Add enter each 100 digital places of PI
$pi =~ s/(\d{10})/$1 /g; # Add space to each 10 digital places of PI
print "PI=3.$pi";
 
 
If you want to calculate PI with more than 1000 places using this script, you need also change max $k from 340 to a larger number.

Post by perl (2014-12-17 17:36)

Post your comment:

Name: Email: Site:

How to install DBI using cygwin

I had to install DBI to use DBD::SQLite. To install DBI using cygwin type:

cpan
install DBI

I got an error of gcc4 and g++4 when trying to install DBI because there are no gcc-4 and g++4 anymore. They are replaced by gcc.exe and g++.exe. So you need to make a sym link to the old filenames if you get the error:
make: gcc-4: Command not found
make: g++-4: Command not found

Type this to correct the gcc-4 and g++-4 errors:

ln -s /usr/bin/gcc.exe /usr/bin/gcc-4.exe
ln -s /usr/bin/g++.exe /usr/bin/g++-4.exe

After this, the DBI module could be installed and DBD::SQLite was also installed using CPAN.

---

The full error that I got because of missing gcc-4:

make: gcc-4: Command not found
make: g++-4: Command not found

Makefile:766: recipe for target 'blib/arch/auto/DBI/DBI.dll' failed
make: *** [blib/arch/auto/DBI/DBI.dll] Error 127
  TIMB/DBI-1.631.tar.gz
  /usr/bin/make -- NOT OK
Running make test
  Can't test without successful make
Running make install
  Make had returned bad status, install seems impossible
Failed during this command:
 TIMB/DBI-1.631.tar.gz                        : make NO
 

Post by perl (2014-10-12 23:43)

Post your comment:

Name: Email: Site:



  Page 1 of 3 NEXT PAGE >>

| Explore users | New posts | Create your blog | Create your photo album |
| About Postbit | Our blog | Terms of use | Contact Postbit |


Copyright © 2017 - postbit.com