Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-16 Thread Timo Paulssen
On 16/09/17 05:19, ToddAndMargo wrote:
> On 09/13/2017 01:36 PM, Trey Harris wrote:
>> L1 is an “ordinary method” (denoted, obviously enough, with the
>> declarator |method|), which as described in the Typesystem
>>  doc, “defines
>> objects of type Method  and binds
>> them to the provided name in the scope of a class.” An ordinary
>> method is usually called using dot syntax, i.e., |$object.getc(...)|.
>
> Okay.  Where did "$object" come from?
>
> It would help if it was written out in long hand so as to explain
> what the "." does.  Still have to have an example to figure out
> what is going on.

$object is just a stand-in for anything that would have the getc method
you're interested in. You can see from the declaration that it's defined
on IO::Handle. The . in $object.getc() means roughly this:

On the object in the variable $object find a method called "getc" and
call it, passing the $object itself as the "self" (also known as invocant).

>> Being a sub, it will be called most often using normal sub-calling
>> syntax, such as |getc($x)|.
>>
>
> Is $x called $object above?

Yes, that's right. Can be any variable that you put an IO::Handle into.

>
>> Now let’s turn to the /return values,/ which are what functional
>> programmers are usually looking at first. They’re marked by |-->|
>>
>
> --> means some kind of pointer to me.  "returns" would be much better
> wording for me, but at least
> I know what they are getting at.

Sadly, "returns" has limitations in certain situations, that's why the
--> syntax is generally preferred.

>> (though some old documentation may use |returns|). Conveniently, both
>> variants L1 and L2 return the same thing (not uncommon, for multi
>> routines): |Str:D|. The |Str| part is a reference to the string type
>> Str . The |:D| suffix says that the
>> value is /defined/; in other words, |getc| will never return the
>> undefined value. (A |:U| suffix would indicate that the return is
>> always /undefined/;
>>
>
> I would love a reference to all that Str:D stuff that defined that
> kind of stuff.
>
> Do you mean "can be undefined"?  Always "undefined" would be useless.

No, 'Always "undefined"' is a useful concept in Perl 6, because type
objects are undefined values, and passing around type objects can be
interesting.
>
>> the default, a lack of any suffix, can be made explicit with |:_|,
>> and means—as in Perl 5—that the return might or might not be defined.)
>>
>> That gets you to the /arguments/ of the two variants. Each are
>> /unary;/ they take just one argument.
>>
>
> Okay, where?

I'm not sure how you mean this. But if you mean "where do you see that
they only take one argument", you can see it from the lack of commas in
the signature. There's just the IO::Handle part. In the method form it
has a "must be defined" constraint, and is pointed out to be a
restriction on the invocant - the thing before the .getc() in the
earlier example). In the sub form it's got an explicit name, $fh, and a
default value, $*ARGFILES. After the arguments there's also the return
type which is introduced with the --> syntax.

>> Putting it all together, it tells you that these are valid examples:
>> |my Str $chr = getc($*IN); # insist on STDIN, even if file arguments
>> were given on the command line $chr = "/dev/tty".IO.open.getc; #
>> Insist not just on STDIN, but on the POSIX tty |
>>
>> Learning to read the Perl 6 doc  signatures
>> may be frustrating at first, but it’s well worth it, and pays dividends.
>>
>> ​
> What in the world is "my Str $chr"?
>
> If returning a type String, why are you using "chr".  I though Perl
> had no defined character type.

"chr" here is only the name of the variable, it has no extra meaning to
the language. The type it's declared as remains Str. Calling it $chr
makes sense, as getc is supposed to only return one character at a time.


> I can see that, I would still need examples.  I was very puzzled about
> what you meant
> until your last two line, which made everything make sense.
>
> How you got from 
>     multi sub getc (IO::Handle $fh = $*ARGFILES --> Str:D)
> to
>    chr = "/dev/tty".IO.open.getc;
>
> is a complete mystery.  How I am suppose to know
> "(IO::Handle $fh = $*ARGFILES" means it wants a file handle?

Here you've switched the method and sub forms around. from the ".getc"
in the example you can tell it's a method call, not a sub call, as those
would look like "$chr = getc()" for example. But I'll explain the sub
form anyway:

That it takes a file handle is in the very first part of the argument
declaration. "IO::Handle $fh = $*ARGFILES" can be taken apart into four
pieces:

1) Whatever is passed must type-check as an IO::Handle
2) The variable inside the sub will be called "$fh" (the name doesn't
make a difference to the caller, but it's good for documentation purposes)
3) The variable 

Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-15 Thread ToddAndMargo

  
  
On 09/13/2017 01:36 PM, Trey Harris
  wrote:


  

  On Sat, Sep 9, 2017
at 3:55 PM ToddAndMargo toddandma...@zoho.com wrote:
  
On
  09/09/2017 07:00 AM, Timo Paulssen wrote:
  > This should be enlightening: https://docs.perl6.org/routine/getc
  >
  
  Problem: also from the link:
  
       method getc(IO::Handle:D: --> Str:D)
       multi sub getc (IO::Handle $fh = $*ARGFILES -->
  Str:D)
  
  This is very frustrating to me as it mean ABSOLUTELY
  NOTHING
  to me.  Without an example, which the link does not
  provide,
  I have not the slightest idea what they are talking about.

  
  So excuse my going
off-topic here, but I hear your frustration and think you’ll
find it valuable to learn what you need to know to not be
frustrated rather than hoping the docs will over time get
more verbose with sundry examples for each possible use of
language features (it probably won’t, for the most part).
  If you’re coming
from Perl 5, which is mostly an explicit-type-less language,
or many other minimally or dynamically-typed languages,
these cryptic lines can be frustrating;
learning-by-example is usually the best way to “grok” a
feature intuitively.
  I think it’s
important to learn to read them, rather than simply ask for
examples (as drove most of the P5 perldoc for builtins). To
explain why: in Haskell, a language which greatly influenced
Perl 6, the type declarations alone are often all the
Haskell programmer needs to fully understand a function;
prose or examples are entirely superfluous. The power of
well- and expressively-typed routines for documentation is
inarguable in efficiency, precision, and concision. The
downside is the necessary investment in overcoming the
learning curve required to read them.
  To return to the
lines that frustrated you, let’s try to make them
understandable—or, at least, make them mean more to you than
“ABSOLUTELY NOTHING”. :-)
  Let’s start at a
high level:
  method getc(IO::Handle:D: --> Str:D)  # L1
multi sub getc (IO::Handle $fh = $*ARGFILES --> Str:D) # L2

  You see getc
defined twice here; they’re each distinct code (that are
documented once because they do much the same thing) due to
their declarators.
  L1 is an “ordinary
method” (denoted, obviously enough, with the declarator method),
which as described in the Typesystem doc, “defines
objects of type Method and binds them to the
provided name in the scope of a class.” An ordinary method
is usually called using dot syntax, i.e., $object.getc(...).

  


Okay.  Where did "$object" come from?

It would help if it was written out in long hand so as to explain
what the "." does.  Still have to have an example to figure out
what is going on.


  

  L2 is a “multisub”,
that is, a sub handled via “multi-dispatch” (in most
languages with the concept, called multimethod dispatch).
Multi-dispatch allows a single routine name to refer to
different code (“polymorphism”) on axes other than the
traditional object-oriented paradigm of “invocant” to
include number, type, definedness, and even value of
arguments at call time. (In this case, there being only one,
it may seem superfluous, but it allows for other modules to
introduce getc
variants of their own without having to invent new names or
muck about with namespaces.) Being a sub, it will be called
most often using normal sub-calling syntax, such as getc($x).

  


Is $x called $object above?


  

  Now let’s turn to
the return values, which are what functional
programmers are usually looking at first. They’re marked by
-->
  

  


--> means some kind of pointer to me.  "returns" would be much
better wording for me, but at least
I know what they are getting at.


  

  (though some old
documentation may use returns).
Conveniently, both variants L1 and L2 return the same thing
(not uncommon, for multi routines): Str:D.
The Str
part is a reference to t

Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-13 Thread Norman Gaywood
Thanks for that Trey!

Previously I would just glance at those definitions and move straight to
the examples. Now I can read them!

Something like this explanation should be on a docs.perl6.org page. Maybe
it is and I've just not found it.

On 14 September 2017 at 06:36, Trey Harris  wrote:

> On Sat, Sep 9, 2017 at 3:55 PM ToddAndMargo toddandma...@zoho.com
>  wrote:
>
> On 09/09/2017 07:00 AM, Timo Paulssen wrote:
>> > This should be enlightening: https://docs.perl6.org/routine/getc
>> >
>>
>> Problem: also from the link:
>>
>>  method getc(IO::Handle:D: --> Str:D)
>>  multi sub getc (IO::Handle $fh = $*ARGFILES --> Str:D)
>>
>> This is very frustrating to me as it mean ABSOLUTELY NOTHING
>> to me.  Without an example, which the link does not provide,
>> I have not the slightest idea what they are talking about.
>>
> So excuse my going off-topic here, but I hear your frustration and think
> you’ll find it valuable to learn what you need to know to not be frustrated
> rather than hoping the docs will over time get more verbose with sundry
> examples for each possible use of language features (it probably won’t, for
> the most part).
>
> If you’re coming from Perl 5, which is mostly an explicit-type-less
> language, or many other minimally or dynamically-typed languages, these
> cryptic lines *can* be frustrating; learning-by-example is usually the
> best way to “grok” a feature intuitively.
>
> I think it’s important to learn to read them, rather than simply ask for
> examples (as drove most of the P5 perldoc for builtins). To explain why: in
> Haskell, a language which greatly influenced Perl 6, the type declarations
> alone are often all the Haskell programmer needs to fully understand a
> function; prose or examples are entirely superfluous. The power of well-
> and expressively-typed routines for documentation is inarguable in
> efficiency, precision, and concision. The downside is the necessary
> investment in overcoming the learning curve required to read them.
>
> To return to the lines that frustrated you, let’s try to make them
> understandable—or, at least, make them mean more to you than “ABSOLUTELY
> NOTHING”. :-)
>
> Let’s start at a high level:
>
> method getc(IO::Handle:D: --> Str:D)  # L1
> multi sub getc (IO::Handle $fh = $*ARGFILES --> Str:D) # L2
>
> You see getc defined twice here; they’re each distinct code (that are
> documented once because they do much the same thing) due to their
> *declarators*.
>
> L1 is an “ordinary method” (denoted, obviously enough, with the declarator
> method), which as described in the Typesystem
>  doc, “defines
> objects of type Method  and binds
> them to the provided name in the scope of a class.” An ordinary method is
> usually called using dot syntax, i.e., $object.getc(...).
>
> L2 is a “multisub”, that is, a sub handled via “multi-dispatch” (in most
> languages with the concept, called multimethod dispatch). Multi-dispatch
> allows a single routine name to refer to different code (“polymorphism”) on
> axes other than the traditional object-oriented paradigm of “invocant” to
> include number, type, definedness, and even value of arguments at call
> time. (In this case, there being only one, it may seem superfluous, but it
> allows for other modules to introduce getc variants of their own without
> having to invent new names or muck about with namespaces.) Being a sub, it
> will be called most often using normal sub-calling syntax, such as
> getc($x).
>
> Now let’s turn to the *return values,* which are what functional
> programmers are usually looking at first. They’re marked by --> (though
> some old documentation may use returns). Conveniently, both variants L1
> and L2 return the same thing (not uncommon, for multi routines): Str:D.
> The Str part is a reference to the string type Str
> . The :D suffix says that the value is
> *defined*; in other words, getc will never return the undefined value. (A
> :U suffix would indicate that the return is always *undefined*; the
> default, a lack of any suffix, can be made explicit with :_, and means—as
> in Perl 5—that the return might or might not be defined.)
>
> That gets you to the *arguments* of the two variants. Each are *unary;*
> they take just one argument.
>
> (In the case of the method, its arity depends on your definition of the
> invocant being an argument; if you subscribe to the view that a method’s
> invocant doesn’t count, then method getc is a nullary, taking no
> argument. But the view that the invocant counts as an argument is useful
> here, so let’s use that definition, making the two both unary routines.)
>
> Both routines’ single argument is of type IO::Handle. They also both
> require definedness, but in different ways. The method uses the :D suffix
> we’ve already seen; in effect, that allows $my-handle.getc to work
> 

Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-13 Thread ToddAndMargo

On 09/13/2017 01:36 PM, Trey Harris wrote:
On Sat, Sep 9, 2017 at 3:55 PM ToddAndMargo toddandma...@zoho.com 
 wrote:


On 09/09/2017 07:00 AM, Timo Paulssen wrote:
 > This should be enlightening: https://docs.perl6.org/routine/getc
 >

Problem: also from the link:

  method getc(IO::Handle:D: --> Str:D)
  multi sub getc (IO::Handle $fh = $*ARGFILES --> Str:D)

This is very frustrating to me as it mean ABSOLUTELY NOTHING
to me.  Without an example, which the link does not provide,
I have not the slightest idea what they are talking about.

So excuse my going off-topic here, but I hear your frustration and think 
you’ll find it valuable to learn what you need to know to not be 
frustrated rather than hoping the docs will over time get more verbose 
with sundry examples for each possible use of language features (it 
probably won’t, for the most part).


