Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: My first perl script is working, what did I do wrong?

by marquezc329 (Scribe)
on Nov 08, 2012 at 22:20 UTC ( #1003005=note: print w/ replies, xml ) Need Help??


in reply to My first perl script is working, what did I do wrong?

Hello killersquirel11. Looks like you've already been given some full rewrites. I just figured I'd give you some smaller points to think about in the future.

if() vs unless() i.e.
if ($#ARGV != 0) -> unless ($#ARGV == 0)
if (not -e $inputFile) -> unless (-e $inputFile)

Multiple Variable Declaration can be broken down from:

my $headerLine= -1; my $prtIndex = -1; my $shpIndex = -1; my $clrIndex = -1; my $sizIndex = -1;
to:

my ($headerLine, $prtIndex, $shpIndex, $clrIndex, $sizIndex) = -1
 my ($headerLine, $prtIndex, $shpIndex, $clrIndex, $sizIndex) = (-1) x 5;

C style for loops like:

sub checkIndices { my $count; for ($count=0; $count<=$#_; $count++){ if($_[$count] < 0){ return (1==0); } } return (1==1); }
Can be "Perl-ified" like:
sub checkIndices { for my $item (@_){ return (1==0) if ($item < 0) } return (1==1); }


Comment on Re: My first perl script is working, what did I do wrong?
Select or Download Code
Re^2: My first perl script is working, what did I do wrong?
by rjt (Deacon) on Nov 08, 2012 at 22:55 UTC

    Multiple Variable Declaration can be broken down from:

    my $headerLine= -1; my $prtIndex = -1; my $shpIndex = -1; my $clrIndex = -1; my $sizIndex = -1;

    to:

    my ($headerLine, $prtIndex, $shpIndex, $clrIndex, $sizIndex) = -1;

    No, this won't do what you expect. Given:

      my ($a, $b, $c) = -1;

    only $a will receive the value ($b and $c will be undef). You can, however, use:

      my ($a, $b, $c) = (-1, -1, -1);

    That being said, I've found more often than not, when I have to start assigning the same value to a bunch of variables, there's probably a deeper design decision I need to question.

    In this case, -1 is used as some sort of magic value to indicate the state of something. Although even better refactoring can be done in this case, at the very least leaving the variables as undef provides better information, especially if any of those variables might legitimately contain -1 in the future.

      Thanks for the replies. In case you couldn't tell, I typically program in C (and sometimes Java, although really when I use Java I treat it a lot like C). I will definitely keep these pointers in mind as I continue to learn how to hack together Perl scripts.
        ... and apparently it didn't post the above as me. Whatever.

      Thank you for your correction rjt. Crackers2 also notified me of this mistake. I edited my response to reflect an alternate method of accomplishing this.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2014-08-01 10:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (1 votes), past polls