Ask a Question related to PERL Beginners, Design and Development.
-
Gedi #1
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
-
undefined subroutine
I'm doing a php to pl migration and having a little trouble. Help appreciated! Undefined subroutine &main::fopen called at... -
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... -
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... -
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... -
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... -
Jeff 'Japhy' Pinyan #2
Re: subroutine problem
On Sep 26, Gedi said:
You should have shown us the error, so that we don't need to run the code.>fully understand the error I am getting and am hoping somebody can point
>me in the right direction.
But as it stands, the code doesn't need to be run.
Why'd you do that??>#use strict;
The three variables $target, $port, and $end_port are all declared as>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;
>}
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
-
R. Joseph Newton #3
Re: subroutine problem
Gedi wrote:
What error? It helps to paste in the error message, as well as marking the> 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.
line numbers it references.
Nuh-uh. You are not ready for help from others when you refuse it from your> #usr/bin/perl -w
>
> #use strict;
compiler. Strict compilation should ALWAYS be your first line of defense
against logic errors.
I don't know if this is the error line, but it is not good. Only use this>
> 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;
when you are handing a function reference to a callback. Under normal
circumstances, you should use scan();
Where idid these variables come from. No such variables have been declared>
> }
>
>
> sub scan {
>
> print "Scanning $target | from port $port to $end_port\n\n";
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?
We can try, once you give us a place to start. I already had to do way to> 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?
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
-
Ged #4
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
-
vel #5
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



Reply With Quote

