Page 1 of 1

Perl on Lucid Puppy

Posted: Mon 13 Dec 2010, 17:48
by dave45
I'd like to teach myself perl and have Lucid Puppy running nicely. I've managed to load a version of perl OK but trying to access CPAN modules blows out with "stuff not loaded" errors.

I guess I'm missing something in the configuration or have loaded the wrong version.

Is there an idiot's guide to loading perl on Lucid Puppy some kind person can point me to?

tvm

this is not programming

Posted: Mon 13 Dec 2010, 21:45
by vovchik
Please leave this part of the forum for programmers....perl is part of every puppy that I know of, starting from 1.07 in my experience.

Re: Perl on Lucid Puppy

Posted: Tue 14 Dec 2010, 00:27
by jpeps
dave45 wrote:I'd like to teach myself perl and have Lucid Puppy running nicely. I've managed to load a version of perl OK but trying to access CPAN modules blows out with "stuff not loaded" errors.

I guess I'm missing something in the configuration or have loaded the wrong version.

Is there an idiot's guide to loading perl on Lucid Puppy some kind person can point me to?

tvm
Perl is already installed in Lucid.

Code: Select all

#!/usr/bin/perl

print "hello world\n";
edit: here's one:
http://www.tizag.com/perlT/perlfirstscript.php

Posted: Wed 15 Dec 2010, 00:39
by jpeps
Here's a little cli calculator; does cube roots, etc to specified digits.


USEAGE: [num] [sign] [num] [-s decimals ]
operators: +, -, /, %, \* or m, ^

Code: Select all

#!/usr/bin/perl

### mc calculator: jpeps  12/10


$one="$ARGV[0]";
$two="$ARGV[2]";
$sign="$ARGV[1]";
$switch="$ARGV[3]";

if ( $switch  eq "-s"){
 $scale="$ARGV[4]";
}
else {
 $scale="2";
}
## Scale factor 
$x = join "", "%.", $scale, "f"; 


sub help {

print "USAGE: [num] [sign] [num] [-s decimals ]\n";
print "operators: +, -, /, %, \\* or m, ^\n";  
}

## Math routines

sub add {
print ($one + $two);
print "\n";

}
sub subtract {
print ($one - $two);
print "\n";
}
sub multiply {
print ($one * $two);
print "\n";
}

sub divide {
$number = $one / $two; 
printf $x, $number;
print "\n";
}

sub modulus {
print ($one % $two); 
print "\n";
}

sub power {
$number = $one ** $two;
printf $x, $number;
print "\n";
}

### Begin Program

if (($one eq "") || ($one eq "-h")){
help;
exit;
} 

if ($sign eq "+"){
add; 
}
elsif ($sign eq "-"){

subtract;
}
elsif (($sign eq "*") || ($sign eq "m")){ 

multiply;
}
elsif ($sign eq '/'){
divide;
}
elsif ($sign eq '%'){
modulus;
}
elsif ($sign eq '^'){
power;
}

Posted: Fri 17 Dec 2010, 09:55
by jpeps
More developed version of MC calculator. This one accepts a chain of numbers to specified digits.

Code: Select all

#!/usr/bin/perl

## CLI calculator 12/10 jpeps

$one="$ARGV[0]";
$two="$ARGV[2]";

## check for -s switch
@switch=grep(/\-s/, "@ARGV");
if ("@switch"){
$var="@switch";
@s = split('\-s', $var);
$scale="$s[1]";

## remove leading whitespace
$scale =~ s/^\s+//;
}
else {
 $scale="2";
}

## Scale factor for number of digits 
$x = join "", "%.", $scale, "f"; 


sub help {

print "USAGE: [num] [sign] [num].. [-s decimals ]\n";
print "operators: +, -, /, %, \\* or m, ^\n";  

}

## Math routines
sub add {

 $result = $one + $two; 
if (( ! "$ARGV[$n+2]" ) && ( ! "$switch")) { 
printf $x, $result;
print "\n";
}
 elsif ( "$ARGV[$n+1]" eq "\-s") {
printf $x, $result;
print "\n";
}
}

