Re: checking for no input

2003-09-09 Thread Jeff 'japhy' Pinyan
On Sep 9, david said:

>Jeff 'Japhy' Pinyan wrote:
>
>> I really that's too much work.  The -t file test should be sufficient:
>
>your version only checks to see is STDIN is attached to a tty. for example,
>run your script from a crontab and you see will it never prints the usage.
>(ie, it always thinks that there is something to work with even there isn't
>any).

Damn.

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
 what does y/// stand for?   why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: checking for no input

2003-09-09 Thread david
Jeff 'Japhy' Pinyan wrote:

> On Sep 9, Kevin Pfeiffer said:
> 
>>In article <[EMAIL PROTECTED]>, David wrote:
>>
>>[...]
>>> [panda]$ html.pl
>>> no input
>>> [panda]$ html.pl file.html
>>> get file file.html
>>> [panda]$ echo "hi" | html.pl
>>> get line hi
>>> [panda]$
>>>
>>> perldoc -f select
>>> perldoc IO::Select
>>
>>Thanks! This is what I was thinking of; I'll take a look.
> 
> I really that's too much work.  The -t file test should be sufficient:
> 
>   if (@ARGV) {
> # getting input via command-line arg
> $html = shift;
>   }
>   elsif (-t STDIN) {
> # STDIN is the user's terminal (as opposed to a piped stream)
> usage();
>   }
>   else {
> # the user has piped us something
> $html = join "", ;
>   }
> 
> See 'perldoc -f -X' for more details.
> 

Jeff,
your version only checks to see is STDIN is attached to a tty. for example, 
run your script from a crontab and you see will it never prints the usage. 
(ie, it always thinks that there is something to work with even there isn't 
any).

david
-- 
$_=q,015001450154015401570040016701570162015401440041,,*,=*|=*_,split+local$";
map{~$_&1&&{$,<<=1,[EMAIL PROTECTED]||3])=>~}}0..s~.~~g-1;*_=*#,

goto=>print+eval

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: checking for no input

2003-09-09 Thread Jeff 'japhy' Pinyan
On Sep 9, Kevin Pfeiffer said:

>In article <[EMAIL PROTECTED]>, David wrote:
>
>[...]
>> [panda]$ html.pl
>> no input
>> [panda]$ html.pl file.html
>> get file file.html
>> [panda]$ echo "hi" | html.pl
>> get line hi
>> [panda]$
>>
>> perldoc -f select
>> perldoc IO::Select
>
>Thanks! This is what I was thinking of; I'll take a look.

I really that's too much work.  The -t file test should be sufficient:

  if (@ARGV) {
# getting input via command-line arg
$html = shift;
  }
  elsif (-t STDIN) {
# STDIN is the user's terminal (as opposed to a piped stream)
usage();
  }
  else {
# the user has piped us something
$html = join "", ;
  }

See 'perldoc -f -X' for more details.

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
 what does y/// stand for?   why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: checking for no input

2003-09-09 Thread Kevin Pfeiffer
In article <[EMAIL PROTECTED]>, David wrote:

[...] 
> [panda]$ html.pl
> no input
> [panda]$ html.pl file.html
> get file file.html
> [panda]$ echo "hi" | html.pl
> get line hi
> [panda]$
> 
> perldoc -f select
> perldoc IO::Select

Thanks! This is what I was thinking of; I'll take a look.


-- 
Kevin Pfeiffer
International University Bremen


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: checking for no input

2003-09-09 Thread david
Kevin Pfeiffer wrote:

> In article <[EMAIL PROTECTED]>, John W. Krahn wrote:
> 
>> "R. Joseph Newton" wrote:
>>> 
>>> Kevin Pfeiffer wrote:
>>> 
>>> > I'm looking at HTML::TokeParser. It expects a scalar with a filename
>>> > or a reference to a scalar containing the data to parse.
>>> >
>>> > This works fine:
>>> >
>>> > my $html;
>>> > if (@ARGV) {  # get filename for
>>> > TokeParser
>>> >   $html = shift;
>>> > } else {
>>> >   my @html = <>;
>>> 
>>> Where is the diamond operator here supposed to be filled from?
>> 
>> <> treats the elements of @ARGV as file names and opens them in order
>> and returns their contents but if @ARGV is empty it returns the contents
>> of STDIN.
> 
> This is what I'm stuck on - is there a way to determine if STDIN is
> getting/is going to get/has gotten any contents?
> 

this might be a little late but the select(r,w,e,t) syscall is what you 
need. the IO::Select module (standard) has a nice OO interface to setting 
up the correct mask for you:

#!/usr/bin/perl -w
use strict;

#-- html.pl

use IO::Select;

if(@ARGV){

my $filename = shift;

print "get file $filename\n";

}else{

my $buf;
my $line;

my $io = IO::Select->new(\*STDIN);

while($io->can_read(0)){
last unless(sysread(STDIN,$buf,1024));
$line .= $buf;
}

if(defined $line){
print "get line $line";
}else{
print STDERR "no input\n";
}
}

__END__

[panda]$ html.pl
no input
[panda]$ html.pl file.html
get file file.html
[panda]$ echo "hi" | html.pl
get line hi
[panda]$

perldoc -f select
perldoc IO::Select

david
-- 
$_=q,015001450154015401570040016701570162015401440041,,*,=*|=*_,split+local$";
map{~$_&1&&{$,<<=1,[EMAIL PROTECTED]||3])=>~}}0..s~.~~g-1;*_=*#,

goto=>print+eval

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: checking for no input

2003-09-09 Thread Jeff 'japhy' Pinyan
On Sep 9, R. Joseph Newton said:

>> >> Kevin Pfeiffer wrote:
>> >>
>> >> > I'm looking at HTML::TokeParser. It expects a scalar with a filename or
>> >> > a reference to a scalar containing the data to parse.
>
>Not the provlem at all, Kevin.  The problem is those damned extra
>operators, in this case the reference-to operator '\' preceding your join
>statement.  Itr should be one or the other, either join or take a
>reference.  Doing both just toasts the code:

No, the HTML::TokeParser method he's using requires either a scalar with a
filename, or a reference to a scalar containing the data to parse.  He's
not just opening a file with open(), he's have a module do the work.

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
 what does y/// stand for?   why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: checking for no input

2003-09-09 Thread R. Joseph Newton
Kevin Pfeiffer wrote:

> In article <[EMAIL PROTECTED]>, John W. Krahn wrote:
>
> > "R. Joseph Newton" wrote:
> >>
> >> Kevin Pfeiffer wrote:
> >>
> >> > I'm looking at HTML::TokeParser. It expects a scalar with a filename or
> >> > a reference to a scalar containing the data to parse.
> >> >
> >> > This works fine:
> >> >
> >> > my $html;
> >> > if (@ARGV) {  # get filename for TokeParser
> >> >   $html = shift;
> >> > } else {
> >> >   my @html = <>;
> >>
> >> Where is the diamond operator here supposed to be filled from?
> >
> > <> treats the elements of @ARGV as file names and opens them in order
> > and returns their contents but if @ARGV is empty it returns the contents
> > of STDIN.
>
> This is what I'm stuck on - is there a way to determine if STDIN is
> getting/is going to get/has gotten any contents?
>
> I thought I would just check with "unless @html...", but the script never
> gets that far, it's waiting for  which never arrives.
>
> $ ./myscript
>
> I just thought that if @ARGV is empty and nothing is being piped to the
> script that I should be able to print a usage message.

Not the provlem at all, Kevin.  The problem is those damned extra operators, in
this case the reference-to operator '\' preceding your join statement.  Itr
should be one or the other, either join or take a reference.  Doing both just
toasts the code:

Greetings! E:\d_drive\perlStuff>perl -w
my $html;
if (@ARGV) {  # get filename for TokeParser
  $html = shift;
} else {
  my @html = <>;
  $html = \(join '', @html);  # pass scalar ref to module
}
open IN, $html;
print "$_" while ();
^Z
FileTest.html
^Z
readline() on closed filehandle IN at - line 9.

Greetings! E:\d_drive\perlStuff>perl -w
my $html;
if (@ARGV) {  # get filename for TokeParser
  $html = shift;
} else {
  my @html = <>;
  $html = \(join '', @html);  # pass scalar ref to module
}
chomp $html;
open IN, $html;
print "$_" while ();
^Z
FileTest.html
^Z
readline() on closed filehandle IN at - line 10.

[snip--about five minor adjustments]

Greetings! E:\d_drive\perlStuff>perl -w
my $html;
if (@ARGV) {  # get filename for TokeParser
  $html = shift;
} else {
  my @html = <>;
  print "$_\n" for (@html);
  $html = (join '', @html);  # pass [either] scalar [or] ref [to
array] to module
  print "$html\n";
}
chomp $html;
print "$html\n";
open IN, $html;
print "$_" while ();
^Z
FileTest.html
^Z
FileTest.html

