[26880] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8879 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jan 23 18:05:48 2006

Date: Mon, 23 Jan 2006 15:05:18 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 23 Jan 2006     Volume: 10 Number: 8879

Today's topics:
        Cannot read from STDIN <wh2leung@student.cs.uwaterloo.ca>
    Re: Cannot read from STDIN <noreply@gunnar.cc>
    Re: Cannot read from STDIN <jgibson@mail.arc.nasa.gov>
    Re: Cannot read from STDIN <wh2leung@student.cs.uwaterloo.ca>
    Re: Cannot read from STDIN <noreply@gunnar.cc>
    Re: cpp-style include for perl? <joe@inwap.com>
        Graph.0.69 Module---how to get a DAG from graph with cy <seanatpurdue@hotmail.com>
    Re: Graph.0.69 Module---how to get a DAG from graph wit <jgibson@mail.arc.nasa.gov>
        perl -w should warn about print $m++,$m,$m++ <jidanni@jidanni.org>
    Re: randomly choose some uniq elements of an array <jgibson@mail.arc.nasa.gov>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Mon, 23 Jan 2006 13:54:01 -0500
From: William <wh2leung@student.cs.uwaterloo.ca>
Subject: Cannot read from STDIN
Message-Id: <Pine.GSO.4.64.0601231351020.1818@cpu02.student.cs.uwaterloo.ca>

Perl script in question: http://mkmxg00/cgi/confirmUpload.pl is as 
follows:

#!/usr/bin/perl -w

#===============================================================================
# confirmUpload.pl
# once the user clicks "Confirm Modifications", this script picks up the 
form's
# newest data from STDIN (POST method), then saves it to the dummylist
#===============================================================================

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use strict;

my $query = new CGI;

# add code that reads in the "Confirm Modifications" request from the CGI 
buffer
my $buffer;
read ( STDIN, $buffer, $ENV{'CONTENT_LENGTH'} );

# buffer now contains data to be written to the dummylist
open ( OUTFD, "/mkapp/webapps/mxrt/data/extra_desk_tickers.txt" ) or 
error("Couldn't open file: $DATAFILE\n");
print ( OUTFD $buffer );
close ( OUTFD );

exit 0;


The javascript code that invoked the above Perl script:
I am currently using XMLHttpRequest as follows:
function saveText( scroll_list, t_area, listToBeUpdated ) {
     var updated = new Option();
     updated.value = t_area.value;
     updated.text = t_area.text;
     for(var i = 0; i < scroll_list.options.length; i++) {
         if( scroll_list.options[i].selected )  {
             scroll_list.options[scroll_list.selectedIndex].text = updated.value;
             scroll_list.options[scroll_list.selectedIndex].value= updated.value;
             break;
         }
     }
     var confirmReq;
     var url = "http://mkmxg00/cgi/confirmUpload.pl";
     confirmReq = new ActiveXObject( "Microsoft.XMLHTTP" );
     confirmReq.onreadystatechange = processReqChange;
     confirmReq.open( "POST", url, true );
     confirmReq.send( "" );

     alert( url );
}

function processReqChange()    {
    if ( confirmReq.readyState == 4 )    {
       if ( confirmReq.status == 200 )  {
             alert( "passed!" );
       }
    }
    else {
       alert( confirmReq.readyState + ", " + confirmReq.status );
    }
}



My problem: nothing is written to 
/mkapp/webapps/mxrt/data/extra_desk_tickers.txt

I have already invoked confirmUpload.pl with the XMLHttpRequest confirmReq 
(I am using IE 6.0 on Windows XP).  Why does the file contains no output 
from the CGI buffer?



------------------------------

Date: Mon, 23 Jan 2006 21:24:37 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Cannot read from STDIN
Message-Id: <43ksg1F1o4fr8U1@individual.net>

William wrote:
> 
> use CGI;
> use CGI::Carp qw(fatalsToBrowser);
> use strict;
> 
> my $query = new CGI;

That means that CGI.pm reads STDIN, which is empty afterwords.

> # add code that reads in the "Confirm Modifications" request from the 
> CGI buffer
> my $buffer;
> read ( STDIN, $buffer, $ENV{'CONTENT_LENGTH'} );

Consequently that won't read anything.

Since you are using CGI, why don't you use it to get and parse POSTed data?

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: Mon, 23 Jan 2006 12:41:19 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Cannot read from STDIN
Message-Id: <230120061241195291%jgibson@mail.arc.nasa.gov>

In article
<Pine.GSO.4.64.0601231351020.1818@cpu02.student.cs.uwaterloo.ca>,
William <wh2leung@student.cs.uwaterloo.ca> wrote:

