Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement a new var @*ARGFILES listing paths for @*ARGS that are existing files #2666

Open
tbrowder opened this issue Feb 1, 2019 · 7 comments

Comments

Projects
None yet
5 participants
@tbrowder
Copy link
Member

commented Feb 1, 2019

The Problem

Iterating over $*ARGFILES throws an error if the system tries to get a handle on a non-existent file.

Expected Behavior

Ability to iterate over only valid file handles as the name implies.

Actual Behavior

Throws on non-existent files.

Steps to Reproduce

$ rm t
$ cat t.p6
say($_.path) for $*ARGFILES;
$ perl6 t.p6
Failed to open file /path/to/t: No such file or directory
  in block <unit> at t.p6 line 1

Environment

Debian 9, 64-bit.
$ perl6 -v
This is Rakudo version 2018.06 built on MoarVM version 2018.06
implementing Perl 6.c.

Solution

Create the new dynamic variable simultaneously with the creation of @*ARGS. It should contain the paths of the elements of @*ARGS that are existing, readable files.

@ugexe

This comment has been minimized.

Copy link
Member

commented Feb 1, 2019

What does "if the file exist" mean to different users? Does it exist at the time the program starts? Does it exist at the moment it is iterated over by $*ARGFILES? I'm not sure a pragma can properly capture all the valid variations one might consider.

A naive solution:

BEGIN @*ARGS = @*ARGS.grep(!*.starts-with("-")).grep(*.IO.e);
say($_.path) for $*ARGFILES;
@tbrowder

This comment has been minimized.

Copy link
Member Author

commented Feb 1, 2019

In my naive view, I see $*ARGFILES reflecting the current file state of CLI @*ARGS when the program starts. That's the only way I've used Perl scripts (P5 or P6) in my years of use since 1993.

@tbrowder

This comment has been minimized.

Copy link
Member Author

commented Feb 2, 2019

I think a better solution is what you suggested, but make it a new dynamic variable: @*ARGFILES. It would be created along with @*ARGS but would just contain the paths of input args that are valid files. I’m renaming and rewording this issue.

@tbrowder tbrowder changed the title implement a pragma for $*ARGFILES to create handles ONLY for existing files implement a new var @*ARGFILES listing paths for @*ARGS that are existing files Feb 2, 2019

@JJ

This comment has been minimized.

Copy link
Contributor

commented Feb 3, 2019

@ugexe It simply means valid filehandles. Right now, as Tom has indicated, $*ARGFILES does create a filehandle for non-existing or invalid files, only nothing can be done with it other than capturing the exception when it's used.

JJ added a commit to perl6/doc that referenced this issue Feb 3, 2019

Adds an example which fails with invalid filehandles.
I couldn't find a way to deal with them, as indicated in #96 and
rakudo/rakudo#2666. I asked [this question in
SO](https://stackoverflow.com/questions/54501546/dealing-with-invalid-filehandles-and-maybe-other-invalid-objects-too),
will reopen if there's some answer that does not involve catching the
error. Meanwhile, closes #96
@lizmat

This comment has been minimized.

Copy link
Contributor

commented Feb 3, 2019

Feels to me $*ARGFILES should return a Failure rather than throw.

@tbrowder

This comment has been minimized.

Copy link
Member Author

commented Feb 3, 2019

Yes, then that would satisfy my use case and be a very useful tool, especially if I could iterate it like this:

for $*ARGFILES -> $handle {
    next if !$handle;
    say $handle.path
}
@ugexe

This comment has been minimized.

Copy link
Member

commented Feb 3, 2019

@JJ It was a rhetorical question. What “valid file handle” means isn’t so clear; backlog the chat channel the last few days to see others with a similar informed opinion and explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.