FileTest.html

FileTest.html


   File Test 


  


  






Greetings! E:\d_drive\perlStuff>

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: checking for no input

2003-09-09 Thread Jeff 'japhy' Pinyan
I deleted the original message by mistake, but here's your answer:  you
want to use the -t file test.

  if (@ARGV) {
# getting input via command-line arg
$html = shift;
  }
  elsif (-t STDIN) {
# STDIN is the user's terminal (as opposed to a piped stream)
usage();
  }
  else {
# the user has piped us something
$html = join "", ;
  }

See 'perldoc -f -X' for more details.

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
 what does y/// stand for?   why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: checking for no input

2003-09-09 Thread Kevin Pfeiffer
In article <[EMAIL PROTECTED]>, John W. Krahn wrote:

> "R. Joseph Newton" wrote:
>> 
>> Kevin Pfeiffer wrote:
>> 
>> > I'm looking at HTML::TokeParser. It expects a scalar with a filename or
>> > a reference to a scalar containing the data to parse.
>> >
>> > This works fine:
>> >
>> > my $html;
>> > if (@ARGV) {  # get filename for TokeParser
>> >   $html = shift;
>> > } else {
>> >   my @html = <>;
>> 
>> Where is the diamond operator here supposed to be filled from?
> 
> <> treats the elements of @ARGV as file names and opens them in order
> and returns their contents but if @ARGV is empty it returns the contents
> of STDIN.

This is what I'm stuck on - is there a way to determine if STDIN is
getting/is going to get/has gotten any contents?

I thought I would just check with "unless @html...", but the script never
gets that far, it's waiting for  which never arrives.

$ ./myscript

I just thought that if @ARGV is empty and nothing is being piped to the
script that I should be able to print a usage message.

-- 
Kevin Pfeiffer


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: checking for no input

2003-09-08 Thread John W. Krahn
"R. Joseph Newton" wrote:
> 
> Kevin Pfeiffer wrote:
> 
> > I'm looking at HTML::TokeParser. It expects a scalar with a filename or a
> > reference to a scalar containing the data to parse.
> >
> > This works fine:
> >
> > my $html;
> > if (@ARGV) {  # get filename for TokeParser
> >   $html = shift;
> > } else {
> >   my @html = <>;
> 
> Where is the diamond operator here supposed to be filled from?

<> treats the elements of @ARGV as file names and opens them in order
and returns their contents but if @ARGV is empty it returns the contents
of STDIN.


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: checking for no input

2003-09-08 Thread R. Joseph Newton
Kevin Pfeiffer wrote:

> I'm looking at HTML::TokeParser. It expects a scalar with a filename or a
> reference to a scalar containing the data to parse.
>
> This works fine:
>
> my $html;
> if (@ARGV) {  # get filename for TokeParser
>   $html = shift;
> } else {
>   my @html = <>;

Where is the diamond operator here supposed to be filled from?  I must admit,
I am not terribly familiar with it, since I don't use it.  I would recommend
that you l.likewise avoid, unless you are certain that it will actuall get
loaded.  Is there a problem with using the more explicit, and readable
?  From your description of the desired usage, in conjunction with a
pipe, this should render some a little less mysterious, at least.

>
>   $html = \(join '', @html);  # pass scalar ref to module
> }
>
> Unless there is no input at all; then the script hangs. So I tried this:
>
> my $html;
> if (@ARGV) {  # get filename for TokeParser
>   $html = shift;
> } elsif (my @html = <>) {
>   $html = \(join '', @html);  # pass scalar ref to module
> } else {
>   die "Usage '$0 [filename]' or 'datasource | $0'\n";
> }
>
> ...with the same result.
>
> -K

I am still a little unclear on what you are trying to do.  If you have a
program that will output html, the code should work, possibly even with the
default diamond operator, certainly with .  What is the context in
which you are testing it?

Hmmm.  Are you sure the hang is not simply the script waiting for the next
line of input?  On my ActiveState installation on Win 32, I signal end of
input with CTL-Z [shows as ^Z on my console.].

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]