| 1 | #!/usr/bin/perl -w | ||
| 2 | #------------------------------------------------------- | ||
| 3 | # Format the output of Data::Walker as HTML, and set up | ||
| 4 | # an HTTP daemon. In this way we can examine Perl objects | ||
| 5 | # using a Web browser. | ||
| 6 | # | ||
| 7 | # This script requires that you have the LWP and the HTTP | ||
| 8 | # bundles installed. If you don't have these installed, | ||
| 9 | # then you should install them anyway, because they are | ||
| 10 | # pretty cool. | ||
| 11 | #------------------------------------------------------- | ||
| 12 | |||
| 13 | use CGI qw(:standard :nodebug); | ||
| 14 | use HTTP::Daemon; | ||
| 15 | use HTTP::Status; | ||
| 16 | use HTTP::Request; | ||
| 17 | use Data::Walker; | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | |||
| 21 | #---------------------------------------------- | ||
| 22 | # Setup the HTTP daemon | ||
| 23 | # | ||
| 24 | sub walk_via_HTTP_daemon { | ||
| 25 | |||
| 26 | my ($w,$timeout,$port,$host) = @_; | ||
| 27 | |||
| 28 | $timeout = 180 unless defined $timeout; | ||
| 29 | $port = 0 unless defined $port; | ||
| 30 | |||
| 31 | my $d; | ||
| 32 | |||
| 33 | if ($port) { | ||
| 34 | $d = new HTTP::Daemon (LocalAddr => $host); | ||
| 35 | } else { | ||
| 36 | $d = new HTTP::Daemon (LocalAddr => $port, LocalPort => $port); | ||
| 37 | } | ||
| 38 | |||
| 39 | unless (defined $d) { | ||
| 40 | warn "Could not bind to port. I'm going to have to exit. Sorry.\n"; | ||
| 41 | exit(-1); | ||
| 42 | } | ||
| 43 | |||
| 44 | my $myurl = $d->url; | ||
| 45 | |||
| 46 | #---------------------------------------------- | ||
| 47 | # Daemonize: fork, and then detatch from the local shell. | ||
| 48 | # | ||
| 49 | defined(my $pid = fork) or die "Cannot fork: $!"; | ||
| 50 | |||
| 51 | if ($pid) { # The parent exits | ||
| 52 | print redirect($myurl); | ||
| 53 | exit 0; | ||
| 54 | } | ||
| 55 | |||
| 56 | close(STDOUT); # The child lives on, but disconnects | ||
| 57 | # from the local terminal | ||
| 58 | |||
| 59 | # We opt not to close STDERR here, because we actually might | ||
| 60 | # want to see error messages at the terminal. | ||
| 61 | |||
| 62 | |||
| 63 | #---------------------------------------------- | ||
| 64 | # Now we enter a never-ending listen loop. | ||
| 65 | # The program ends when the timer runs out. | ||
| 66 | # We do not have any alarm handler, we just exit. | ||
| 67 | # | ||
| 68 | LISTEN: { | ||
| 69 | |||
| 70 | alarm($timeout); # (re-)set the clock | ||
| 71 | |||
| 72 | my $c = $d->accept; # $c is a connection | ||
| 73 | redo LISTEN unless defined $c; | ||
| 74 | |||
| 75 | my $r = $c->get_request; # $r is a request | ||
| 76 | redo LISTEN unless defined $r; | ||
| 77 | |||
| 78 | #---------------------------------------------- | ||
| 79 | # Parse the document path and interpret it as a "path" | ||
| 80 | # into the object. Pass this to the Data::Walker::cd() method. | ||
| 81 | # | ||
| 82 | my $target = $r->{_url}->{path}; | ||
| 83 | $target = "/" unless defined $target and $target ne ""; | ||
| 84 | $target =~ s#/+#/#g; | ||
| 85 | $w->cd($target); | ||
| 86 | |||
| 87 | my $pwd = $w->pwd; | ||
| 88 | $pwd =~ s#->#/#g; | ||
| 89 | $pwd =~ s/[{}]//g; | ||
| 90 | $pwd =~ s#/+#/#g; | ||
| 91 | |||
| 92 | print $c "<h1>Data::Walker->cwd $pwd</h1>\n<pre>\n\n"; | ||
| 93 | ( my $pwd_parent = $pwd ) =~ s#/[^/]+$##; | ||
| 94 | |||
| 95 | #---------------------------------------------- | ||
| 96 | # Insert hyperlinks into the output of ls() | ||
| 97 | # | ||
| 98 | my @output = split /\n/, $w->ls("-al"); | ||
| 99 | |||
| 100 | foreach (@output) { | ||
| 101 | |||
| 102 | $_ = CGI::escapeHTML($_); | ||
| 103 | |||
| 104 | next unless m#(HASH|ARRAY)#; | ||
| 105 | s#^[-\w]+#<a href="/$pwd/$&">$&</a>#; | ||
| 106 | s#^\.\.#<a href="/$pwd_parent">$&</a>#; | ||
| 107 | s#/+#/#g; | ||
| 108 | } | ||
| 109 | |||
| 110 | #---------------------------------------------- | ||
| 111 | # Print the formatted lines to the client | ||
| 112 | # | ||
| 113 | local($,) = "\n"; | ||
| 114 | print $c @output; | ||
| 115 | close $c; | ||
| 116 | |||
| 117 | redo LISTEN; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | 1; # END | ||
| 123 |