Revision: 1421
Author:   [email protected]
Date:     Fri Jun 15 09:04:32 2012
Log: Add --debug, add --open docs, use Browser::Open if available, rework related logic.

http://code.google.com/p/perl-devel-nytprof/source/detail?r=1421

Modified:
 /trunk/bin/nytprofhtml

=======================================
--- /trunk/bin/nytprofhtml      Mon Jan  2 12:45:54 2012
+++ /trunk/bin/nytprofhtml      Fri Jun 15 09:04:32 2012
@@ -13,6 +13,7 @@
 use strict;

 use Carp;
+use Config qw(%Config);
 use Getopt::Long;
 use List::Util qw(sum max);
 use File::Copy;
@@ -71,6 +72,7 @@
     'minimal|m!'=> \my $opt_minimal,
     'mergeevals!'=> \(my $opt_mergeevals = 1),
     'profself!'     => sub { }, # handled in BEGIN above
+    'debug!'        => \my $opt_debug,
 ) or do { exit usage(); };


@@ -82,6 +84,7 @@
--file <file>, -f <file> Read profile data from the specified file [default: nytprof.out] --out <dir>, -o <dir> Write report files to this directory [default: nytprof]
  --delete,      -d         Delete any old report files in <dir> first
+ --open                    Open the generated report in a web browser
  --lib <lib>,   -l <lib>   Add <lib> to the beginning of \@INC
--minimal, -m Don't generate graphviz .dot files or block/sub-level reports
  --no-mergeevals           Disable merging of string evals
@@ -1226,57 +1229,58 @@

 sub open_browser_on {
     my $index = shift;
-    if (eval { require ActiveState::Browser; 1 }) {
-        ActiveState::Browser::open($index);
+
+ return if eval { require Browser::Open; Browser::Open::open_browser($index, 1); };
+    warn "$@\n" if $@ && $opt_debug;
+
+ return if eval { require ActiveState::Browser; ActiveState::Browser::open($index); 1 };
+    warn "$@\n" if $@ && $opt_debug && $^O eq "MSWin32";
+
+
+    my $BROWSER;
+    if ($^O eq "MSWin32") {
+        $BROWSER = "start %s";
+    }
+    elsif ($^O eq "darwin") {
+        $BROWSER = "/usr/bin/open %s";
     }
     else {
-        my $BROWSER;
-        if ($^O eq "MSWin32") {
-            $BROWSER = "start %s";
-        }
-        elsif ($^O eq "darwin") {
-            $BROWSER = "/usr/bin/open %s";
+        my @try = qw(xdg-open);
+        if ($ENV{BROWSER}) {
+            push(@try, split(/:/, $ENV{BROWSER}));
         }
         else {
-            my @try = qw(xdg-open);
-            if ($ENV{BROWSER}) {
-                push(@try, split(/:/, $ENV{BROWSER}));
+            push(@try, qw(firefox galeon mozilla opera netscape));
+        }
+        unshift(@try, "kfmclient") if $ENV{KDE_FULL_SESSION};
+        unshift(@try, "gnome-open") if $ENV{GNOME_DESKTOP_SESSION_ID};
+        for (grep { have_prog($_) } @try) {
+            if ($_ eq "kfmclient") {
+                $BROWSER .= " openURL %s";
+            }
+            elsif ($_ eq "gnome-open" || $_ eq "opera") {
+                $BROWSER = "$_ %s";
             }
             else {
-                push(@try, qw(firefox galeon mozilla opera netscape));
-            }
-            unshift(@try, "kfmclient") if $ENV{KDE_FULL_SESSION};
-            unshift(@try, "gnome-open") if $ENV{GNOME_DESKTOP_SESSION_ID};
-            for (@try) {
-                if (have_prog($_)) {
-                    if ($_ eq "kfmclient") {
-                        $BROWSER .= " openURL %s";
-                    }
-                    elsif ($_ eq "gnome-open" || $_ eq "opera") {
-                        $BROWSER = "$_ %s";
-                    }
-                    else {
-                        $BROWSER = "$_ %s &";
-                    }
-                    last;
-                }
-            }
-        }
-        if ($BROWSER) {
-            (my $cmd = $BROWSER) =~ s/%s/"$index"/;
-            #warn "Running $cmd\n";
-            system($cmd);
-        }
-        else {
- warn "Don't know how to invoke your web browser.\nPlease visit $index yourself!\n";
+                $BROWSER = "$_ %s &";
+            }
+            last;
         }
     }
+    if ($BROWSER) {
+        (my $cmd = $BROWSER) =~ s/%s/"$index"/;
+        warn "Running $cmd\n" if $opt_debug;
+        system($cmd);
+    }
+    else {
+ warn "Don't know how to invoke your web browser.\nPlease visit $index yourself!\n";
+    }
 }


 sub have_prog {
     my $prog = shift;
-    for (split(":", $ENV{PATH})) {
+    for (split($Config{path_sep}, $ENV{PATH})) {
         return 1 if -x "$_/$prog";
     }
     return 0;
@@ -1898,6 +1902,8 @@

 Make your web browser visit the report after it has been generated.

+If this doesn't work well for you, try installing the L<Browser::Open> module.
+
 =item -h, --help

 Print the help message

--
You've received this message because you are subscribed to
the Devel::NYTProf Development User group.

Group hosted at:  http://groups.google.com/group/develnytprof-dev
Project hosted at:  http://perl-devel-nytprof.googlecode.com
CPAN distribution:  http://search.cpan.org/dist/Devel-NYTProf

To post, email:  [email protected]
To unsubscribe, email:  [email protected]

Reply via email to