If you’re coming from Perl 5, which is mostly an explicit-type-less 
language, or many other minimally or dynamically-typed languages, these 
cryptic lines /can/ be frustrating; learning-by-example is usually the 
best way to “grok” a feature intuitively.


I think it’s important to learn to read them, rather than simply ask for 
examples (as drove most of the P5 perldoc for builtins). To explain why: 
in Haskell, a language which greatly influenced Perl 6, the type 
declarations alone are often all the Haskell programmer needs to fully 
understand a function; prose or examples are entirely superfluous. The 
power of well- and expressively-typed routines for documentation is 
inarguable in efficiency, precision, and concision. The downside is the 
necessary investment in overcoming the learning curve required to read them.


To return to the lines that frustrated you, let’s try to make them 
understandable—or, at least, make them mean more to you than “ABSOLUTELY 
NOTHING”. :-)


Let’s start at a high level:

|method getc(IO::Handle:D: --> Str:D) # L1 multi sub getc (IO::Handle $fh 
= $*ARGFILES --> Str:D) # L2 |


You see |getc| defined twice here; they’re each distinct code (that are 
documented once because they do much the same thing) due to their 
/declarators/.


L1 is an “ordinary method” (denoted, obviously enough, with the 
declarator |method|), which as described in the Typesystem 
 doc, “defines 
objects of type Method  and binds 
them to the provided name in the scope of a class.” An ordinary method 
is usually called using dot syntax, i.e., |$object.getc(...)|.


L2 is a “multisub”, that is, a sub handled via “multi-dispatch” (in most 
languages with the concept, called multimethod dispatch). Multi-dispatch 
allows a single routine name to refer to different code (“polymorphism”) 
on axes other than the traditional object-oriented paradigm of 
“invocant” to include number, type, definedness, and even value of 
arguments at call time. (In this case, there being only one, it may seem 
superfluous, but it allows for other modules to introduce |getc| 
variants of their own without having to invent new names or muck about 
with namespaces.) Being a sub, it will be called most often using normal 
sub-calling syntax, such as |getc($x)|.


Now let’s turn to the /return values,/ which are what functional 
programmers are usually looking at first. They’re marked by |-->| 
(though some old documentation may use |returns|). Conveniently, both 
variants L1 and L2 return the same thing (not uncommon, for multi 
routines): |Str:D|. The |Str| part is a reference to the string type Str 
. The |:D| suffix says that the value 
is /defined/; in other words, |getc| will never return the undefined 
value. (A |:U| suffix would indicate that the return is always 
/undefined/; the default, a lack of any suffix, can be made explicit 
with |:_|, and means—as in Perl 5—that the return might or might not be 
defined.)


That gets you to the /arguments/ of the two variants. Each are /unary;/ 
they take just one argument.


(In the case of the method, its arity depends on your definition of the 
invocant being an argument; if you subscribe to the view that a method’s 
invocant doesn’t count, then |method getc| is a nullary, taking no 
argument. But the view that the invocant counts as an argument is useful 
here, so let’s use that definition, making the two both unary routines.)


Both routines’ single argument is of type |IO::Handle|. They also both 
require definedness, but in different ways. The method uses the |:D| 
suffix we’ve already seen; in effect, that allows |$my-handle.getc| to 
work provided |$my-handle| has been set to a defined IO::Handle object. 
But if the variable was declared with |my IO::Handle $my-handle;| but 
never assigned to, |$my-handle.getc| will not work; neither will 
|IO::Handle.getc| (which would work if |:D| weren’t included in the 
signature; o

Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-13 Thread Trey Harris
On Sat, Sep 9, 2017 at 3:55 PM ToddAndMargo toddandma...@zoho.com
 wrote:

On 09/09/2017 07:00 AM, Timo Paulssen wrote:
> > This should be enlightening: https://docs.perl6.org/routine/getc
> >
>
> Problem: also from the link:
>
>  method getc(IO::Handle:D: --> Str:D)
>  multi sub getc (IO::Handle $fh = $*ARGFILES --> Str:D)
>
> This is very frustrating to me as it mean ABSOLUTELY NOTHING
> to me.  Without an example, which the link does not provide,
> I have not the slightest idea what they are talking about.
>
So excuse my going off-topic here, but I hear your frustration and think
you’ll find it valuable to learn what you need to know to not be frustrated
rather than hoping the docs will over time get more verbose with sundry
examples for each possible use of language features (it probably won’t, for
the most part).

If you’re coming from Perl 5, which is mostly an explicit-type-less
language, or many other minimally or dynamically-typed languages, these
cryptic lines *can* be frustrating; learning-by-example is usually the best
way to “grok” a feature intuitively.

I think it’s important to learn to read them, rather than simply ask for
examples (as drove most of the P5 perldoc for builtins). To explain why: in
Haskell, a language which greatly influenced Perl 6, the type declarations
alone are often all the Haskell programmer needs to fully understand a
function; prose or examples are entirely superfluous. The power of well-
and expressively-typed routines for documentation is inarguable in
efficiency, precision, and concision. The downside is the necessary
investment in overcoming the learning curve required to read them.

To return to the lines that frustrated you, let’s try to make them
understandable—or, at least, make them mean more to you than “ABSOLUTELY
NOTHING”. :-)

