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: Intermediate:
Problems with strict

 



Warren Bell
Deleted

Apr 2, 2001, 12:23 PM

Post #1 of 3 (410 views)
Problems with strict Can't Post

I'm trying to get this piece of code to print out my IP. If I take out the 'use strict' and 'my's it works fine. The way it is now it doesn't print out anything. Can someone tell me why the $ip is empty when using strict?

#!/usr/bin/perl

use strict;

my @network = `ifconfig ppp0`;

# get IP from @network
# --------------------

foreach my $line (@network) {
if ($line =~ /.*inet addr:(.*) P-t-P.*/) {
my $ip = $1;
}
}

print my $ip;
print "\n";

exit;




freddo
User

Apr 2, 2001, 3:12 PM

Post #2 of 3 (402 views)
Re: Problems with strict [In reply to] Can't Post

Hello Warren,

if you program works without "use strict", then i guess that the problem should come from this line:

print my $ip;

try to use:

print $ip;

"my" is used to declare a local variable to a block, once you have declared it, you dont need to write "my" each time., this should gives (not tested, i'm on w2k by now):

#!/usr/bin/perl
use strict;

my $ip;
my @network = `ifconfig ppp0`;

# get IP from @network
foreach my $line (@network) {
$ip = $1 if ($line =~ /.*inet addr:(.*) P-t-P.*/);
}

print "$ip\n";
exit;



Real Programmers use goto;


Warren Bell
Deleted

Apr 2, 2001, 3:27 PM

Post #3 of 3 (400 views)
Re: Problems with strict [In reply to] Can't Post

It wasn't the 'print my $ip' at bottom, it needed the 'my $ip' above, or outside of the foreach loop.

Thanks


 
 


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

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