Professional Web Applications Themes

subroutine problem - PERL Beginners

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 ...

  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. #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]japhypobox.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

  3. #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
    cirstances, 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

  4. #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" <rjnewtonefn.org>
    > Date: 2003/09/27 Sat AM 04:59:35 GMT
    > To: Gedi <gedintlworld.com>
    > CC: [email]beginnersperl.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
    > cirstances, 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-unsubscribeperl.org[/email]
    > For additional commands, e-mail: [email]beginners-helpperl.org[/email]
    >
    >
    -----------------------------------------
    Email provided by [url]http://www.ntlhome.com/[/url]


    Ged Guest

  5. #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

Similar Threads

  1. Subroutine interaction
    By Jan Eden in forum PERL Beginners
    Replies: 8
    Last Post: January 29th, 03:02 PM
  2. Who called subroutine.
    By John Hennessy in forum PERL Beginners
    Replies: 1
    Last Post: December 11th, 10:24 AM
  3. how to trace a subroutine?
    By Wild Pete in forum PERL Modules
    Replies: 2
    Last Post: December 2nd, 02:15 AM
  4. reference to a subroutine in INC
    By Raj Karadakal in forum PERL Beginners
    Replies: 8
    Last Post: November 11th, 11:24 PM
  5. Subroutine as a new Task
    By Jochen Friedmann in forum PERL Miscellaneous
    Replies: 1
    Last Post: September 15th, 08:59 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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