Let’s start at a high level:

method getc(IO::Handle:D: --> Str:D)  # L1
multi sub getc (IO::Handle $fh = $*ARGFILES --> Str:D) # L2

You see getc defined twice here; they’re each distinct code (that are
documented once because they do much the same thing) due to their
*declarators*.

L1 is an “ordinary method” (denoted, obviously enough, with the declarator
method), which as described in the Typesystem
 doc, “defines objects
of type Method  and binds them to the
provided name in the scope of a class.” An ordinary method is usually
called using dot syntax, i.e., $object.getc(...).

L2 is a “multisub”, that is, a sub handled via “multi-dispatch” (in most
languages with the concept, called multimethod dispatch). Multi-dispatch
allows a single routine name to refer to different code (“polymorphism”) on
axes other than the traditional object-oriented paradigm of “invocant” to
include number, type, definedness, and even value of arguments at call
time. (In this case, there being only one, it may seem superfluous, but it
allows for other modules to introduce getc variants of their own without
having to invent new names or muck about with namespaces.) Being a sub, it
will be called most often using normal sub-calling syntax, such as getc($x).

Now let’s turn to the *return values,* which are what functional
programmers are usually looking at first. They’re marked by --> (though
some old documentation may use returns). Conveniently, both variants L1 and
L2 return the same thing (not uncommon, for multi routines): Str:D. The Str
part is a reference to the string type Str .
The :D suffix says that the value is *defined*; in other words, getc will
never return the undefined value. (A :U suffix would indicate that the
return is always *undefined*; the default, a lack of any suffix, can be
made explicit with :_, and means—as in Perl 5—that the return might or
might not be defined.)

That gets you to the *arguments* of the two variants. Each are *unary;*
they take just one argument.

(In the case of the method, its arity depends on your definition of the
invocant being an argument; if you subscribe to the view that a method’s
invocant doesn’t count, then method getc is a nullary, taking no argument.
But the view that the invocant counts as an argument is useful here, so
let’s use that definition, making the two both unary routines.)

Both routines’ single argument is of type IO::Handle. They also both
require definedness, but in different ways. The method uses the :D suffix
we’ve already seen; in effect, that allows $my-handle.getc to work provided
$my-handle has been set to a defined IO::Handle object. But if the variable
was declared with my IO::Handle $my-handle; but never assigned to,
$my-handle.getc will not work; neither will IO::Handle.getc (which would
work if :D weren’t included in the signature; one way of getting “class
methods” in Perl 6 is to create methods with :U invocants).

The multisub L2 also requires definedness, but does it in a different way.
The IO::Handle $f

Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-13 Thread ToddAndMargo

On 09/08/2017 11:41 PM, ToddAndMargo wrote:

Hi All!

I am trying to convert some Perl 5 code to Perl 6.

What do I use in place of
 use Term::ReadKey qw ( ReadKey ReadMode );

I am trying to convert the following:


use Term::ReadKey qw ( ReadKey ReadMode );

sub DumpKeyboard () {
# Dump the contents of the keyboard buffer, if any
my $Trash;

ReadMode 4; # Turn off controls keys
while  ( defined ( $Trash = ReadKey ( -1, \*STDIN ) ) ) {
   # print "\$Trash = $Trash\n";
}
ReadMode 1; # Reset tty mode before exiting
}






Follow up as promised.

P6 comes with a built in "prompt" sub.  It does it job.
But is not always appropriate as it

1) retains any trash deliberately or accidentally placed
   in the keyboard buffer
2) it requires that you press enter to terminate the command

So, I build my own "Pause" command to deal with the problems
of "prompt":

1) dump (flush) the keyboard buffer
2) print out a message of your choosing
3) wait for a new (fresh) keyboard stroke
4) return the ASCII equivalent key that was pressed

So with a ton of help from you guys and the chat line, this
is what I have come up with.