sub subtract {
 $result = $one - $two; 
if (( ! "$ARGV[$n+2]" ) && ( ! "$switch")) { 
printf $x, $result;
print "\n";
}
 elsif ( "$ARGV[$n+1]" eq "\-s") {
}
}

sub multiply {
$result =  $one * $two;
if (( ! "$ARGV[$n+2]" ) && ( ! "$switch")) { 
printf $x, $result;
print "\n";
}
 elsif ( "$ARGV[$n+1]" eq "\-s") {
}
}

sub divide {
$result = $one / $two; 
if (( ! "$ARGV[$n+2]" ) && ( ! "$switch")) { 
printf $x, $result;
print "\n";
}
 elsif ( "$ARGV[$n+1]" eq "\-s") {
printf $x, $result;
print "\n";
}
}

sub modulus {
$result =  $one % $two;
if (( ! "$ARGV[$n+2]" ) && ( ! "$switch")) { 
printf $x, $result;
print "\n";
}
 elsif ( "$ARGV[$n+1]" eq "\-s") {
printf $x, $result;
print "\n";
}
}

sub power {
$result = $one ** $two;
if (( ! "$ARGV[$n+2]" ) && ( ! "$switch")) { 
printf $x, $result;
print "\n";
}
 elsif ( "$ARGV[$n+1]" eq "\-s") {
printf $x, $result;
print "\n";
}
}
## End of Subs

### Begin Program

if (($one eq "") || ($one eq "-h")){
help;
exit;
} 

## Set args  for $two and $sign
$n = "0"; 
$i = "0";

## Begin while loop: loops through arguments
while ( "$two" ) {
$i++;
$n = $n + 2;
$two = "$ARGV[$n]"; 

$sign[$i] = "$ARGV[$n - 1]";


## Begin calculations

if ($sign[$i] eq "+") {
add;
$one = "$result"; 
} 

elsif ($sign[$i] eq "-"){
subtract;
$one = "$result";
}

elsif (($sign[$i] eq "*") || ($sign[$i] eq "m")){ 
multiply;
$one = "$result";
}

elsif ($sign[$i] eq '/'){
divide;
$one = "$result";
}

elsif ($sign[$i] eq '%'){
modulus;
$one = "$result";
}

elsif ($sign[$i] eq '^'){
power;
$one = "$result";
}
}

Posted: Fri 17 Dec 2010, 19:59
by jpeps
Should be about done. This version eliminates need for whitespaces to make entry easier and more readable. Also, substituted "x" for "m" for multiplication.
Both "x" and "*" required special handling, since they're special characters.

Updated 7/16/11: add negative integers with 'm': 3*m2

Code: Select all

#!/usr/bin/perl

## CLI calculator 12/10 jpeps
## 7/16/11  Add negative integers (m); example: 5*m4

## Add spaces around args
$args="@ARGV";
$args =~ s/\+/ \+ /g;
$args =~ s/\-/ \- /g;
$args =~ s/\*/ x /g;
$args =~ s/\^/ \^ /g;
$args =~ s/x/ x /g;
$args =~ s/\// \/ /g;
$args =~ s/\%/ \% /g;
$args =~ s/\- s/ \-s /;
$args =~ s/m/\-/g;

@args = split(" ",$args);

$one="$args[0]";
$two="$args[2]";
## check for -s switch
@switch=grep(/\-s/, "@args");
if ("@switch"){
$var="@switch";
@s = split('\-s', $var);
$scale="$s[1]";

## remove leading whitespace
$scale =~ s/^\s+//;
}
else {
 $scale="2";
}

## Scale factor for number of digits 
$x = join "", "%.", $scale, "f"; 


sub help {

print "USAGE: [num][sign][num]... [-s decimals ]\n";
print "operators: +, -, /, %, \\* or x, ^\n";  
print "negative numbers: use m: ex: 5*m4\n"; 

}

## Math routines
sub add {

 $result = $one + $two; 
if (( ! "$args[$n+2]" ) && ( ! "$switch")) { 
printf $x, $result;
print "\n";
}
 elsif ( "$args[$n+1]" eq "\-s") {
printf $x, $result;

print "\n";
}
}

