3-argument open on STDIN

3-argument open on STDIN

am 17.08.2011 23:37:52 von Bryan R Harris

How can I do a 3-argument open on STDIN? This doesn't work because the
3-argument open won't open STDIN when you tell it to open "-".

**************************************
@files = ("-");

for (@files) {
print reverse readfile($_);
}

sub readfile {

open(my $fh,"<",$_[0]) or die "$me: Couldn't open $_[0]: $!\n";
my(@fc) = <$fh>;
close($fh) or die "$me: Couldn't close $_[0]: $!\n";
if ($fc[0] =~ /\r/) { @fc = map { s/\r\n?/\n/g; split /(?<=\n)/ } @fc; }
if (wantarray()) { return @fc; }
else { return join('', @fc) }
}
**************************************

How can I use the "safe" 3-argument open and still be able to read off a
pipe?

- Bryan




--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: 3-argument open on STDIN

am 23.08.2011 01:42:14 von merlyn

>>>>> "Bryan" == Bryan R Harris writes:

Bryan> How can I use the "safe" 3-argument open and still be able to read off a
Bryan> pipe?

You don't. 2-arg open has to be good for something.

And 2-arg open is perfectly safe if the second arg is a literal:

open OTHER, "<-" or die;
open my $handle, "<-" or die;

Don't let anyone tell you "Always use 3-arg open" unless they also
footnote it with "unless you have no variables involved".

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: 3-argument open on STDIN

am 23.08.2011 05:17:11 von Bryan R Harris

>>>>>> "Bryan" == Bryan R Harris writes:
>
> Bryan> How can I use the "safe" 3-argument open and still be able to read off
> a
> Bryan> pipe?
>
> You don't. 2-arg open has to be good for something.
>
> And 2-arg open is perfectly safe if the second arg is a literal:
>
> open OTHER, "<-" or die;
> open my $handle, "<-" or die;
>
> Don't let anyone tell you "Always use 3-arg open" unless they also
> footnote it with "unless you have no variables involved".


Hmm. With this tool if there's a pipe and no user-supplied files, I just
put "-" onto the list of files to search -- using the 2-arg open. Someone
suggested that was a bad idea, so I switched to the 3-arg open but that
broke reading off the pipe.

So is it right that in order to read off the pipe *and* be safe, I have to
have both types of "open" statements in my code?

- Bryan



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: 3-argument open on STDIN

am 23.08.2011 09:16:59 von Rob Dixon

On 23/08/2011 04:17, Bryan R Harris wrote:
>
>
>>>>>>> "Bryan" == Bryan R Harris writes:
>>
>> Bryan> How can I use the "safe" 3-argument open and still be able to read off
>> a
>> Bryan> pipe?
>>
>> You don't. 2-arg open has to be good for something.
>>
>> And 2-arg open is perfectly safe if the second arg is a literal:
>>
>> open OTHER, "<-" or die;
>> open my $handle, "<-" or die;
>>
>> Don't let anyone tell you "Always use 3-arg open" unless they also
>> footnote it with "unless you have no variables involved".
>
>
> Hmm. With this tool if there's a pipe and no user-supplied files, I just
> put "-" onto the list of files to search -- using the 2-arg open. Someone
> suggested that was a bad idea, so I switched to the 3-arg open but that
> broke reading off the pipe.
>
> So is it right that in order to read off the pipe *and* be safe, I have to
> have both types of "open" statements in my code?

If your file names aren't hard-coded into the program then yes.

This same question received five replies on 18 August. Did you read
them?

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: 3-argument open on STDIN

am 24.08.2011 01:25:27 von Bryan R Harris

>>>>>>>> "Bryan" == Bryan R Harris writes:
>>>
>>> Bryan> How can I use the "safe" 3-argument open and still be able to read
>>> off
>>> a
>>> Bryan> pipe?
>>>
>>> You don't. 2-arg open has to be good for something.
>>>
>>> And 2-arg open is perfectly safe if the second arg is a literal:
>>>
>>> open OTHER, "<-" or die;
>>> open my $handle, "<-" or die;
>>>
>>> Don't let anyone tell you "Always use 3-arg open" unless they also
>>> footnote it with "unless you have no variables involved".
>>
>>
>> Hmm. With this tool if there's a pipe and no user-supplied files, I just
>> put "-" onto the list of files to search -- using the 2-arg open. Someone
>> suggested that was a bad idea, so I switched to the 3-arg open but that
>> broke reading off the pipe.
>>
>> So is it right that in order to read off the pipe *and* be safe, I have to
>> have both types of "open" statements in my code?
>
> If your file names aren't hard-coded into the program then yes.
>
> This same question received five replies on 18 August. Did you read
> them?

Absolutely, I did -- and I got the script working by having logic that
selects whether to use the 2-arg open or the 3-arg open. But since I had
Randall's attention I thought I'd make sure that was the better thing to do.

Thanks to all for the responses -- a great list!

- Bryan



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/