Thank you all for the help!
-T


Pause.pm6

#!/usr/bin/env perl6
# Pause.pm6

#`{
References:
https://github.com/krunen/term-termios

https://github.com/ab5tract/Terminal-Print/blob/master/lib/Terminal/Print/RawInput.pm6#L17
https://linux.die.net/man/3/termios

# zef install Term::termios   (as root)
# zef install Terminal::Print::RawInput   (as root)
}


use Term::termios;
use Terminal::Print::RawInput;

sub Pause ( $PauseMessage) is export {
   #`{
This sub will
   1) dump (flush) the keyboard buffer
   2) print out $PauseMessage
   3) wait for a new (fresh) keyboard stroke
   4) return the ASCII equivalent key that was pressed

Note: the GUI (Xfce, ec.) has precedence over reading
  unusual keys, such as the "F" keys, etc.
   }

   my $char;
   my $saved_termios;
   my $termios;

   # Save the previous attrs
   $saved_termios := Term::termios.new(fd => 1).getattr;

   # Get the existing attrs in order to modify them
   $termios := Term::termios.new(fd => 1).getattr;

   # Set the tty to raw mode
   $termios.makeraw;

   # int "\n";
   print "$PauseMessage";

   $saved_termios.setattr(:FLUSH);

   my $in-supply = raw-input-supply;
   react {
  whenever $in-supply -> $c {
 $char = $c.ord < 32 ?? '^' ~ ($c.ord + 64).chr !! $c;
 # printf "got: %3d  %2s  %2s\r\n", $c.ord, $c.ord.base(16), $char;

 # done if $c eq 'q';
 done;
  }
   }

   $saved_termios.setattr(:DRAIN) if $saved_termios;
   print "$char\n";
   return $char
}




And a tester for it:


Pause.pl6

#!/usr/bin/env perl6

use lib '/home/linuxutil';
use strict;
use Pause; # qw[ Pause ];

my @z = qw[z zz zzz  snore];
say "Sleeping for ", @z.elems, " seconds.  Press some keys.";
for @z { sleep 1; print "$_ " }; print "\n";

my $Key = Pause( 'Press any key to continue (including weird keys)... ' );
print "The key that was pressed was <$Key>\n";



Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-09 Thread ToddAndMargo

On 09/09/2017 02:02 PM, Timo Paulssen wrote:
The important part of the page is actually the one titled "Waiting for 
potential combiners". You'll want to either use binary encoding instead 
of utf8 (the default) or use an encoding that doesn't have combiners, 
like latin1 or ascii.




Problem: also from the link:

method getc(IO::Handle:D: --> Str:D)
multi sub getc (IO::Handle $fh = $*ARGFILES --> Str:D)


my $character = getc();

or

my $character = $*IN.getc();

or

my $character = $my-input-handle.getc();


Thank you!



Since you need to have an encoding that won't wait for combiners after a 
key was pressed you'll probably have an open call, for example 
"/dev/tty".IO.open(:r);


You lost me



You are already using termios to make the terminal "raw"; BTW, you only 
need either makeraw or the 4 lines of flag setting and unsetting.


Also, the flags you were looking for are in the termios(3) man page. If 
your system doesn't have it for some reason, have a look at 
https://linux.die.net/man/3/termios


Will look.  Thank you!


Hope that helps!
   - Timo



--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~


Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-09 Thread Timo Paulssen
The important part of the page is actually the one titled "Waiting for
potential combiners". You'll want to either use binary encoding instead
of utf8 (the default) or use an encoding that doesn't have combiners,
like latin1 or ascii.


> Problem: also from the link:
>
> method getc(IO::Handle:D: --> Str:D)
> multi sub getc (IO::Handle $fh = $*ARGFILES --> Str:D)

my $character = getc();

or

my $character = $*IN.getc();

or

my $character = $my-input-handle.getc();

Since you need to have an encoding that won't wait for combiners after a
key was pressed you'll probably have an open call, for example
"/dev/tty".IO.open(:r);

You are already using termios to make the terminal "raw"; BTW, you only
need either makeraw or the 4 lines of flag setting and unsetting.

Also, the flags you were looking for are in the termios(3) man page. If
your system doesn't have it for some reason, have a look at
https://linux.die.net/man/3/termios

Hope that helps!
  - Timo


Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-09 Thread ToddAndMargo

On 09/09/2017 07:00 AM, Timo Paulssen wrote:

This should be enlightening: https://docs.perl6.org/routine/getc



Hi Timo,

From the link:

Buffering terminals