> Perl script in question: http://mkmxg00/cgi/confirmUpload.pl is as 
> follows:
> 
> #!/usr/bin/perl -w
> 
>
> #=============================================================================
> ==
> # confirmUpload.pl
> # once the user clicks "Confirm Modifications", this script picks up the 
> form's
> # newest data from STDIN (POST method), then saves it to the dummylist
>
> #=============================================================================
> ==
> 
> use CGI;
> use CGI::Carp qw(fatalsToBrowser);
> use strict;
> 
> my $query = new CGI;
> 
> # add code that reads in the "Confirm Modifications" request from the CGI 
> buffer
> my $buffer;
> read ( STDIN, $buffer, $ENV{'CONTENT_LENGTH'} );
> 
> # buffer now contains data to be written to the dummylist
> open ( OUTFD, "/mkapp/webapps/mxrt/data/extra_desk_tickers.txt" ) or 
> error("Couldn't open file: $DATAFILE\n");

If you are going to write to the file, you should open it for writing:

  open ( OUTFD, '>', 
    "/mkapp/webapps/mxrt/data/extra_desk_tickers.txt" ) or 
    error("Couldn't open file: $DATAFILE\n");

$DATAFILE doesn't contain anything here. Please post real code.

> print ( OUTFD $buffer );
> close ( OUTFD );
> 
> exit 0;
> 
> 
> The javascript code that invoked the above Perl script:
> I am currently using XMLHttpRequest as follows:

[javascript snipped]

> My problem: nothing is written to 
> /mkapp/webapps/mxrt/data/extra_desk_tickers.txt
> 
> I have already invoked confirmUpload.pl with the XMLHttpRequest confirmReq 
> (I am using IE 6.0 on Windows XP).  Why does the file contains no output 
> from the CGI buffer?
>

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


------------------------------

Date: Mon, 23 Jan 2006 15:41:24 -0500
From: William <wh2leung@student.cs.uwaterloo.ca>
Subject: Re: Cannot read from STDIN
Message-Id: <Pine.GSO.4.64.0601231538560.1818@cpu02.student.cs.uwaterloo.ca>

On Mon, 23 Jan 2006, Gunnar Hjalmarsson wrote:

> William wrote:
>> 
>> use CGI;
>> use CGI::Carp qw(fatalsToBrowser);
>> use strict;
>> 
>> my $query = new CGI;
>
> That means that CGI.pm reads STDIN, which is empty afterwords.

I don't quite understand what you meant.
so you meant $query contains all the content of my CGI buffer?

>
>> # add code that reads in the "Confirm Modifications" request from the 
>> CGI buffer my $buffer; read ( STDIN, $buffer, $ENV{'CONTENT_LENGTH'} );
>
> Consequently that won't read anything.
>
> Since you are using CGI, why don't you use it to get and parse POSTed data?

as in query->param("parameter_name")?


------------------------------

Date: Mon, 23 Jan 2006 21:56:08 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Cannot read from STDIN
Message-Id: <43kub4F1onf8pU1@individual.net>

William wrote:
> On Mon, 23 Jan 2006, Gunnar Hjalmarsson wrote:
>> William wrote:
>>>
>>> my $query = new CGI;
>>
>> That means that CGI.pm reads STDIN, which is empty afterwords.
> 
> I don't quite understand what you meant.
> so you meant $query contains all the content of my CGI buffer?

No, $query is just an object reference to the CGI object.

>>> # add code that reads in the "Confirm Modifications" request from the 
>>> CGI buffer my $buffer; read ( STDIN, $buffer, $ENV{'CONTENT_LENGTH'} );
>>
>> Consequently that won't read anything.
>>
>> Since you are using CGI, why don't you use it to get and parse POSTed 
>> data?
> 
> as in query->param("parameter_name")?

Almost. As in $query->param("parameter_name").
--------------^

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: Mon, 23 Jan 2006 11:35:32 -0800
From: Joe Smith <joe@inwap.com>
Subject: Re: cpp-style include for perl?
Message-Id: <Mf2dnSal5o6arUjeRVn-uQ@comcast.com>

Mike Ballard wrote:

>     @filelist = `/bin/ls -1 abs*`;

That's not the right way to do it.
	@filelist = glob 'abs*';

		-Joe


------------------------------

Date: Mon, 23 Jan 2006 14:50:13 -0500
From: Sean <seanatpurdue@hotmail.com>
Subject: Graph.0.69 Module---how to get a DAG from graph with cycles
Message-Id: <dr3c1l$gib$1@mailhub227.itcs.purdue.edu>

