Professional Web Applications Themes

checking for no input - PERL Beginners

I'm looking at HTML::TokePr. It expects a scalar with a filename or a reference to a scalar containing the data to p. This works fine: my $html; if (ARGV) { # get filename for TokePr $html = shift; } else { my html = <>; $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 TokePr $html = shift; } elsif (my html = <>) { $html = \(join '', html); # pass scalar ...

  1. #1

    Default checking for no input

    I'm looking at HTML::TokePr. It expects a scalar with a filename or a
    reference to a scalar containing the data to p.

    This works fine:

    my $html;
    if (ARGV) { # get filename for TokePr
    $html = shift;
    } else {
    my html = <>;
    $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 TokePr
    $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


    --
    Kevin Pfeiffer
    International University Bremen

    Kevin Pfeiffer Guest

  2. #2

    Default Re: checking for no input

    Kevin Pfeiffer wrote:
    > I'm looking at HTML::TokePr. It expects a scalar with a filename or a
    > reference to a scalar containing the data to p.
    >
    > This works fine:
    >
    > my $html;
    > if (ARGV) { # get filename for TokePr
    > $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
    <STDIN>? 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 TokePr
    > $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 <STDIN>. 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

    R. Joseph Newton Guest

  3. #3

    Default Re: checking for no input

    "R. Joseph Newton" wrote:
    >
    > Kevin Pfeiffer wrote:
    >
    > > I'm looking at HTML::TokePr. It expects a scalar with a filename or a
    > > reference to a scalar containing the data to p.
    > >
    > > This works fine:
    > >
    > > my $html;
    > > if (ARGV) { # get filename for TokePr
    > > $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
    John W. Krahn Guest

  4. #4

    Default Re: checking for no input

    In article <3F5D3D55.9F6246BCacm.org>, John W. Krahn wrote:
    > "R. Joseph Newton" wrote:
    >>
    >> Kevin Pfeiffer wrote:
    >>
    >> > I'm looking at HTML::TokePr. It expects a scalar with a filename or
    >> > a reference to a scalar containing the data to p.
    >> >
    >> > This works fine:
    >> >
    >> > my $html;
    >> > if (ARGV) { # get filename for TokePr
    >> > $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 <STDIN> 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

    Kevin Pfeiffer Guest

  5. #5

    Default Re: checking for no input

    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 "", <STDIN>;
    }

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

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


    Jeff 'Japhy' Pinyan Guest

  6. #6

    Default Re: checking for no input

    Kevin Pfeiffer wrote:
    > In article <3F5D3D55.9F6246BCacm.org>, John W. Krahn wrote:
    >
    > > "R. Joseph Newton" wrote:
    > >>
    > >> Kevin Pfeiffer wrote:
    > >>
    > >> > I'm looking at HTML::TokePr. It expects a scalar with a filename or
    > >> > a reference to a scalar containing the data to p.
    > >> >
    > >> > This works fine:
    > >> >
    > >> > my $html;
    > >> > if (ARGV) { # get filename for TokePr
    > >> > $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 <STDIN> 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 TokePr
    $html = shift;
    } else {
    my html = <>;
    $html = \(join '', html); # pass scalar ref to module
    }
    open IN, $html;
    print "$_" while (<IN>);
    ^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 TokePr
    $html = shift;
    } else {
    my html = <>;
    $html = \(join '', html); # pass scalar ref to module
    }
    chomp $html;
    open IN, $html;
    print "$_" while (<IN>);
    ^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 TokePr
    $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 (<IN>);
    ^Z
    FileTest.html
    ^Z
    FileTest.html

    FileTest.html

    FileTest.html
    <html>
    <head>
    <title> File Test </title>
    </head>
    <body>
    <form method="POST" action="FileTest.cgi" enctype="multipart/form-data">
    <input name="up_file" type="file" value="my_test.txt">
    <input type="submit">
    </form>
    </html>





    Greetings! E:\d_drive\perlStuff>

    Joseph

    R. Joseph Newton Guest

  7. #7

    Default Re: checking for no input

    On Sep 9, R. Joseph Newton said:
    >> >> Kevin Pfeiffer wrote:
    >> >>
    >> >> > I'm looking at HTML::TokePr. It expects a scalar with a filename or
    >> >> > a reference to a scalar containing the data to p.
    >
    >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::TokePr method he's using requires either a scalar with a
    filename, or a reference to a scalar containing the data to p. He's
    not just opening a file with open(), he's have a module do the work.

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

    Jeff 'Japhy' Pinyan Guest

  8. #8

    Default Re: checking for no input

    Kevin Pfeiffer wrote:
    > In article <3F5D3D55.9F6246BCacm.org>, John W. Krahn wrote:
    >
    >> "R. Joseph Newton" wrote:
    >>>
    >>> Kevin Pfeiffer wrote:
    >>>
    >>> > I'm looking at HTML::TokePr. It expects a scalar with a filename
    >>> > or a reference to a scalar containing the data to p.
    >>> >
    >>> > This works fine:
    >>> >
    >>> > my $html;
    >>> > if (ARGV) { # get filename for
    >>> > TokePr
    >>> > $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,015001450154015401570040016701570162015401440 041,,*,=*|=*_,split+local$";
    map{~$_&1&&{$,<<=1,$#.=qq~\x63\x68\x72\x28_[$_..$||3])=>~}}0..s~.~~g-1;*_=*#,

    goto=>print+eval
    David Guest

  9. #9

    Default Re: checking for no input

    In article <20030909172934.45352.qmailonion.perl.org>, 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

    Kevin Pfeiffer Guest

  10. #10

    Default Re: checking for no input

    On Sep 9, Kevin Pfeiffer said:
    >In article <20030909172934.45352.qmailonion.perl.org>, 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 "", <STDIN>;
    }

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

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

    Jeff 'Japhy' Pinyan Guest

  11. #11

    Default Re: checking for no input

    Jeff 'Japhy' Pinyan wrote:
    > On Sep 9, Kevin Pfeiffer said:
    >
    >>In article <20030909172934.45352.qmailonion.perl.org>, 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 "", <STDIN>;
    > }
    >
    > 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,015001450154015401570040016701570162015401440 041,,*,=*|=*_,split+local$";
    map{~$_&1&&{$,<<=1,$#.=qq~\x63\x68\x72\x28_[$_..$||3])=>~}}0..s~.~~g-1;*_=*#,

    goto=>print+eval
    David Guest

  12. #12

    Default Re: checking for no input

    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]japhypobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

Similar Threads

  1. cfcalendar date input into an input field
    By dpinder123 in forum Coldfusion Flash Integration
    Replies: 1
    Last Post: August 31st, 09:11 PM
  2. $_POST behavior from input=submit vs input-type=button
    By Victor A. Cuya in forum PHP Development
    Replies: 2
    Last Post: May 11th, 03:42 PM
  3. Replies: 0
    Last Post: December 31st, 06:40 PM
  4. Replies: 0
    Last Post: October 24th, 10:41 PM
  5. Input Regex (error checking)
    By Ned Cunningham in forum PERL Beginners
    Replies: 0
    Last Post: August 18th, 05:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139