Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default subroutine problem

    Hi all,

    I have recently started to learn perl. After reading Randal Schwartz’s
    Learning perl, I decided to give my first program a whirl.
    Upon writing it, I was checking each section of code as I went along to
    make sure everything worked.

    I got to one section and couldn’t get it to run as a subroutine. I don’t
    fully understand the error I am getting and am hoping somebody can point
    me in the right direction.

    Here is a snippet of my code (edited/reduced to about ¼ of the full
    size) which gives the same error as my full program.

    #usr/bin/perl -w

    #use strict;
    use warnings;
    use IO::Socket;

    print "1. network Listing only\n";
    print "2. Port scan only\n";

    print "\nPlease enter selection: ";
    chomp (my $selection = <STDIN>);

    if ($selection == 1) {
    #bla bla, don't need this info
    }
    elsif ($selection == 2) {
    print "\nEnter target: ";
    chomp(my $target = <STDIN>);
    print "Enter start port: ";
    chomp(my $port = <STDIN>);
    print "Enter end port: ";
    chomp(my $end_port = <STDIN>);
    &scan;
    }


    sub scan {

    print "Scanning $target | from port $port to $end_port\n\n";
    foreach (; $port<=$end_port; $port++) {
    if ( IO::Socket::INET->new(
    PeerAddr => $target,
    PeerPort => $port,
    Proto => 'tcp',
    Timeout => 1)) {
    print "Port $port is open\n";
    }
    }
    print "\nPort scan complete\n";
    exit;
    }


    Can anyone suggest why I am getting an error and how I can fix it?

    Thanks

    Ged.

    Gedi Guest

  2. Similar Questions and Discussions

    1. undefined subroutine
      I'm doing a php to pl migration and having a little trouble. Help appreciated! Undefined subroutine &main::fopen called at...
    2. how to trace a subroutine?
      I have a subroutine "foo" in a file "my.pl". I invoke it by: require "my.pl"; &foo; This has worked fine for a while. Now I try to make...
    3. reference to a subroutine in @INC
      Hi, I am trying to package a perl script and the modules it uses , in a tar file. When untarred on any machine, the modules can be found in a known...
    4. Subroutine as a new Task
      Hello, how can I start a soubroutine as a new task. The main script should run on until the end is reached. But the subroutine should run in...
    5. using subroutine from another file
      Let's say thatI wroute some code long time ago: code1.pl. In the file there is this subroutine1(); now I am writting another code code2.pl, and...
  3. #2

    Default Re: subroutine problem

    On Sep 26, Gedi said:
    >fully understand the error I am getting and am hoping somebody can point
    >me in the right direction.
    You should have shown us the error, so that we don't need to run the code.
    But as it stands, the code doesn't need to be run.
    >#use strict;
    Why'd you do that??
    >elsif ($selection == 2) {
    > print "\nEnter target: ";
    > chomp(my $target = <STDIN>);
    > print "Enter start port: ";
    > chomp(my $port = <STDIN>);
    > print "Enter end port: ";
    > chomp(my $end_port = <STDIN>);
    > &scan;
    >}
    The three variables $target, $port, and $end_port are all declared as
    lexical variables in that block. They can't be seen OUTSIDE the block.
    That means your function scan() can't see them. If you didn't call the
    function, but rather put the code of the function IN the elsif block,
    you'd be ok.

    You should pass the variables to the function.

    # ...
    scan($port, $end_port, $target);
    }

    sub scan {
    my ($from, $to, $where) = @_;
    print "Scanning $where | from port $from to $to\n\n";
    for my $p ($from .. $to) {
    print "Port $p is open\n" if IO::Socket::INET->new(
    PeerAddr => $where,
    PeerPort => $p,
    Proto => 'tcp',
    Timeout => 1,
    );
    }
    print "Port scan complete\n\n";
    }

    --
    Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

  4. #3

    Default Re: subroutine problem

    Gedi wrote:
    > Hi all,
    >
    > I have recently started to learn perl. After reading Randal Schwartz’s
    > Learning perl, I decided to give my first program a whirl.
    > Upon writing it, I was checking each section of code as I went along to
    > make sure everything worked.
    >
    > I got to one section and couldn’t get it to run as a subroutine. I don’t
    > fully understand the error I am getting and am hoping somebody can point
    > me in the right direction.
    >
    > Here is a snippet of my code (edited/reduced to about ¼ of the full
    > size) which gives the same error as my full program.
    What error? It helps to paste in the error message, as well as marking the
    line numbers it references.
    > #usr/bin/perl -w
    >
    > #use strict;
    Nuh-uh. You are not ready for help from others when you refuse it from your
    compiler. Strict compilation should ALWAYS be your first line of defense
    against logic errors.
    >
    > use warnings;
    > use IO::Socket;
    >
    > print "1. network Listing only\n";
    > print "2. Port scan only\n";
    >
    > print "\nPlease enter selection: ";
    > chomp (my $selection = <STDIN>);
    >
    > if ($selection == 1) {
    > #bla bla, don't need this info
    > }
    > elsif ($selection == 2) {
    > print "\nEnter target: ";
    > chomp(my $target = <STDIN>);
    > print "Enter start port: ";
    > chomp(my $port = <STDIN>);
    > print "Enter end port: ";
    > chomp(my $end_port = <STDIN>);
    > &scan;
    I don't know if this is the error line, but it is not good. Only use this
    when you are handing a function reference to a callback. Under normal
    circumstances, you should use scan();
    >
    > }
    >
    >
    > sub scan {
    >
    > print "Scanning $target | from port $port to $end_port\n\n";
    Where idid these variables come from. No such variables have been declared
    within either the scope of the function or the main namespace. I notice,
    though that you do declare and assign values to, then never use,
    similarly-named variables within an elsif block above. Did you mean to
    declare them in the main namespace, then assign them within the else block?
    > foreach (; $port<=$end_port; $port++) {
    > if ( IO::Socket::INET->new(
    > PeerAddr => $target,
    > PeerPort => $port,
    > Proto => 'tcp',
    > Timeout => 1)) {
    > print "Port $port is open\n";
    > }
    > }
    > print "\nPort scan complete\n";
    > exit;
    > }
    >
    >
    > Can anyone suggest why I am getting an error and how I can fix it?
    We can try, once you give us a place to start. I already had to do way to
    much of your work to determine where the errors arose. FWIW, without strict
    compilation, there were no errors.
    The messages you got were warnings, telling you that your code was probably
    not working [ie that variables were being used without being assigned a
    value.
    That is a good indication that you should turn strict back on to help find
    where the problems are coming from

    Joseph

    R. Joseph Newton Guest

  5. #4

    Default Re: subroutine problem

    Jeff / Joseph,

    Thanks for you replies. Appologies about commenting out the 'use strict'. I was playing about with the program and was checking the different errors I got with and without it. Didn't realised it was still commented when I posted.

    Its funny, but I spent about an hour trying to get this thing to work. When I woke up this morning, the answer (well idea of what it was) poped into my head. Jeff just confirmed this (and gave me a few pointers on how to go about it)

    I will include the error next time. (i'm sure there will be a next time :))

    Thanks again.
    >
    > From: "R. Joseph Newton" <rjnewton@efn.org>
    > Date: 2003/09/27 Sat AM 04:59:35 GMT
    > To: Gedi <gedi@ntlworld.com>
    > CC: [email]beginners@perl.org[/email]
    > Subject: Re: subroutine problem
    >
    > Gedi wrote:
    >
    > > Hi all,
    > >
    > > I have recently started to learn perl. After reading Randal Schwartz’s
    > > Learning perl, I decided to give my first program a whirl.
    > > Upon writing it, I was checking each section of code as I went along to
    > > make sure everything worked.
    > >
    > > I got to one section and couldn’t get it to run as a subroutine. I don’t
    > > fully understand the error I am getting and am hoping somebody can point
    > > me in the right direction.
    > >
    > > Here is a snippet of my code (edited/reduced to about ¼ of the full
    > > size) which gives the same error as my full program.
    >
    > What error? It helps to paste in the error message, as well as marking the
    > line numbers it references.
    >
    > > #usr/bin/perl -w
    > >
    > > #use strict;
    >
    > Nuh-uh. You are not ready for help from others when you refuse it from your
    > compiler. Strict compilation should ALWAYS be your first line of defense
    > against logic errors.
    >
    > >
    > > use warnings;
    > > use IO::Socket;
    > >
    > > print "1. network Listing only\n";
    > > print "2. Port scan only\n";
    > >
    > > print "\nPlease enter selection: ";
    > > chomp (my $selection = <STDIN>);
    > >
    > > if ($selection == 1) {
    > > #bla bla, don't need this info
    > > }
    > > elsif ($selection == 2) {
    > > print "\nEnter target: ";
    > > chomp(my $target = <STDIN>);
    > > print "Enter start port: ";
    > > chomp(my $port = <STDIN>);
    > > print "Enter end port: ";
    > > chomp(my $end_port = <STDIN>);
    > > &scan;
    >
    > I don't know if this is the error line, but it is not good. Only use this
    > when you are handing a function reference to a callback. Under normal
    > circumstances, you should use scan();
    >
    > >
    > > }
    > >
    > >
    > > sub scan {
    > >
    > > print "Scanning $target | from port $port to $end_port\n\n";
    >
    > Where idid these variables come from. No such variables have been declared
    > within either the scope of the function or the main namespace. I notice,
    > though that you do declare and assign values to, then never use,
    > similarly-named variables within an elsif block above. Did you mean to
    > declare them in the main namespace, then assign them within the else block?
    >
    > > foreach (; $port<=$end_port; $port++) {
    > > if ( IO::Socket::INET->new(
    > > PeerAddr => $target,
    > > PeerPort => $port,
    > > Proto => 'tcp',
    > > Timeout => 1)) {
    > > print "Port $port is open\n";
    > > }
    > > }
    > > print "\nPort scan complete\n";
    > > exit;
    > > }
    > >
    > >
    > > Can anyone suggest why I am getting an error and how I can fix it?
    >
    > We can try, once you give us a place to start. I already had to do way to
    > much of your work to determine where the errors arose. FWIW, without strict
    > compilation, there were no errors.
    > The messages you got were warnings, telling you that your code was probably
    > not working [ie that variables were being used without being assigned a
    > value.
    > That is a good indication that you should turn strict back on to help find
    > where the problems are coming from
    >
    > Joseph
    >
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    >
    >
    -----------------------------------------
    Email provided by [url]http://www.ntlhome.com/[/url]


    Ged Guest

  6. #5

    Default Re: subroutine problem

    the problem in if statement. do u need $section =2 value means

    if ( $section != 1) {
    // now execute other code here
    }
    vel Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139