Using getc to get a single keypress from a terminal will
only work properly if you've set the terminal to
"unbuffered". Otherwise the terminal will wait for the
return key to be struck or the buffer to be filled up
before perl6 gets even a single byte of data.

This is exactly what I am after.

Problem: also from the link:

method getc(IO::Handle:D: --> Str:D)
multi sub getc (IO::Handle $fh = $*ARGFILES --> Str:D)

This is very frustrating to me as it mean ABSOLUTELY NOTHING
to me.  Without an example, which the link does not provide,
I have not the slightest idea what they are talking about.

Would you mind jotting down an example of how to read
a key press?


Many thanks,
-T


Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-09 Thread ToddAndMargo

On 09/09/2017 11:28 AM, Brandon Allbery wrote:
On Sat, Sep 9, 2017 at 2:57 AM, ToddAndMargo > wrote:


This guy maybe?

https://github.com/krunen/term-termios/blob/master/README.md


I can't tell what he is saying.

And his example wont read a carriage return


That's very low level. And it's disabling icrnl, so a carriage return is 
exactly that. \r not \n. (Same for output, it disables onlcr so \n moves 
down a line without moving the cursor back to the left; you have to 
output the \r yourself if you want it.)


There is no exact high level replacement for that perl 5 module *yet*. 
There's work on a branch of https://github.com/ab5tract/terminal-print 
for this, that I think hasn't been merged yet, much less released.


There is also a higher level gotcha: IIRC the read routine in rakudo 
wants to read an extra character to make sure it's got a full Unicode 
character. (Although arguably it should not in this case because there's 
an easy way to know if you need another character or not.) But even 
then, probably for single character input you want to set binary mode on 
$*IN.


How do I set $*IN to binary mode?

What I am after is the dump anything in the keyboard buffer.
Then wait for a (any) response from the user.


Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-09 Thread Brandon Allbery
On Sat, Sep 9, 2017 at 2:57 AM, ToddAndMargo  wrote:

> This guy maybe?
>
> https://github.com/krunen/term-termios/blob/master/README.md
>
> I can't tell what he is saying.
>
> And his example wont read a carriage return


That's very low level. And it's disabling icrnl, so a carriage return is
exactly that. \r not \n. (Same for output, it disables onlcr so \n moves
down a line without moving the cursor back to the left; you have to output
the \r yourself if you want it.)

There is no exact high level replacement for that perl 5 module *yet*.
There's work on a branch of https://github.com/ab5tract/terminal-print for
this, that I think hasn't been merged yet, much less released.

There is also a higher level gotcha: IIRC the read routine in rakudo wants
to read an extra character to make sure it's got a full Unicode character.
(Although arguably it should not in this case because there's an easy way
to know if you need another character or not.) But even then, probably for
single character input you want to set binary mode on $*IN.

-- 
brandon s allbery kf8nh   sine nomine associates
allber...@gmail.com  ballb...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net


Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-09 Thread Timo Paulssen
This should be enlightening: https://docs.perl6.org/routine/getc


Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-09 Thread ToddAndMargo

On 09/09/2017 12:07 AM, ToddAndMargo wrote:

On 09/08/2017 11:41 PM, ToddAndMargo wrote:

Hi All!

I am trying to convert some Perl 5 code to Perl 6.

What do I use in place of
 use Term::ReadKey qw ( ReadKey ReadMode );

I am trying to convert the following:


use Term::ReadKey qw ( ReadKey ReadMode );

sub DumpKeyboard () {
# Dump the contents of the keyboard buffer, if any
my $Trash;

ReadMode 4; # Turn off controls keys
while  ( defined ( $Trash = ReadKey ( -1, \*STDIN ) ) ) {
   # print "\$Trash = $Trash\n";
}
ReadMode 1; # Reset tty mode before exiting
}






Fedora Linux 26 and Scientific Linux 7.3

So far, I have:


#!/usr/bin/env perl6
# Pause.pm6

use Term::termios;

# reference: https://github.com/krunen/term-termios
# # zef install Term::termios   (as root)

# `man termios` for flags

sub Pause () is export {

# Save the previous attrs
my $saved_termios := Term::termios.new(fd => 1).getattr;

# Get the existing attrs in order to modify them
my $termios := Term::termios.new(fd => 1).getattr;

# Set the tty to raw mode
$termios.makeraw;

# You could also do the same in the old-fashioned way
$termios.unset_iflags();
$termios.set_oflags();
$termios.set_cflags();
$termios.unset_lflags();

# Set the modified atributes, delayed until the buffer is emptied
$termios.setattr(:DRAIN);

print "\n";
print "Press any key to continue...";

# Loop on characters from STDIN
# loop {
#my $c = $*IN.getc;
#print "got: " ~ $c.ord ~ "\r\n";
#last if $c eq 'q';
# }

my $c = $*IN.getc;

# Restore the saved, previous attributes before exit
$saved_termios.setattr(:DRAIN);
print "\n";
}



