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

Hey all,

I have a prob. Please look at this error and tell me what I'm doing wrong, I keep getting:

Use of implicit split to @_ is deprecated at npa line 16. syntax error at npa line 22, near "else" Execution of npa aborted due to compilation errors.

I am VERY new, and I can't seem to get this down, probably something to do with the reg exp I'm trying to use. All I want it to do it spit out an error when a letter is inputed.

I want it to read 6 numbers, pull the numbers from the file, then spit it out. But if a letter is inputed, I want it to error out. If I take out the if & else, it works, but if I hit a letter it spits out all the data in the file, thats 216 lines, toooo much.

VERY MESSY CODE WARNING.

#!/usr/local/bin/perl -w $cnip = $ENV{'QUERY_STRING'}; print "\n Look Up NPANXX Info"; print "\n =============================="; print "\n Please enter NPANXX:_______\b\b\b\b\b\b\b +"; chomp($cnip=<STDIN>); if($cnip =~ /\d{6}/){ }; \@datb = `cat tech3 | grep $cnip`; \@cnip = $cnip; #printing the info foreach $datb (\@datb){ \@cnip = split (\/\:/, $datb); print "\n ========================================\n"; print " NPANXX Line Switch MSR&CUSTGP VMX"; print " $cnip $cnip[1] $cnip[2] $cnip[3] $cnip[4] $cnip +[5]"; print " ========================================\n\n"; else { print "\nValid NPANXX not entered!\n"; };



I know I'm probably gonna lose exp points for this one, but I really need help. Code is very ugly, but it spits out the data quite nicely when it works right!

Replies are listed 'Best First'.
Re: Checking Input using reg exp.
by grep (Monsignor) on Dec 28, 2001 at 06:57 UTC
    You have some brackets messed up. I have this compiling.
    You should really use CGI.pm for this. If you are looking for a quick but excellent CGI tutorial check out Ovid's page. Right at the top is a link to his CGI course.
    I also added a few quick comments.
    #!/usr/local/bin/perl -w $cnip = $ENV{'QUERY_STRING'}; print "\n Look Up NPANXX Info"; print "\n =============================="; print "\n Please enter NPANXX:_______\b\b\b\b\b\b\b +"; chomp($cnip=<STDIN>); if($cnip =~ /\d{6}/){ @datb = `cat tech3 | grep $cnip`; #You could do this a a file open + and grep the contents... it would be more portable and perlyer :) @cnip = $cnip; #printing the info foreach $datb (@datb){ @cnip = split (\/\:/, $datb); print "\n ===================================== +===\n"; print " NPANXX Line Switch MSR&CUSTGP VMX"; print " $cnip $cnip[1] $cnip[2] $cnip[3] $cnip[4] $cnip +[5]"; print " ======================================= +=\n\n"; } @datb = `cat tech3 | grep $cnip`; @cnip = $cnip; #printing the info foreach $datb (@datb){ @cnip = split (\/\:/, $datb); print "\n ===================================== +===\n"; print " NPANXX Line Switch MSR&CUSTGP VMX"; print " $cnip $cnip[1] $cnip[2] $cnip[3] $cnip[4] $cnip +[5]"; print " ======================================= +=\n\n"; } } else { ### you has this left bracket way up under your 'if' print "\nValid NPANXX not entered!\n"; }
    HTH

    grep
    grep> cd pub grep> more beer
      If you look closely, you'll see this isn't CGI code... just VERY dirty shell code. He probably got you fooled by that $cnip = $ENV{'QUERY_STRING'}; line. Note that he's not printing any HTTP headers (which is kinda trivial for CGI scripts). I can only assume that there is actually an environment variable called QUERY_STRING. A ++ for your comments on perlier code and your fixes on his referencing attempt tho :-)

      Greetz
      Beatnik
      ... Quidquid perl dictum sit, altum viditur.
      Cool, I got it figured out. Its working great now. Now to make it look a bit perlyer, he he. I've been working with C and Shell progs most of my natural programming life, he he. Here is the working code:

      #!/usr/local/bin/perl $cnip = $ENV{'QUERY_STRING'}; print "\n Look Up NPANXX Info"; print "\n =============================="; print "\n Please enter NPANXX:_______\b\b\b\b\b\b\b +"; chomp($cnip=<STDIN>); if($cnip =~ /\d{6}/){ &npa1 } else{ print "\n Valid NPANXX not entered!\n"; }; sub npa1 { @datb = `cat tech3 | grep $cnip`; @cnip = $cnip; #printing the info foreach $datb (@datb){ @cnip = split (/\:/, $datb); print "\n ========================================\n"; print " NPANXX Line Switch MSR&CUSTGP VMX"; print " $cnip $cnip[1] $cnip[2] $cnip[3] $cnip[4] $cnip +[5]"; print " ========================================\n\n"; }; };



      I'll make it prettier and stuff, TEAR IT APPART PLEASE!!
        Perltidy will help you clean it up and "make it prettier".

        If things get any worse, I'll have to ask you to stop helping me.

        You don't need to escape the ':' in the regex.
        @cnip = split /:/, $datb
        will do fine.

        Also using cat & grep seems a bit messy, what about:

        use File::Slurp; @datb = grep { /$cnip/ } read_file('tech3');
Re: Checking Input using reg exp.
by defyance (Curate) on Dec 28, 2001 at 06:55 UTC
    Okay, I figure out what I need to do, use a subroutine! Now I just gotta figure out this damn syntax error, any ideas on that, am I overlooking something simple?
Re: Checking Input using reg exp.
by Beatnik (Parson) on Dec 28, 2001 at 18:18 UTC
    #!/usr/local/bin/perl use strict; #$cnip = $ENV{'QUERY_STRING'}; #Uhm where is this coming from??? print "\n"," "x22,"Look Up NPANXX Info\n"; print " "x19,"="x30,"\n"; print " "x19,"Please enter NPANXX:"; my $cnip; chomp($cnip=<STDIN>); if($cnip =~ /\d{6}/){ &npa1 } else{ print "\n"," "x21,"Valid NPANXX not entered!\n"; } sub npa1 { open(TECH,"<tech3") || die "Oops : $!"; while(<TECH>) { if (/$cnip/) { my (@cnip) = split(/\:/); print "\n"," "x16,"="x40,"\n"; print " "x16,"NPANXX Line Switch MSR&CUSTGP VMX"; print " "x16,"$cnip $cnip[1] $cnip[2] $cnip[3] $cnip[4] $c +nip[5]"; print " "x16,"="x40,"\n\n"; } } close(TECH); }
    Just a guess...

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
      See, I like that, Thats much nicer. By the way, I forgot to take out the QUERY_STRING statement, as this started out as a CGI script, but I changed my mind. OOPS, he he. Open insted of cat(DOH!!) Shoulda thought about that. I'm new to perl, used to C and Shell programming. Thats awesome guys, thanks for the input.