http://qs1969.pair.com?node_id=501608

kanish has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have a .pl file which was done by perl trainers.

I need to write the code to validate the .pl file which was done by perl trainers.

The following are I have to write code to validate them.

  • 1. Find the unncessary grouping. i.e.
    $a=~s#(foo)(bar)#$1#;
    Here first group only given in replace, but second group not in replace. So It'll eat memory unnecessarily.
  • 2. If variable declared, but not in use anywhere. It'll show.
  • 3. If there is a subroutine, but it'll not call anywhere, It'll show.
  • ...
  • ...

    I need to validate these things in my coding. Please give if there is any short way or any modules.

    Thanks in Advance.

    Kanishk

  • Replies are listed 'Best First'.
    Re: Write Coding to validate the .pl Files
    by dtr (Scribe) on Oct 20, 2005 at 11:55 UTC

      Might Devel::Cover help at all? It's useful in test suites to analyse how much of the code in a module is actually tested by the test suite. It'll show branches that aren't followed, and statements that are never reached in a nice pretty html file.

    Re: Write Coding to validate the .pl Files
    by blazar (Canon) on Oct 20, 2005 at 10:59 UTC

      IIUC you're not really asking how to "validate the .pl Files". You're rather asking how to find code that's possibly correct syntactically/semantically but that does something in an awkward or suboptimal way. If really so, then chances are that there may not be easy if (any) ways to do most of the things you want to.

      After all

      "Nothing but perl can parse Perl."

      But some checks of some relevance for your needs may already be taken care of by the strict and warnings pragmas, since you did enforce their use anyway, didn't you?

        Don't forget use diagnostics;.


        holli, /regexed monk/
    Re: Write Coding to validate the .pl Files
    by diotalevi (Canon) on Oct 20, 2005 at 14:40 UTC
      Incidentally, that second capture won't waste memory beyond what's required to note some offsets. Once you have any captures at all, the original string is copied and then all captures are just implemented like substr() lookups into that string.
    Re: Write Coding to validate the .pl Files
    by planetscape (Chancellor) on Oct 21, 2005 at 06:15 UTC
    Re: Write Coding to validate the .pl Files
    by Util (Priest) on Oct 22, 2005 at 01:22 UTC

      B::Xref is recommended in some of the nodes that planetscape listed. B::Xref generates a cross reference listing of all definitions and uses of variables, subroutines and formats in a Perl program. Here is loosely tested code that uses B::Xref to detect unreferenced subroutines:

      use strict; use warnings; my $script_name = shift or die; my @lines = `perl -MO=Xref,-r $script_name` or die; my %used_subs; my %defined_subs; foreach (@lines) { my ($file, $subr, $line, $package, $type, $object, $event) = split; next if $file ne $script_name or $type ne '&' or $package ne 'main'; if ( $event eq 'subdef' ) { $defined_subs{$object} = 1; } elsif ( $event eq 'subused' ) { $used_subs{$object} = 1; } } foreach my $key ( sort keys %defined_subs ) { next if $used_subs{$key}; print "Sub not directly called: '$key'\n"; }