in reply to Re: beginner syntax question
in thread beginner syntax question

Everything dragonchild said holds true, let me append my .02 cents. This is how I would rewrite your script without altering too much.
#!/usr/bin/perl -w # IP ranger use strict; my ($stip,$edip,$count,@openip); open(IPFILE,"<iplist.txt") # open readonly || die "Could not open iplist.txt:$! \n"; while (<IPFILE>) { chomp; ($stip,$edip) = split(/:/,$_,2); # split(/:/); print "$stip\n"; print "$edip\n"; @openip = split(/\./,$stip); $count = @openip; print "$count\n"; # you can avoid the creation of the $count # variable and use the following statement # print scalar(@openip), "\n"; # for ($i = 0; $i <= 4; $i++) { # print "@srtip\n"; # }; }; close(IPFILE);

Update: semi-colon after open() was a poorly over looked error... fixed

<font face="verdana" size=1"> - yo tengo mas que aprender

Replies are listed 'Best First'.
Re3: beginner syntax question
by dragonchild (Archbishop) on Sep 19, 2001 at 18:09 UTC
    s0ttle has the rewrite correct. However, Perl idiom (and tradional coding practice) would dictate declaring a variable in tightest scope possible. Thus, I moved the my to within the while loop.
    #!/usr/bin/perl -w # IP ranger # If you're on 5.6.0 or higher, add the following two lines # and remove the -w #use 5.6.0; use strict; #use warnings; open(IPFILE,"<iplist.txt"); # open readonly || die "Could not open iplist.txt:$! \n"; while (<IPFILE>) { chomp; my ($stip,$edip) = split ':', $_, 2; # split(/:/); print "$stip\n"; print "$edip\n"; my @openip = split '.', $stip; # split(/\./,$stip); my $count = @openip; print "$count\n"; # you can avoid the creation of the $count # variable and use the following statement # print scalar(@openip), "\n"; # for ($i = 0; $i <= 4; $i++) { # print "@srtip\n"; # }; }; close(IPFILE);

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

or before &#124;&#124;, for &#124;&#124; comes before or
by TGI (Parson) on Sep 19, 2001 at 20:00 UTC

    Watch the precedence of your ors! Compare the following lines of code:

    my $foo = $bar || die "Bela Lugosi is dead: $!"; my $FOO = $BAR or die "I'm dead, I'm dead, I'm dead";

    s0ttle's code looks pretty good (but I haven't run it), and dragonchild's point about moving variable declarations to get the smallest scope possible is good too. But their use of  open() || die makes me nervous.

    Remember that || has a very high precedence and or has a low precedence. In the code at the top of my post, line 1 dies if $bar evaluates to false; line 2 dies if the assignment to $FOO fails.

    I've been burned by using the || die syntax before. I kept doing it after some kindly monk (I forget who) warned me not to, and doomed myself to spend much time debugging as a result.


    Like Cassandra, TGI says moo

      While I understand your point and agree to a certain extent, I have to say that there is nothing wrong with using || in the following code:
      open(FILE, "filename") || die "Can't open filename: $!";
      There is more than one way to deal with operator precedence. One way is to substitute the or operator as you suggested. But a perfectly acceptable alternative is to use parentheses to make the precedence explicit. That's what was done is this case. It may make you nervous, but it's still perfectly acceptable (and in fact easier for novices to understand).

      buckaduck

Re: Re: beginner syntax question
by tommyw (Hermit) on Sep 19, 2001 at 18:11 UTC

    open(IPFILE,"<iplist.txt"); # open readonly || die "Could not open iplist.txt:$! \n";
    will make the script fall over, due the extraneous semi-colon after the open call.

    @openip = split('.',$stip); # split(/\./,$stip); reproduces the original problem. That backslash needs to be there.

    What's the point of offering a rewritten version, if you're not even going to test it? Oh, XP whoring :-)

    I have to agree though, the use of strict, and moving chomp do make life easier in the long run.

    Just for the record, having fixed those two problems, I get:

    10.1.2.2
    10.1.2.200
    4