Re: Diamond <> or fileinput-like input handling (was Re: what type $in,$out and $err is)

2018-11-10 Thread yary
Can't answer most of your points.

How to replicate this behavior in Raku without handling all the
> args-handling and opening/closing logic yourself is now… unclear, at best,
> and may simply be missing.
>

https://marketing.perl6.org/id/1541379592/pdf_digital has a hint
"

   -

   see IO::CatHandle type if you need old behaviour

( Isn't "CatHandle" just a long way of writing "tail" ? :-o )

https://docs.perl6.org/type/IO::CatHandle - Its constructor accepts a list
of strings as filenames ... because IO::Handle is responsible for
converting strings (Cool) to handles, it still needs work to treat  '-' as
*IN but that's do-able

my $unixy-in = IO::CatHandle.new(@*ARGS.map: { $_ eq '-' ?? $*IN !! $_ } )

-y


On Mon, Nov 5, 2018 at 3:37 PM Trey Harris  wrote:

> On Mon, Nov 5, 2018 at 11:54 AM Ralph Mellor
> [ralphdjmel...@gmail.com](mailto:ralphdjmel...@gmail.com)
> 
> wrote:
>
> On Sun, Oct 28, 2018 at 7:26 PM Xiao Yafeng  wrote:
>>
>>> Besides, just curious, why choose '_' as default it looks strange
>>>
>>
>> Turns out it's deprecated in 6.d:
>>
>> https://marketing.perl6.org/id/1541379592/pdf_digital
>>
> Is it, for Proc objects, in addition to for  as mentioned in the
> ChangeLog ?
>
> They mean different things (that simply happen to semantically overlap
> frequently):
>
>- In  , use of '-' indicates
>$*IN or $*OUT for read-only or write-only uses respectively.
>- In Proc , the default of '-'
>indicates standard POSIX-like practice of inheriting stdin, stdout and
>stderr from the parent, no matter how the parent (or its parent(s)) have
>duped or redirected them.
>
> From the respective code, I think it is not; as the ChangeLog states, it’s
> deprecated for , but not for Proc.
>
> This passage
> 
> from the built-in variables doc is interesting:
>
> Argument related variables
>
>- $*ARGFILES An IO::ArgFiles 
>(an empty subclass of IO::CatHandle) that uses @*ARGS as source files,
>if it contains any files, or $*IN otherwise. When $*IN is used, its
>:nl-in, :chomp, :encoding, and :bin will be set on the IO::ArgFiles
> object.
>
> As of 6.d language, $*ARGFILES *inside* sub MAIN
>  is always set to $*IN,
> even when @*ARGS is not empty.
>
>- @*ARGS Arguments from the command line.
>
> This is getting at the issue with replicating the common “while diamond”
> operation (originally just while (<>)) in Perl 5, which allowed easy
> replication of Unix/POSIX default line-oriented file-handling behavior:
> namely, concatenating the lines of each filename given in the optional
> positionals, unless the filename was the single hyphen "-", in which case
> lines from standard input were read instead; when the positionals were
> empty, an implicit "-" was assumed.
>
> (If you haven’t played with this behavior directly, it’s very easy to just
> use cat or grep to observe this behavior; with no files, stdin is used;
> with files, stdin is usually not used, but - alone can be used to switch
> to stdin after reading named files. And not just “after”—it’s permissible,
> although a bit unusual, to put - between file names, in which case lines
> from the named files preceding the hyphen are processed, then lines from
> stdin are processed until stdin closes, then files named after the hyphen
> are processed. It’s even allowable to include the single hyphen more than
> once, which forces reopening of stdin for another round of input until it’s
> closed again. This behavior is ubiquitous in the Unix world; Perl 5 had it
> built-in, Python has its fileinput module
> , and so on.)
>
> How to replicate this behavior in Raku without handling all the
> args-handling and opening/closing logic yourself is now… unclear, at best,
> and may simply be missing.
>
> In this passage 
> from the Perl 5-to-6 guide (which, as an ancillary help document, does not
> have any authority for language specification), we read:
>
> Note that reading line-by-line from a filehandle has changed.
>
> In Perl 5, it was done in a while loop using the diamond operator. Using
> for instead of while was a common bug, because the for causes the whole
> file to be sucked in at once, swamping the program’s memory usage.
>
> In Perl 6, for statement is *lazy*, so we read line-by-line in a for loop
> using the .lines method.
>
> while ()  { } # Perl 5
>
> for $IN_FH.lines { } # Perl 6
>
> $IN_FH is, I think, given in the latter example in a hand-wavy way. The
> thing that comes *closest* to cat, grep, 

Diamond <> or fileinput-like input handling (was Re: what type $in,$out and $err is)

2018-11-05 Thread Trey Harris
On Mon, Nov 5, 2018 at 11:54 AM Ralph Mellor
[ralphdjmel...@gmail.com](mailto:ralphdjmel...@gmail.com)

wrote:

On Sun, Oct 28, 2018 at 7:26 PM Xiao Yafeng  wrote:
>
>> Besides, just curious, why choose '_' as default it looks strange
>>
>
> Turns out it's deprecated in 6.d:
>
> https://marketing.perl6.org/id/1541379592/pdf_digital
>
Is it, for Proc objects, in addition to for  as mentioned in the
ChangeLog ?

They mean different things (that simply happen to semantically overlap
frequently):

   - In  , use of '-' indicates $*IN
   or $*OUT for read-only or write-only uses respectively.
   - In Proc , the default of '-'
   indicates standard POSIX-like practice of inheriting stdin, stdout and
   stderr from the parent, no matter how the parent (or its parent(s)) have
   duped or redirected them.

>From the respective code, I think it is not; as the ChangeLog states, it’s
deprecated for , but not for Proc.

This passage
 from
the built-in variables doc is interesting:

Argument related variables

   - $*ARGFILES An IO::ArgFiles 
   (an empty subclass of IO::CatHandle) that uses @*ARGS as source files,
   if it contains any files, or $*IN otherwise. When $*IN is used, its
   :nl-in, :chomp, :encoding, and :bin will be set on the IO::ArgFiles
    object.

As of 6.d language, $*ARGFILES *inside* sub MAIN
 is always set to $*IN,
even when @*ARGS is not empty.

   - @*ARGS Arguments from the command line.

This is getting at the issue with replicating the common “while diamond”
operation (originally just while (<>)) in Perl 5, which allowed easy
replication of Unix/POSIX default line-oriented file-handling behavior:
namely, concatenating the lines of each filename given in the optional
positionals, unless the filename was the single hyphen "-", in which case
lines from standard input were read instead; when the positionals were
empty, an implicit "-" was assumed.

(If you haven’t played with this behavior directly, it’s very easy to just
use cat or grep to observe this behavior; with no files, stdin is used;
with files, stdin is usually not used, but - alone can be used to switch to
stdin after reading named files. And not just “after”—it’s permissible,
although a bit unusual, to put - between file names, in which case lines
from the named files preceding the hyphen are processed, then lines from
stdin are processed until stdin closes, then files named after the hyphen
are processed. It’s even allowable to include the single hyphen more than
once, which forces reopening of stdin for another round of input until it’s
closed again. This behavior is ubiquitous in the Unix world; Perl 5 had it
built-in, Python has its fileinput module
, and so on.)

How to replicate this behavior in Raku without handling all the
args-handling and opening/closing logic yourself is now… unclear, at best,
and may simply be missing.

In this passage  from
the Perl 5-to-6 guide (which, as an ancillary help document, does not have
any authority for language specification), we read:

Note that reading line-by-line from a filehandle has changed.

In Perl 5, it was done in a while loop using the diamond operator. Using for
instead of while was a common bug, because the for causes the whole file to
be sucked in at once, swamping the program’s memory usage.

In Perl 6, for statement is *lazy*, so we read line-by-line in a for loop
using the .lines method.

while ()  { } # Perl 5

for $IN_FH.lines { } # Perl 6

$IN_FH is, I think, given in the latter example in a hand-wavy way. The
thing that comes *closest* to cat, grep, Perl 5’s diamond, or Python’s
fileinput is—I believe—the unadorned routine lines()
:

for lines() -> $line { ... }

but it has the nonstandard behavior (which may be a bug) of only observing
the first hyphen found in the argument list and ignoring any successive
ones (at least in 6.c). Also, while I’m having trouble getting a 6.d to
work for me right now, I think Raku will give a deprecation notice when it
encounters that first hyphen—so to get the same behavior you’ll again need
to handle all hyphens on the command line yourself.

Just fyi — It’s entirely possible I’m wrong here, and just haven’t found
the right bit of the docs, or that once I get 6.d built I’ll find that
$*ARGFILES.lines — which would *seem* to me to be the right thing to use —
works for this purpose.

I’m not clear why its behavior is changed in sub MAIN—obviously, you can
handle the positionals yourself in the 

Re: what type $in,$out and $err is

2018-11-05 Thread Ralph Mellor
On Sun, Oct 28, 2018 at 7:26 PM Xiao Yafeng  wrote:

> Besides, just curious, why choose '_' as default it looks strange
>

Turns out it's deprecated in 6.d:

https://marketing.perl6.org/id/1541379592/pdf_digital


Re: what type $in,$out and $err is

2018-11-01 Thread Ralph Mellor
On Sun, Oct 28, 2018 at 7:26 PM Xiao Yafeng  wrote:

> I'm curious about what type of $in is on Proc class.


`$in` isn't "on `Proc` class".

`$in` is a *parameter* of the `.new` *method*.


> I mean, if $in is IO::Pipe object


As a *parameter* of the `.new` method *declaration* the type of `$in` is
not explicitly specified. When a parameter's type is not explicitly
specified
it defaults to the type `Any`.

The type of the corresponding `:in` *argument*  in a *call* of the `.new`
method is of whatever type you choose to pass. Any ordinary value is
a sub-type of `Any` so the method will accept it. For example, `True`
and `False` are ordinary values so will be accepted.

Besides, just curious, why choose '_' as default it looks strange
>

It's '-', not '_'.

'-' is a 50 year old or more unix convention that, when used for a filename
or pipe name means, depending on context, the standard input or output
handles of a process.

cf https://stackoverflow.com/a/8045639/1077672

hth

--
raiph


Re: what type $in,$out and $err is

2018-10-28 Thread Brad Gilbert
That is talking about the arguments for the method/subroutine call.

The way you pass in $in as True, is to add `:in`

run "cat", "-n", :in, :out;

The `:in` and `:out` are exactly the same as `:in(True)` `:out(True)`

run "cat", "-n", :in(True), :out(True);

Which is also the same as `in => True`

run "cat", "-n", in => True, out => True;

There is even a shortcut for `in => $in`

my $in = True;
my $out = True;

run "cat", "-n", :$in, :$out;
run "cat", "-n", in => $in, out => $out;
run "cat", "-n", :in($in), :out($out);

---

Basically that is a way to indicate that you are actually interested
in those filehandles,
and for the routine to set them up for you.
On Sun, Oct 28, 2018 at 2:26 PM Xiao Yafeng  wrote:
>
> I'm curious about what type of $in is on Proc class. As described in perl6doc:
> $in, $out and $err are the three standard streams of the
> to-be-launched program, and default to "-" meaning they inherit the
> stream from the parent process. Setting one (or more) of them to True
> makes the stream available as an IO::Pipe object of the same name,
> like for example $proc.out.
>
> I mean, if $in is IO::Pipe object, how can I pass it True?
>
> > my IO::Pipe $bb = True;
> Type check failed in assignment to $bb; expected IO::Pipe but got Bool
> (Bool::True)
>   in block  at  line 4
>
> I'm interested in the underlying mechanics of  it. Please enlighten me.
>
> Besides, just curious, why choose '_' as default it looks strange


Re: what type $in,$out and $err is

2018-10-28 Thread Brandon Allbery
It takes Any — and quite a few more things than are currently documented,
like IIRC filenames, and looks at the actual type passed to decide what to
do with it.

On Sun, Oct 28, 2018 at 3:31 PM Xiao Yafeng  wrote:

> I'm curious about what type of $in is on Proc class. As described in
> perl6doc:
> $in, $out and $err are the three standard streams of the
> to-be-launched program, and default to "-" meaning they inherit the
> stream from the parent process. Setting one (or more) of them to True
> makes the stream available as an IO::Pipe object of the same name,
> like for example $proc.out.
>
> I mean, if $in is IO::Pipe object, how can I pass it True?
>
> > my IO::Pipe $bb = True;
> Type check failed in assignment to $bb; expected IO::Pipe but got Bool
> (Bool::True)
>   in block  at  line 4
>
> I'm interested in the underlying mechanics of  it. Please enlighten me.
>
> Besides, just curious, why choose '_' as default it looks strange
>


-- 
brandon s allbery kf8nh
allber...@gmail.com


what type $in,$out and $err is

2018-10-28 Thread Xiao Yafeng
I'm curious about what type of $in is on Proc class. As described in perl6doc:
$in, $out and $err are the three standard streams of the
to-be-launched program, and default to "-" meaning they inherit the
stream from the parent process. Setting one (or more) of them to True
makes the stream available as an IO::Pipe object of the same name,
like for example $proc.out.

I mean, if $in is IO::Pipe object, how can I pass it True?

> my IO::Pipe $bb = True;
Type check failed in assignment to $bb; expected IO::Pipe but got Bool
(Bool::True)
  in block  at  line 4

I'm interested in the underlying mechanics of  it. Please enlighten me.

Besides, just curious, why choose '_' as default it looks strange