sub subtract {
 $result = $one - $two; 
if (( ! "$args[$n+2]" ) && ( ! "$switch")) { 
printf $x, $result;
print "\n";
}
 elsif ( "$args[$n+1]" eq "\-s") {
printf $x, $result;
print "\n";

}
}

sub multiply {
$result =  $one * $two;

if (( ! "$args[$n+2]" ) && ( ! "$switch")) { 
printf $x, $result;
print "\n";
}
 elsif ( "$args[$n+1]" eq "\-s") {
printf $x, $result;
print "\n";
}
}

sub divide {
$result = $one / $two; 
if (( ! "$args[$n+2]" ) && ( ! "$switch")) { 
printf $x, $result;
print "\n";
}
 elsif ( "$args[$n+1]" eq "\-s") {
printf $x, $result;
print "\n";
}
}

sub modulus {
$result =  $one % $two;
if (( ! "$args[$n+2]" ) && ( ! "$switch")) { 
printf $x, $result;
print "\n";
}
 elsif ( "$args[$n+1]" eq "\-s") {
printf $x, $result;
print "\n";
}
}

sub power {
$result = $one ** $two;
if (( ! "$args[$n+2]" ) && ( ! "$switch")) { 
printf $x, $result;
print "\n";
}
 elsif ( "$args[$n+1]" eq "\-s") {
printf $x, $result;
print "\n";
}
}
## End of Subs

### Begin Program

if (($one eq "") || ($one eq "-h")){
help;
exit;
} 

## Set args  for $two and $sign
$n = "0"; 
$i = "0";

## Begin while loop: loops through arguments
while ( "$two" ) {
$i++;
$n = $n + 2;
$two = "$args[$n]"; 

$sign[$i] = "$args[$n - 1]";


## Begin calculations

if ($sign[$i] eq "+") {
add;
$one = "$result"; 
} 

elsif ($sign[$i] eq "-"){
subtract;
$one = "$result";
}

elsif (($sign[$i] eq "*") || ($sign[$i] eq "x")){ 
multiply;
$one = "$result";
}

elsif ($sign[$i] eq '/'){
divide;
$one = "$result";
}

elsif ($sign[$i] eq '%'){
modulus;
$one = "$result";
}

elsif ($sign[$i] eq '^'){
power;
$one = "$result";
}
}

Posted: Sun 17 Jul 2011, 06:24
by jpeps
I'm using this calculator quite a bit in scripts, and updated it to include capacity for negative integers using "m"

Code: Select all

 
/mnt/sda2/Desktop $ mc 6*m2
-12.00
/mnt/sda2/Desktop $

Posted: Tue 19 Jul 2011, 17:02
by jpeps
This bash frontend adds a memory function to the mc calculator, so that typing "M" substitutes in the last result:



change log:
ver. 3.0 7/18/11 added frontend
ver. 3.2 7/19/11 improved memory function; prints with "mc M"
Clear memory with "mc c"

example:

Code: Select all

/mnt/sda2/Desktop $ mc 23.23*2^2 -s3
2158.532
/mnt/sda2/Desktop $ mc M/12
179.88
/mnt/sda2/Desktop $ mc M   
179.88
/mnt/sda2/Desktop $ mc c
/mnt/sda2/Desktop $

script:

Code: Select all

#!/bin/sh
if [ "$1" == "" -o "$1" == "-h" ]; then  
  echo "Substitute "M" for memory; clear with 'mc c'"
   mc 
   exit 
fi  

VAR=`echo "$@" | grep "M"`

 ## Print memory with "M" option. 
 if [ "$VAR" ]; then  
   if [ "$VAR" == "M" ]; then 
          cat /tmp/.mc
          exit
   fi  
   if [ ! -f /tmp/.mc -o "$(cat /tmp/.mc)" == "" ]; then 
          echo "nothing in memory" 
          exit
     fi
    MEM="$(cat /tmp/.mc | sed 's/\-/m/')"
    ARG=`echo "$@" | sed "s/M/$MEM/"`
    mc "$ARG" >/tmp/.mc
 else
   mc "$@" >/tmp/.mc 
  
 fi

cat /tmp/.mc