Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > IO::Scalar insanity

Reply
Thread Tools

IO::Scalar insanity

 
 
bill
Guest
Posts: n/a
 
      03-01-2005



I run into the need to do this *all* the time, but I have not come
up with a solution. This need arises almost invariably during the
debugging of a CGI script. At the beginning of the run I want to
inspect all the input that the CGI script will read from STDIN,
but I want to then "restore" this input back to the STDIN stream
so that execution/debugging session can proceed normally.

I *think* the best solution would be to slurp all the text into a
lexical

my $input = do { local $/; <STDIN> };
# {
# open my $out, ">debugging_data" or die "$!\n";
# print $out $input;
# close $out or die "$!\n";
# }

associate this lexical with an IO::Scalar handle

my $SH = IO::Scalar->new(\$input);

and then redefine STDIN so that it points to this IO::Scalar.
*This* is the part that I just can't figure out. Following what
I see in tutorials and books, I have tried

open(STDIN, "<&$SH") or die "$!\n";

but the open fails with an "invalid argument" error message. I've
also tried

open(STDIN, "<&=$SH") or die "$!\n";

(which fails with the same error), and

*STDIN = *SH;

and many variants thereof, all of which fail silently.

It is obvious that I have *no clue* and am just wildly trying out
random stuff.

I've read every bit of documentation I can think of, but I have
not hit on the answer to this question. I know this is all very
basic, but if someone could spell it out for me I'd greatly appreciate
it.

bill
 
Reply With Quote
 
 
 
 
Mark Clements
Guest
Posts: n/a
 
      03-01-2005
bill wrote:
> I run into the need to do this *all* the time, but I have not come
> up with a solution. This need arises almost invariably during the
> debugging of a CGI script. At the beginning of the run I want to
> inspect all the input that the CGI script will read from STDIN,
> but I want to then "restore" this input back to the STDIN stream
> so that execution/debugging session can proceed normally.
>
> I *think* the best solution would be to slurp all the text into a
> lexical
>
> my $input = do { local $/; <STDIN> };
> # {
> # open my $out, ">debugging_data" or die "$!\n";
> # print $out $input;
> # close $out or die "$!\n";
> # }
>
> associate this lexical with an IO::Scalar handle
>
> my $SH = IO::Scalar->new(\$input);
>
> and then redefine STDIN so that it points to this IO::Scalar.
> *This* is the part that I just can't figure out. Following what
> I see in tutorials and books, I have tried
>
> open(STDIN, "<&$SH") or die "$!\n";


you probably want to open it on the file descriptor (take a look at fileno):

redwood 24357 $ perl -le 'open IN,"</etc/passwd";open STDIN,"<&".fileno(IN);
$a=<STDIN>;
print "$a\n"'
root:0:1:Super-User:/:/sbin/sh


*but* what you probably want to be doing is using the CGI module to
parse your cgi parameters. You can initialize this from a FileHandle or
IO::File object as necessary, so you could slurp STDIN into a lexical
variable, create an IO::String object from this, dump out the results
and then initialize the CGI object from this. Also check out
restore_parameters in the CGI module.

It is highly likely that I am missing something obvious here.

Mark
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-01-2005
bill <> wrote in news:d02hah$k8r$1
@reader2.panix.com:


> I run into the need to do this *all* the time, but I have not come
> up with a solution. This need arises almost invariably during the
> debugging of a CGI script. At the beginning of the run I want to
> inspect all the input that the CGI script will read from STDIN,
> but I want to then "restore" this input back to the STDIN stream
> so that execution/debugging session can proceed normally.


http://search.cpan.org/~lds/CGI.pm-
3.05/CGI.pm#CREATING_A_NEW_QUERY_OBJECT_FROM_AN_INPUT_F ILE

I am curious why that is not a satisfactory method.

Sinan.
 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      03-01-2005
In <d02hah$k8r$> bill <> writes:

>I run into the need to do this *all* the time, but I have not come
>up with a solution. This need arises almost invariably during the
>debugging of a CGI script. At the beginning of the run I want to
>inspect all the input that the CGI script will read from STDIN,
>but I want to then "restore" this input back to the STDIN stream
>so that execution/debugging session can proceed normally.


>I *think* the best solution would be to slurp all the text into a
>lexical


> my $input = do { local $/; <STDIN> };
> # {
> # open my $out, ">debugging_data" or die "$!\n";
> # print $out $input;
> # close $out or die "$!\n";
> # }


>associate this lexical with an IO::Scalar handle


> my $SH = IO::Scalar->new(\$input);


>and then redefine STDIN so that it points to this IO::Scalar.
>*This* is the part that I just can't figure out. Following what
>I see in tutorials and books, I have tried


> open(STDIN, "<&$SH") or die "$!\n";


>but the open fails with an "invalid argument" error message. I've
>also tried


> open(STDIN, "<&=$SH") or die "$!\n";


>(which fails with the same error), and


> *STDIN = *SH;


>and many variants thereof, all of which fail silently.


>It is obvious that I have *no clue* and am just wildly trying out
>random stuff.


>I've read every bit of documentation I can think of, but I have
>not hit on the answer to this question. I know this is all very
>basic, but if someone could spell it out for me I'd greatly appreciate
>it.


Not so basic, I think. IIRC Perl can't do what you want it to do;
something having to do with needing a real file descriptor associated
with the handle before it can dupe it; IO::Scalar and the like
don't provide the FILENO method, AFAIK.

But how about putting this at some suitable location in your source:

my $peek = do { local $/; <STDIN> };

unless (my $pid = open(STDIN, "-|")) {
die "Fork failed: $!" unless defined $pid;
print $peek;
exit 0;
}