Here I use an example to illustrate the problem I met when using Graph 
Theory Module: I have a directed graph looks like the following (see the 
figure).
Edges in the original directed graph:
[main] -> [foo]; [main] -> [bar];
[foo]   -> [trouble];
[trouble] -> [foo];

I want to find all the cycles in the graph. whenever I find a cycle, I 
want to break it by deleting an edge starting from a vertex farthest 
away from node [main] to get the following graph ---in this case, 
deleting [trouble] -> [foo] because [trouble] is the farthest node in 
this cycle.
So, edges after the desired manipulation are:
[main] -> [foo]; [main] -> [bar];
[foo]   -> [trouble];
-----------------------fiugre of original graph------------
     [main]
      /    \
   [foo]    [bar]
    /  /
   /  /
  [trouble]
-----------------------end of the figure-------------------

I tried to use @v = $g->connected_component_by_index($i), but it seems
to return single nodes (which is deemed as strongly connected component
itself), which is not what I want.
$g->find_a_cycle does not work either, because it returns same cycle no
matter how many times you call it.

I am newbie in perl. Can someone give me a suggestion how to utilize
the existing APIs in Graph.0.69 to achieve what I want?
Here is what I experimented a bit(which failed), and it may give a 
better background of my question.

====================================================================
#mycode.pl
       6
       7 use Graph;
       8 my $gcall = Graph->new;
      11
      12 while (<>) {
      13     chomp;
      14     if (/^(.*)\t->\t(.*)\t=>\w*\t->(\d*)/) {
      15         $gcall->add_edge($1, $2);
      16         print $1, " to ", $2, " with ", $3, "\n";
      17         #print $2, "\n";
      18     }
      19
      20 }
      21########at this point, the graph has been built#############
      46
      47 foreach (1..5) {
      48   print "SCC $_:",
             $gcall->strongly_connected_component_by_index($_), "\n\n";
      49   print "cycle $_:",
             $gcall->find_a_cycle;
      50}
        ######### my tries to try to report cycles, FAILED  ####

========================================================================


------------------------------

Date: Mon, 23 Jan 2006 14:28:43 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Graph.0.69 Module---how to get a DAG from graph with cycles
Message-Id: <230120061428431902%jgibson@mail.arc.nasa.gov>

In article <dr3c1l$gib$1@mailhub227.itcs.purdue.edu>, Sean
<seanatpurdue@hotmail.com> wrote:

> Here I use an example to illustrate the problem I met when using Graph 
> Theory Module: I have a directed graph looks like the following (see the 
> figure).
> Edges in the original directed graph:
> [main] -> [foo]; [main] -> [bar];
> [foo]   -> [trouble];
> [trouble] -> [foo];
> 
> I want to find all the cycles in the graph. whenever I find a cycle, I 
> want to break it by deleting an edge starting from a vertex farthest 
> away from node [main] to get the following graph ---in this case, 
> deleting [trouble] -> [foo] because [trouble] is the farthest node in 
> this cycle.
> So, edges after the desired manipulation are:
> [main] -> [foo]; [main] -> [bar];
> [foo]   -> [trouble];
> -----------------------fiugre of original graph------------
>      [main]
>       /    \
>    [foo]    [bar]
>     /  /
>    /  /
>   [trouble]
> -----------------------end of the figure-------------------
> 
> I tried to use @v = $g->connected_component_by_index($i), but it seems
> to return single nodes (which is deemed as strongly connected component
> itself), which is not what I want.
> $g->find_a_cycle does not work either, because it returns same cycle no
> matter how many times you call it.
> 
> I am newbie in perl. Can someone give me a suggestion how to utilize
> the existing APIs in Graph.0.69 to achieve what I want?
> Here is what I experimented a bit(which failed), and it may give a 
> better background of my question.
> 
> ====================================================================
> #mycode.pl
>        6
>        7 use Graph;
>        8 my $gcall = Graph->new;
>       11
>       12 while (<>) {
>       13     chomp;
>       14     if (/^(.*)\t->\t(.*)\t=>\w*\t->(\d*)/) {
>       15         $gcall->add_edge($1, $2);
>       16         print $1, " to ", $2, " with ", $3, "\n";
>       17         #print $2, "\n";
>       18     }
>       19
>       20 }
>       21########at this point, the graph has been built#############
>       46
>       47 foreach (1..5) {
>       48   print "SCC $_:",
>              $gcall->strongly_connected_component_by_index($_), "\n\n";
>       49   print "cycle $_:",
>              $gcall->find_a_cycle;
>       50}
>         ######### my tries to try to report cycles, FAILED  ####
> 
> ========================================================================