But it stacks up Carriage Returns (enter key).  And it just
acts weird.

And "man termios" is not showing any of the flags he is using.

:'(

What am I doing wrong?

-T





And you have to press "any" key twice.

Here is a tester:

#!/usr/bin/env perl6

use lib '/home/linuxutil';
use strict;
use Pause; # qw[ Pause ];

Pause;


--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~


Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-09 Thread ToddAndMargo

On 09/08/2017 11:41 PM, ToddAndMargo wrote:

Hi All!

I am trying to convert some Perl 5 code to Perl 6.

What do I use in place of
 use Term::ReadKey qw ( ReadKey ReadMode );

I am trying to convert the following:


use Term::ReadKey qw ( ReadKey ReadMode );

sub DumpKeyboard () {
# Dump the contents of the keyboard buffer, if any
my $Trash;

ReadMode 4; # Turn off controls keys
while  ( defined ( $Trash = ReadKey ( -1, \*STDIN ) ) ) {
   # print "\$Trash = $Trash\n";
}
ReadMode 1; # Reset tty mode before exiting
}






Fedora Linux 26 and Scientific Linux 7.3

So far, I have:


#!/usr/bin/env perl6
# Pause.pm6

use Term::termios;

# reference: https://github.com/krunen/term-termios
# # zef install Term::termios   (as root)

# `man termios` for flags

sub Pause () is export {

   # Save the previous attrs
   my $saved_termios := Term::termios.new(fd => 1).getattr;

   # Get the existing attrs in order to modify them
   my $termios := Term::termios.new(fd => 1).getattr;

   # Set the tty to raw mode
   $termios.makeraw;

   # You could also do the same in the old-fashioned way
   $termios.unset_iflags();
   $termios.set_oflags();
   $termios.set_cflags();
   $termios.unset_lflags();

   # Set the modified atributes, delayed until the buffer is emptied
   $termios.setattr(:DRAIN);

   print "\n";
   print "Press any key to continue...";

   # Loop on characters from STDIN
   # loop {
   #my $c = $*IN.getc;
   #print "got: " ~ $c.ord ~ "\r\n";
   #last if $c eq 'q';
   # }

   my $c = $*IN.getc;

   # Restore the saved, previous attributes before exit
   $saved_termios.setattr(:DRAIN);
   print "\n";
}



But it stacks up Carriage Returns (enter key).  And it just
acts weird.

And "man termios" is not showing any of the flags he is using.

:'(

What am I doing wrong?

-T


--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~


Re: What is P6 for P5 `use Term::ReadKey`?

2017-09-08 Thread ToddAndMargo

On 09/08/2017 11:41 PM, ToddAndMargo wrote:

Hi All!

I am trying to convert some Perl 5 code to Perl 6.

What do I use in place of
 use Term::ReadKey qw ( ReadKey ReadMode );

I am trying to convert the following:


use Term::ReadKey qw ( ReadKey ReadMode );

sub DumpKeyboard () {
# Dump the contents of the keyboard buffer, if any
my $Trash;

ReadMode 4; # Turn off controls keys
while  ( defined ( $Trash = ReadKey ( -1, \*STDIN ) ) ) {
   # print "\$Trash = $Trash\n";
}
ReadMode 1; # Reset tty mode before exiting
}







This guy maybe?

https://github.com/krunen/term-termios/blob/master/README.md

I can't tell what he is saying.

And his example wont read a carriage return


--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~


What is P6 for P5 `use Term::ReadKey`?

2017-09-08 Thread ToddAndMargo

Hi All!

I am trying to convert some Perl 5 code to Perl 6.

What do I use in place of
use Term::ReadKey qw ( ReadKey ReadMode );

I am trying to convert the following:


use Term::ReadKey qw ( ReadKey ReadMode );

sub DumpKeyboard () {
   # Dump the contents of the keyboard buffer, if any
   my $Trash;

   ReadMode 4; # Turn off controls keys
   while  ( defined ( $Trash = ReadKey ( -1, \*STDIN ) ) ) {
  # print "\$Trash = $Trash\n";
   }
   ReadMode 1; # Reset tty mode before exiting
}




--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~