Madhur Kashyap wrote:
Hello Perl Experts,

Hello,

I am facing a problem day-in and day-out with my perl code. There are
a couple of subroutines in it.  Below I am writing the stub-form of my
perl code.

[code moved to the end of message]

I am calling lot of subroutines in updateNetPairsDB () subroutine and readDefectData () is one of them. If I define all the subroutines in the sequence mentioned above then, I face lot of wierd problems in readDefectData () function. For example, have a look at the readDefectData () function, the construct where I am comparing whether $_[0]=~/SHORT/ never works.

You are using Perl4 style subroutine calls (&subroutine;) which may affect the contents of the @_ array inside the subroutine. You should use the Perl5 style of subroutine calls (without the ampersand.)


perldoc perlsub

You will say that the file from where I am
reading might not have the data in expected format. Let me give one or
two exmaple entries from that file

===================================================================
mechanism=ME1_SHORT x=3 y=10 k0=90
mechanism=ME2_SHORT x=5 y=10 k0=20
===================================================================

There are a couple of things which I tried to make my code work in the
expected fashion

+ I substituted the '=' in $_[0] with ':'   --- This worked

+ I used another variable instead of $_ for reading line and
performing other operations ---  did not work

+ I tried to split the value in $_[0] before comparing --- did not
work and infact it dont even performed splitting ..heaven knows why?

+ I declared one or two dummy variables and performed some unecessary
comparison operations --- again did not work and infact comparison in
those unecessary options also did not work.

+ I changed the sequence in which I had called the various functions
in updateNetPairsDB () function ---- did not work either

+ I changed the sequence in which the functions are defined/declared
to a different sequence --- THIS ONE WORKED LIKE A CHARM.

Also, any of the above attempts I did not get any warnings or errors
during initial compilation.
Also, the subroutine readDefectData when written in another perl
program with only this function worked seamlessly.

This is something extremely unusual for me as I am a beginner in perl
or rather I should say scripting languages. It had never use to happen
with C/C++. I had earlier faced similar issues with some perl programs
which I had written before.

Is there something wrong in the coding style which I am using, which
can potentially cause perl parser to behave abnormally? Are there any
specific guidelines to avoid these kind of problems afront? I am
planning to implement another big job in perl and need to make a
decision whether I should stick with perl or go back to C/C++. Please
help me out with this.

If you are coming to Perl from another language you should read the perltrap man page:


perldoc perltrap

If you have style questions:

perldoc perlstyle

If you have syntax questions:

perldoc perlsyn

To get a listing of all of Perl's man pages:

perldoc perl

Or a verbose listing:

perldoc perltoc


===================================================================
#!/usr/local/bin/perl
#Some comments
...
use strict;
use warnings;
no warnings 'uninitialized';

You shouldn't do that. The warnings pragma is lexically scoped so you should only turn off warnings in the smallest possible scope instead the whole program. And better yet, write the code so that it does not produce warnings at all.


perldoc perllexwarn

use Switch;
use IPC::Open2;
use FileHandle;

use vars qw($BFE_INST_DIR $techNode $gdsFile $netFile $topCellName
$excludeCells $outputFileName $pgNets $signalNetsOnly);

The vars pragma creates package variables which you almost always do not need. Have you tried creating those as lexically scoped variables?


sub main {
        &setup ();
        &extractSPRL ();
        &prepSVDB ();
        &updateNetPairsDB ();
}

sub setup {
}

sub extractSPRL {
}

sub prepSVDB {
}

sub loadNxfDB {
}

sub preProSprlDB {
}

sub readDefectData {
        my $defectDataFH=new FileHandle;
        open ($defectDataFH,$_[0]) or die "Error: Cannot load defectivity
data, $_[0]\n";

You are creating $defectDataFH as a FileHandle object so you should be using the open method that it provides:


$defectDataFH->open ... or die ...;

perldoc FileHandle


But you don't really need the FileHandle module as the version of Perl that you are using allows you to create lexically scoped filehandles:


open my $defectDataFH, $_[0] or die ...;


You should include the $! variable (C's errno/strerror) in your error message so you know why it failed to open. (Or perhaps $^E depending on your OS.)


perldoc perlvar

        print "Loading defect data ... ";
        my %short;
        while ($_=$defectDataFH->getline) {
                chomp;
                print "P8: $_\n";
                @_=split (/ /,$_);

Which is the long way of saying:

                split / /;

You shouldn't modify the @_ array as it has a special meaning inside of a subroutine.

perldoc perlsub


If you really want to use @_ you should localize its contents:

                local @_ = split / /;

Or better yet, use a locally scoped lexical variable:

                my @temp = split / /;

Your first argument to split() is a regular expression matching a single space character. If you are certain that all your input lines have fields that are separated by exactly one space then that will work as expected.

                print "P9: @_\n";
                if ($_[0]=~/SHORT/) {

If any of the input lines have leading space characters then 'SHORT' could be in $_[1] (or $_[2] or ?)


                  .... # performs some initializing kind of stuff and
returns a hash to the caller.

You can't return a hash from a subroutine, either a reference to a hash or a list.

perldoc perlsub

                }
}

sub readLayerMap {
}

sub execQuery {
}

sub calcWCAR {
}

sub updateNetPairsDB {
        my %nxfDB=&loadNxfDB
("\/tmp\/${topCellName}\/LVS\/svdb\/${topCellName}.nxf");

The '/' character is not special in a string and doesn't need to be escaped.

my %nxfDB = loadNxfDB( "/tmp/$topCellName/LVS/svdb/$topCellName.nxf" );

If you are uncomfortable with perl's string interpolation you could use concatenation instead:

my %nxfDB = loadNxfDB( '/tmp/' . $topCellName . '/LVS/svdb/' . $topCellName . '.nxf' );

        &preProSprlDB 
("\/tmp\/$topCellName\/SPRL\/sprl.dat","\/tmp\/$topCellName\/SPRL\/mod_sprl.dat");
        my %short=&readDefectData ("$BFE_INST_DIR\/decks\/$techNode.ymps");
        my %layerMap=&readLayerMap ("$BFE_INST_DIR\/decks\/$techNode.map");
        my $readBuff=new FileHandle;
        my $writeBuff=new FileHandle;
        my $queryLogFH=new FileHandle;
        my $sprlDBFH=new FileHandle;
        my $queryResponse;
        my %wcarDB;
        ........
         #lot of stuff goes on after this.
}

#Finally I call the main subroutine
&main ();
===================================================================


John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to