You have posted 19 lines of an at-least 50-line program, so your posted
program is not complete. Also, you do not show your data. Here is a way
to create a graph equivalent to your sample graph without reading
external data:

my $gcall = my $g = Graph->new( edges => 
  [ ['a','b'], ['a','c'], ['b','d'], ['d','b']] );

Disclaimer: I don't know much about graphs, and I don't know about all
the graph terminology used by the Graph module. In particular, I don't
know what "connected" (strongly or weakly) means, so I don't know how
that concept might be pertinent to your problem.

It is true that Graph::find_a_cycle() will find a random cycle if one
exists in your graph, and will only return one. However, if you then
break that cycle by removing an edge, then you can go on to finding and
breaking the next one.

I am not sure how to find the vertex "farthest from node [main]" in
cases more complex than your example. Here is a sample program that
uses Graph::APSP_Floyd_Warshall() to find the "all pairs shortest
paths" and Graph::path_length() to find the shortest path length from
the root of the graph to any node. If these do not give the results you
are looking for, then you will have to substitute some other method for
picking which edge from a cycle of vertices to delete. The program adds
a 3-vertex cycle to your original sample:

#!/usr/local/bin/perl
use strict;
use warnings;
use Graph;

my $g = Graph->new(
  edges => 
    [ 
      ['a','b'],
      ['a','c'],
      ['b','d'],
      ['d','b'],
      ['c','e'],
      ['e','f'],
      ['f','c'],
    ]
);

print "Graph: ", $g->stringify(), "\n";
my @v = sort $g->vertices();
print "Vertices: @v\n";

# find and remove all cycles
while( my @cyc = $g->find_a_cycle() ) {
  print "\nFound cycle: @cyc\n";
  my $apsp = $g->APSP_Floyd_Warshall();
  my %dist = map { $_ => $apsp->path_length('a',$_) } @cyc;
  my $far = (sort { $dist{$a} <=> $dist{$b} } keys %dist )[-1];
  print "Farthest vertex is $far\n";
  CYC: for my $v ( @cyc ) {
    next if $v eq $far;
    next unless $g->has_edge($far,$v);
    print "Remove edge (${far}->$v)\n";
    $g->delete_edge($far,$v);
    print "Graph is now: ", $g->stringify(), "\n";
    last CYC;
  }
}
 ... which gives as output:

Graph: a-b,a-c,b-d,c-e,d-b,e-f,f-c
Vertices: a b c d e f

Found cycle: b d
Farthest vertex is d
Remove edge (d->b)
Graph is now: a-b,a-c,b-d,c-e,e-f,f-c

Found cycle: e f c
Farthest vertex is f
Remove edge (f->c)
Graph is now: a-b,a-c,b-d,c-e,e-f

__END__

Note: this program can be made more efficient, but you should only
worry about that if your graph is very large.

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


------------------------------

Date: Mon, 23 Jan 2006 23:25:55 +0800
From: Dan Jacobson <jidanni@jidanni.org>
Subject: perl -w should warn about print $m++,$m,$m++
Message-Id: <87slrf2cfw.fsf@jidanni.org>

Idea: -w or -W should warn if one uses ++, -- modifying a variable
twice in the same statement, as noted on perlop, else users might
think:
 Should one feel bad about the apparent disorder?
 $ perl -wle 'print $m++,$m,$m++'
 021 #Hmm, that 2 should be 1?
 Same also if one uses $m++,$m,$m++ in perlform's formats.
 $ perl -wle 'print $m++ + $m + $m++'
 2 #OK with me.
 Moral: maybe ++ only safe with the = operator?:
 $ perl -wle '$e=$m++.$m.$m++; print $e'
 011



------------------------------

Date: Mon, 23 Jan 2006 10:15:27 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: randomly choose some uniq elements of an array
Message-Id: <230120061015274304%jgibson@mail.arc.nasa.gov>

In article <iin5t15vq4e14949riutff67ouhs08fl22@4ax.com>, robic0 wrote:

> On Sun, 22 Jan 2006 02:12:36 +0100, Martin Kissner <news@chaos-net.de> wrote:
> 
> >John W. Krahn wrote :
> >> Martin Kissner wrote:
> >

[advice on checking return of opendir snipped]

> Not sure where this follow will follow but..
> I want to remind the OP that opendir will actually "change directory" on the
> OS
> level so that a subsiquent glob will read from that directory.

This is not true. Calling opendir does _not_ change the default
directory of the process. You may be thinking of File::Find, which
_does_ change the default directory before calling the user's callback
function.

[additional ranting snipped]

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 8879
***************************************


home help back first fref pref prev next nref lref last post