Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Is it there, or is it not?? Quirkiness with error handling.

by defyance (Curate)
on Jul 11, 2002 at 22:33 UTC ( [id://181161]=perlquestion: print w/replies, xml ) Need Help??

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

Quick Question...

I'm having issues. All I'm doing is opening a file, finding the data read by STDIN, and prints the found data to the terminal. If the data is not found, I want it to error out, and write STDIN to a log. This should be simple, but I'm obviously doing something wrong, and can't seem to find the err in my ways.. Please take a look at the below code:

The code does use strict and -w, it declares the var's using use vars qw($var);.

sub npa { #This purdy little thing searches the NPA-NXX given to STDIN #Then it prints the happy little output to the terminal. print "\n", " " x 31, "Look Up NPANXX Info\n"; print " " x 27, "=" x 30, "\n"; print " " x 30, "Please enter NPANXX:"; chomp( $cnip = <STDIN> ); if ( $cnip =~ /\d{6}/ ) { &npa1; } else { &err2; } sub npa1 { open( TECH, "</export/home2/knxvcs/knxvcs56/ttu/npalist" ) || die "Oops : $!"; while (<TECH>) { if ( $cnip / ) { (@npa) = split (/\:/); print "\n", " " x 14, "*" x 60; print "\n", " " x 24, "=" x 40, "\n"; print " " x 24, "NPANXX Line Switch MSR&CUSTGP VMX +\n"; print " " x 24, "$npa[0] $npa[1] $npa[2] $npa[3] $npa[4] $npa +[5]\n"; print " " x 24, "=" x 40, "\n\n"; &con; } else { &err3($cnip) } } close(TECH); #Close file } }
Then err3 sub will put the unfound data from STDIN to a log file like so:
sub err3 { #Woah man.. print "\n" x 2; print " " x 20, "Selection Not Found! Press <Enter> to Continue"; <>; open(ERRLOG, ">>/export/home2/knxvcs/knxvcs56/ttu/errlog") || die "Oops : $!"; print ERRLOG "$cnip\n"; close(ERRLOG); system("clear"); menu(); }

This should be so simple, but I don't know what the heck I'm doing wrong!! If it doesn't find any data, it processes what the else tells it to do, and move on happily, or thats the way I think it should go.. It seems that its not finding any of the data, because even if I put something in that I know is in the data file, it sends me to the &err3 sub, and puts the unfound data into the log file. If I comment out else { &err3($cnip) }, it finds the data that it should happily, and moves on. Like I said IT WORKS When I uncomment the else { &err3($cnip) } line, aside from not erroring out if data isn't found....Am I overlooking something simple? If so, go easy on me, I've had a bad day..

Any ideas??


perl -e '$a="3567"; $b=hex($a); printf("%2X\n",$a);'

edited: ~Sat Jul 13 16:10:32 2002 (GMT)
by footpad: Added <readmore> tag, per Consideration.

Replies are listed 'Best First'.
Re: Is it there, or is it not?? Quirkiness with error handling.
by Aristotle (Chancellor) on Jul 11, 2002 at 23:24 UTC

    Why did you nest the subroutine definition? That's very odd to read and will break if you add lexicals. Also, please do not call subroutines using an ampersand. Write err3() rather than &err3(). Anyway..

    If I understand your problem description correctly, you want err3() to only be called when none of your lines match? The way you have it, it will be called once for every time a line does not match. Is that what you want it to do? If not, the following would work:
    my $had_match = 0; #see? now the sub nesting breaks the code.. while (<TECH>) { if ( /$cnip/ ) { $had_match++; (@npa) = split (/\:/); print "\n", " " x 14, "*" x 60; print "\n", " " x 24, "=" x 40, "\n"; print " " x 24, "NPANXX Line Switch MSR&CUSTGP VMX +\n"; print " " x 24, "$npa[0] $npa[1] $npa[2] $npa[3] $npa[4] $npa +[5]\n"; print " " x 24, "=" x 40, "\n\n"; con(); } } close(TECH); #Close file err3($cnip) unless $had_match;

    Makeshifts last the longest.

      "... please do not call subroutines using an ampersand. Write err3() rather than &err3()."

      FYI: this is a special syntax. Calling a subroutine this way from within a subroutine has the effect of passing the (remaining) arguments to the inner sub that were passed to the outer. To wit:

      # main outer( 1, qw( A Set of Args )); sub outer { my $pass = shift; &inner if $pass; } sub inner { print "inner: ", join( ' ', @_ ), "\n"; }

      will print:

      A Set of Args

      dmm

      If you GIVE a man a fish you feed him for a day
      But,
      TEACH him to fish and you feed him for a lifetime

        Note that you are confusing &mysub; and &mysub(); which are very different. (tye)Re: A question of style goes into these in some detail.

        As for avoiding & on subroutine calls, that is, to a great extent, a style issue. Frankly, several of the reasons for using & on subroutine calls (avoid collisions with built-ins, stylistically distinguish user subs from built-ins, uniformity of sigils with other common user-defined items) are much more compelling to me than any of the reasons for not using them: "looks like Perl4" (big deal), "overrides prototypes" (don't use prototypes in most cases).

        So I strongly disagree with Aristotle on that point.

                - tye (but my friends call me "Tye")
      That did the trick!

      I got into a bad habit of using ampersands for every call to a sub when I first started learning Perl. I realize that its not always a good idea, especially in a case like this.

      I nested that for lack of knowing better, put simply. Thanks for the advice, sorry about the confusion in my writeup.

      And no, I didn't not want it to call everytime a line did not match. I was writing this in a hurry, and intern proved that patience pays off, cause had I been patient, I would not have had this problem!!

      /me loves learning new stuff everyday, just wish I could spend more time learning more Perl!

      --~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--

      perl -e '$a="3567"; $b=hex($a); printf("%2X\n",$a);'

        Something I just thought of.. these oodles of "some string" x $repetition look rather ugly. Maybe you would profit from learning about Perl's format support? It lets you write report generation routines in a visually oriented way.

        Makeshifts last the longest.

Re: Is it there, or is it not?? Quirkiness with error handling.
by particle (Vicar) on Jul 11, 2002 at 23:01 UTC
    if ( $cnip / ) {
    does what, exactly? ;-)

    ~Particle *accelerates*

      Honestly, I have NO idea what that does, its supposed to be if (/$cnip/) { Guess I got in too much of a hurry when pasting to see that.. LOL

      --~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--

      perl -e '$a="3567"; $b=hex($a); printf("%2X\n",$a);'

Re: Is it there, or is it not?? Quirkiness with error handling.
by Gerard (Pilgrim) on Jul 11, 2002 at 23:03 UTC
    Where is sub err2 ???
      Per your request:
      sub err2 { #You messed up again... print "\n" x 2; print " " x 14, "Valid Selection Not Entered! Press <Enter> to Continue"; <>; system("clear"); menu(); }

      and the other sub that gets called in this snippett in case you want to see it.

      sub con { #Gives the option to connect to different system print " " x 34, "Connect To $npa[2]?\n\n"; print " " x 20, "Press v for VM and s for Switch <Enter> To Ex +it:"; chomp( $read = <STDIN> ); if ( $read =~ m/^[sS]/ ) { system( "/usr/bin/wmc/talker", $npa[6] ); } if ( $read =~ m/^[vV]/ ) { system( "/usr/bin/telnet", $npa[7]) + } else { &menu } system("clear"); menu(); }

      --~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--~~--

      perl -e '$a="3567"; $b=hex($a); printf("%2X\n",$a);'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://181161]
Approved by dimmesdale
Front-paged by dimmesdale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found