Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Write Coding to validate the .pl Files

by kanish (Sexton)
on Oct 20, 2005 at 10:47 UTC ( #501608=perlquestion: print w/ replies, xml ) Need Help??
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

  • Comment on Write Coding to validate the .pl Files
    Download Code
    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 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 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 (Canon) 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"; }

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: perlquestion [id://501608]
    Approved by lamp
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (13)
    As of 2014-10-21 11:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (100 votes), past polls