The subsequent parent code will read from STDIN as before, but now
it's coming from the child process.

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      03-01-2005
In <Xns960C9E508FAA6asu1cornelledu@127.0.0.1> "A. Sinan Unur" <> writes:

>bill <> wrote in news:d02hah$k8r$1
>@reader2.panix.com:



>> I run into the need to do this *all* the time, but I have not come
>> up with a solution. This need arises almost invariably during the
>> debugging of a CGI script. At the beginning of the run I want to
>> inspect all the input that the CGI script will read from STDIN,
>> but I want to then "restore" this input back to the STDIN stream
>> so that execution/debugging session can proceed normally.


>http://search.cpan.org/~lds/CGI.pm-
>3.05/CGI.pm#CREATING_A_NEW_QUERY_OBJECT_FROM_AN_INPUT_F ILE


>I am curious why that is not a satisfactory method.


For starters, it assumes that the CGI script is implemented through
CGI.pm, which may not be the case. Even when the script is
implemented using CGI.pm, if it was written by someone else in a
less than perspicuous style, I prefer to minimize the amount of
such code that I must understand.

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-01-2005
kj <> wrote in
news:d02ohk$es4$:

> In <Xns960C9E508FAA6asu1cornelledu@127.0.0.1> "A. Sinan Unur"
> <> writes:
>
>>bill <> wrote in news:d02hah$k8r$1
>>@reader2.panix.com:

>
>
>>> I run into the need to do this *all* the time, but I have not come
>>> up with a solution. This need arises almost invariably during the
>>> debugging of a CGI script. At the beginning of the run I want to
>>> inspect all the input that the CGI script will read from STDIN,
>>> but I want to then "restore" this input back to the STDIN stream
>>> so that execution/debugging session can proceed normally.

>
>>http://search.cpan.org/~lds/CGI.pm-
>>3.05/CGI.pm#CREATING_A_NEW_QUERY_OBJECT_FROM_AN_INPUT_F ILE

>
>>I am curious why that is not a satisfactory method.

>
> For starters, it assumes that the CGI script is implemented through
> CGI.pm, which may not be the case.


I am not going to get into that argument.

> Even when the script is implemented using CGI.pm, if it was written
> by someone else in a less than perspicuous style, I prefer to minimize
> the amount of such code that I must understand.


This makes no sense. How can coding style be an issue when we are talking
about passing parameters using an input file? No matter how bad the
original coding style, the way you would initialize a new CGI object from
a parameter file is the same.

Sinan.
 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      03-02-2005
On Tue, 1 Mar 2005, kj wrote:

> In <Xns960C9E508FAA6asu1cornelledu@127.0.0.1> "A. Sinan Unur" <> writes:
>
> >http://search.cpan.org/~lds/CGI.pm-
> >3.05/CGI.pm#CREATING_A_NEW_QUERY_OBJECT_FROM_AN_INPUT_F ILE

>
> >I am curious why that is not a satisfactory method.

>
> For starters, it assumes that the CGI script is implemented through
> CGI.pm, which may not be the case.


Anyone who's expert enough to produce a safe and reliable
implementation that doesn't use CGI.pm will have no difficulty in
solving that problem without any help from us.

> Even when the script is implemented using CGI.pm, if it was written
> by someone else in a less than perspicuous style, I prefer to
> minimize the amount of such code that I must understand.


It sounds as if you're trying to solve a problem that has rather
little technical content, but is more an exercise in sociology.
 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      03-02-2005
In <. ac.uk> "Alan J. Flavell" <> writes:

>On Tue, 1 Mar 2005, kj wrote:


>> In <Xns960C9E508FAA6asu1cornelledu@127.0.0.1> "A. Sinan Unur" <> writes:
>>
>> >http://search.cpan.org/~lds/CGI.pm-
>> >3.05/CGI.pm#CREATING_A_NEW_QUERY_OBJECT_FROM_AN_INPUT_F ILE

>>
>> >I am curious why that is not a satisfactory method.

>>
>> For starters, it assumes that the CGI script is implemented through
>> CGI.pm, which may not be the case.


>Anyone who's expert enough to produce a safe and reliable
>implementation that doesn't use CGI.pm will have no difficulty in
>solving that problem without any help from us.


The OP didn't specify whether he was debugging his own code, or
that what he was debugging was a "safe and reliable" anything.

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      03-02-2005
On Wed, 2 Mar 2005, kj wrote, quoting me:

> >Anyone who's expert enough to produce a safe and reliable
> >implementation that doesn't use CGI.pm will have no difficulty in
> >solving that problem without any help from us.

>
> The OP didn't specify whether he was debugging his own code, or
> that what he was debugging was a "safe and reliable" anything.


And what conclusion would you draw from that? (I've already drawn
mine, by the way.)

 
Reply With Quote
 
bill
Guest
Posts: n/a
 
      03-02-2005



Thanks for all the help.

BTW, the code I'm debugging is indeed something I wrote, but it
uses SOAP::Lite, whose documentation I find pretty unhelpful and
whose code I find absolutely impenetrable. I have no idea whether
SOAP::Lite uses CGI.pm for its "CGI-based" servers.

I am surprised to learn that one can't make a string/buffer look
like STDIN in a Perl script. I could *swear* I saw this done
somewhere.

bill

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Management Insanity JavaEnquirer Java 6 07-21-2005 08:58 PM
OSPF to RIP redistribution control insanity check Ivan Ostreš Cisco 1 10-21-2004 05:17 PM
NOT off-topic. The definition of insanity is... Bay Area Dave Digital Photography 20 05-10-2004 05:21 AM
mouse wheel insanity Bob Computer Support 8 02-24-2004 11:49 PM
Viewstate Insanity Big D ASP .Net 6 01-06-2004 09:56 PM



Advertisments
 



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