CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Subroutine issue

 



popeye
Novice

Dec 18, 2013, 12:11 PM

Post #1 of 4 (1167 views)
Subroutine issue Can't Post

Im not sure why the following code works when out of the sub but doesnt when wrapped in the sub ...


Code
#!/usr/bin/perl 
#sub mydoerr {

undef $w;
#undef $x;
$x=xxx;
undef $y;
undef $z;

if ((defined $w) || (defined $x) || (defined $y) || (defined $z)) {
print "at least one variable is defined\n";
} else {
print "None of the variables are defined.\n";
}

#}

#sub mydoerr;


The output when the statements are not defined as a sub is :


Quote
[popeye]$ dout
at least one variable is defined
[popeye]$


When sub is uncommented :



Quote
[popeye]$ dout
[popeye]$



Kenosis
User

Dec 18, 2013, 12:22 PM

Post #2 of 4 (1160 views)
Re: [popeye] Subroutine issue [In reply to] Can't Post

Always:

Code
use strict; 
use warnings;


I think you meant:

Code
$x='xxx';


I also think you meant:

Code
mydoerr();


Using lexical variables:

Code
#!/usr/bin/perl  
sub mydoerr {
my ( $w, $x, $y, $z );

$x = 'xxx';

if ( ( defined $w ) || ( defined $x ) || ( defined $y ) || ( defined $z ) )
{
print "at least one variable is defined\n";
}
else {
print "None of the variables are defined.\n";
}

}

mydoerr();



(This post was edited by Kenosis on Dec 18, 2013, 12:25 PM)


Zhris
Enthusiast

Dec 18, 2013, 12:23 PM

Post #3 of 4 (1159 views)
Re: [popeye] Subroutine issue [In reply to] Can't Post

Hi,

The final line of your code looks as though its meant to call the subroutine. In which case the correct syntax would be:


Code
sub mydoerr; 
mydoerr( );


Chris


popeye
Novice

Dec 18, 2013, 12:32 PM

Post #4 of 4 (1151 views)
Re: [Zhris] Subroutine issue [In reply to] Can't Post

 

Thank you everyone ... sheez .... terrrible eyes ! :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives