Re: Raku IO IceDrive bug

2023-11-05 Thread ToddAndMargo via perl6-users



On Oct 22, 2023, at 06:02, ToddAndMargo via perl6-users 
 wrote:


spurt "$TestFile", "Old File Found\n";
spurt "$TestFile", "New File Opened\n";


On 11/5/23 12:08, William Michels via perl6-users wrote:
> Won't adding a `\n` newline to the `spurt()` call eventually cause
> problems?
>
> What happens if you omit the `\n` below?
>
> Best, Bill.
>
>

Hi Bill,

\n just adds a line feed to the file.  Without
it, when I cat the file in my shell, the prompt
for the next command comes up at the end of the
file, rather than on the next line.

-T



Re: Raku IO IceDrive bug

2023-11-05 Thread William Michels via perl6-users
Won't adding a `\n` newline to the `spurt()` call eventually cause problems? 

What happens if you omit the `\n` below?

Best, Bill.


> On Oct 22, 2023, at 06:02, ToddAndMargo via perl6-users 
>  wrote:
> 
> spurt "$TestFile", "Old File Found\n";
> spurt "$TestFile", "New File Opened\n";





Re: Raku IO IceDrive bug

2023-10-21 Thread Ralph Mellor
Hi again. :)

Let me try again. I'll try cover things more clearly.



#1 There was/is a bug in your code, or at least code that you
say isn't working on your system. (It works on glot.io. See #3)

```
$WanIpFileName.IO.open(:w);
spurt "$WanIpFileName", "New File Opened";
```

These two lines clash badly. They are a bug waiting to
happen. The glot.io system resolves the clash in your
favor, so the clash isn't apparent. You are saying that
on your Windows system, the code fails.

The simplest thing to do is just fix the bug, rather than
worry about understanding it. The simplest way to fix
the bug is to simply drop the first line. It's redundant at
best, and causes the bug at worst, so just get rid of it.

If you don't want to understand the bug, but do want to
know about the other issues, skip ahead to #2 or #3.



In case you *do* want to understand the bug, let's
start with another way to fix the bug. You could write:
```
my $handle = $WanIpFileName.IO.open(:w);
$handle.spurt: "$WanIpFileName", "New File Opened";
```
That works, and should work on Windows.

This time the code *saves the file handle* corresponding
to the first line, and then uses that handle as the invocant
of a `.spurt` *method*, thus connecting the handle opened
in the first line to the `spurt` done in the second line.

Maybe *that* has explained all you need to know. If so,
feel free to skip ahead to #2 or #3.

Repeating the two lines that clashed:
```
$WanIpFileName.IO.open(:w);
spurt "$WanIpFileName", "New File Opened";
```

The first line opens the file in write mode -- and then
leaves it like that and throws away the file handle.

That's an issue. If you understand why it's an issue,
then great. If you don't, then that's OK too, and I'll
move on, because it isn't too important.

The second line has no idea the first line opened the
very same file it's just about to open, so it tries to open
it *again*, write to the file handle, and then close it.

Finally, at program exit, the file system tries to clean
up an exiting process, eg closing any file handles left
open and resolving clashes like your code generates.

On some systems the code will succeed. On your system
you say it doesn't. See #3 for demo and further discussion.



#2 There was/is a doc failure. I now see another one or two.

The doc failed to say that `spurt` opens the file. It should.

(Then you would presumably have understood you did not
need the `open` line.)

The doc failed to say `open` returns a file handle. It should.

(Then you would presumably have understood that if you
used an `open` line, you needed to keep the file handle
and use a `.spurt` *method* on that file handle.)

The example doc you looked at includes a line that does
the equivalent of `touch`. You thought you could use it
but chop off the `.close`.

(But you didn't realize you needed to keep the file handle,
as just explained.)



#3 There is a Windows difference. Your code runs fine on glot.io.

Please click this link, and then the Run button:

https://glot.io/snippets/gpv7tdaca0

That contains your code. And it's working. The difference is it's not Windows.

NB. Each time you click Run, glot.io automatically cleans the
file system. Every time. That's why it always says 'file does
not exist' at the start. Every time.

NB. I added the lines declaring the missing variable and sub,
and checking the file does not exist at the start, and some
commented out stuff and so on. Feel free to edit the code to
whatever you want if you want to. If you want to start over,
click refresh on your browser and click 'Reload'.


love, raiph


Re: Raku IO IceDrive bug

2023-10-16 Thread ToddAndMargo via perl6-users




On 10/16/23 06:54, Ralph Mellor wrote:

if not FileExists( $WanIpFileName )  {
 $WanIpFileName.IO.open(:w);
 spurt "$WanIpFileName", "New File Opened";
}


Afaik, depending on how you choose to look at things,
there's a doc error (doc for `spurt` doesn't mention that
it opens the file it's spurting to), and/or behavior due to
your Windows file system / account settings that leads
to the issue you see, and/or a bug in your code.

Let me explain. Here's what the code does:

1. Opens the file with `$WanIpFileName.IO.open(:w);`
if it doesn't already exist.

2. Opens the file with `spurt` and writes to it.

And that causes the behavior you see.

Presumably, on your system/setup, doing those two things
fails because the second open fails due to the first one.

So it succeeds the second time around because the file
already exists (though blank) and the `spurt` goes ahead.

I'd argue that the bug is ultimately in your code, perhaps
due to you not realizing that `spurt` opens the file it spurts
to, because the doc doesn't say so.

Whichever way one looks at it, there's no bug in Raku(do).

--
love, raiph



Hi Raiph,

Interesting.  Be nice if it was me.  I can fix me.
I can't fix bugs in languages.

Looking at
https://docs.raku.org/type/IO::Path

I only find one instance of IO.open and it is an
example for "unlink"

'foo.txt'.IO.open(:w).close;

I was using it to create a file.  I can not find
any discussion of that.

And I could find no discussion of file locks either.

spurt "$WanIpFileName", "New File Opened";

was only added to see it I could write to the newly
created file.  Apparently, not.  And no error message
complaining about being denied access (file lock?).

Maybe I am opening a handle that is blocking
spurt from writing to the file.  It should not
matter, but apparently is does.

And if the above is the case, it is a bug in Raku.

Maybe I should have done
$WanIpFileName.IO.open(:w).close;


What I need is what function Raku is using to support fileapi.h:
CreateFileA function (fileapi.h)

https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea

And I see that fileapi.h is also opening a handle
which probably should also be closed.

I really do not feel like writing the api call myself.

Thank you for the help,
-T

p.s.  I adore Linux's "touch" command.





Re: Raku IO IceDrive bug

2023-10-16 Thread Ralph Mellor
> if not FileExists( $WanIpFileName )  {
> $WanIpFileName.IO.open(:w);
> spurt "$WanIpFileName", "New File Opened";
> }

Afaik, depending on how you choose to look at things,
there's a doc error (doc for `spurt` doesn't mention that
it opens the file it's spurting to), and/or behavior due to
your Windows file system / account settings that leads
to the issue you see, and/or a bug in your code.

Let me explain. Here's what the code does:

1. Opens the file with `$WanIpFileName.IO.open(:w);`
if it doesn't already exist.

2. Opens the file with `spurt` and writes to it.

And that causes the behavior you see.

Presumably, on your system/setup, doing those two things
fails because the second open fails due to the first one.

So it succeeds the second time around because the file
already exists (though blank) and the `spurt` goes ahead.

I'd argue that the bug is ultimately in your code, perhaps
due to you not realizing that `spurt` opens the file it spurts
to, because the doc doesn't say so.

Whichever way one looks at it, there's no bug in Raku(do).

--
love, raiph