codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Passing value through function then print... GONE WRONG!!  bl4kh4k at 01:32 on Monday, September 01, 2008
 

I was on perlguru.com and nobody can seem to figure this out... or at least thats the assumption I am under. Basically I want to pass the ipAddress value through the function and then print it but everytime it prints a outputs a HASH(0x800d80) value of some sorts. I'll provide both files so that if someone wants to look through it. I've been at it for days and can't figure it out at all. Here they are... any help would be much appreciated...

Subnetter.pl


#!/usr/bin/perl -w

use strict;
use warnings;
use diagnostics;
use Process;

my $process = eval { new Process(); } or die ($@);

# Print introduction of script.
$process->PrintIntro();

print "Enter the IP address to be subnetted.\n";
my $ipAddress = <STDIN>;
chomp ($ipAddress);

if ($ipAddress =~ m/^(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)$/)
{
print "hi";
}
else
{
print "bye";
}

print "... to how many subnets?\n";
my $subnetNum = <STDIN>;
chomp ($subnetNum);
print "Information based on criteria:\n";

# Subnet inputted data.
$process->Subnet($ipAddress);

my $classA = 127;
my $classB = 191;
my $classC = 223;
my $classD = 239;
my $classE = 255;

# Print end of script.
$process->PrintEnd();



----------------------------

Process.pm


package Process;
use strict;

sub new
{
my $self = {};
$self->{_Subnet} = @_;
$self->{_PrintIntro} = undef;
$self->{_PrintEnd} = undef;
bless($self);
return $self;
}

sub Subnet
{
my ($ipAddress) = @_;
print $ipAddress;
# - 8392064;
}

sub PrintIntro
{
print "\n";
print "----------------------------------------------------------------------";
print "\n";
print "Script: Subnetter";
print "\n";
print "Description: This script will provide subnetting information\n";
print " based on the information provided.";
print "\n";
print "----------------------------------------------------------------------";
print "\n\n";
print "$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%";
print "\n\n";

my $self = shift;
if (@_) { $self->{PRINT} = shift }
return $self->{PRINT};
}

sub PrintEnd
{
print "\n\n";
print "$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$%$ %$%$%$%$%$%";
print "\n\n";

my $self = shift;
if (@_) { $self->{PRINT} = shift }
return $self->{PRINT};
}

1;


  Re: Passing value through function then print... GONE WRONG!!  hermanningjaldsson at 14:27 on Tuesday, September 02, 2008
 

#instead of going:
print STDOUT $somevar;

#try going
print STDOUT %$somevar;

#or
print STDOUT %{$somevar};

#or
print STDOUT %{$somevar[0]};

#the content of the variable is just a reference, u want to get to where it refers
#and not just print out the address.




  Re: Passing value through function then print... GONE WRONG!!  bl4kh4k at 18:22 on Tuesday, September 02, 2008
 

I tried each of those examples. i understand now why... as its just printing the reference to the variable. But still no luck. I even went to debug a big by throwing this at the top of my script

## Debug ###################
use Carp qw( confess );
$SIG{__DIE__} = \&confess;
$SIG{__WARN__} = \&confess;
############################

I saw that reference and the value that belonged to that reference. It said that the variable is not initialized. I'm assuming the examples you were giving me are to initialize the variable and pull the value out of the reference... but I don't know... it didn't work... heres where i call it... and the function that is being called... any more thoughts? A good example would be helpful to.. I've been googling a lot but I can't seem to find anything that focuses on this issue.

$process->Subnet($ipAddress);

sub Subnet
{
my ($ipAddress) = @_;
print STDOUT "$ipAddress";
}

Also, the line my ($ipAddress) = @_..... what is the difference between @_ and specifying undef?

  Re: Passing value through function then print... GONE WRONG!!  hermanningjaldsson at 18:42 on Tuesday, September 02, 2008
 

#@_ automatically contains whatever is sent to current function.
#@_ is a list, but u send in a scalar.
#
#so change Subnet function to this.
#might work.

sub Subnet
{
my $ipAddress = shift;
print STDOUT "$ipAddress";
}

  Re: Passing value through function then print... GONE WRONG!!  bl4kh4k at 18:49 on Tuesday, September 02, 2008
 

Still only prints the reference.

  Re: Passing value through function then print... GONE WRONG!!  hermanningjaldsson at 19:04 on Tuesday, September 02, 2008
 

#okey now i was getting bothered with this so i actually installed that program and #had it run.

#and yes i got the same error as u.

#what u need is a debugger man, that way u can watch exactly whats in each variable at #each time. with the debugger the problem is spotted in one sec, the best debugger for #perl is ptkdb.

#anyways, the thing is that what u want is not in @_ at the first place, its at the #second.

#so u do:

sub Subnet
{
my $ipAddress = $_[1];
print STDOUT "$ipAddress";
}



  Re: Passing value through function then print... GONE WRONG!!  bl4kh4k at 19:07 on Tuesday, September 02, 2008
 

When I first started trying perl I was looking for some type of editor to use... couldn't really find one for a mac so I gave up and started doing it the oldfashion way. Turns out thats not always a good idea. I'm going to go get the debugger and thank you so much. I can finally move on with my program ^_^ Thanks a million!!!

  Re: Passing value through function then print... GONE WRONG!!  bl4kh4k at 19:11 on Tuesday, September 02, 2008
 

I'm assuming that its the second because the reference is passed through the first?? Is this right?

  Re: Passing value through function then print... GONE WRONG!!  hermanningjaldsson at 19:20 on Tuesday, September 02, 2008
 

sub new
{
my $self = {};
$self->{_Subnet} = @_;
$self->{_PrintIntro} = undef;
$self->{_PrintEnd} = undef;
bless($self);
return $self;
}


function Subnet is called through an object called $process
the process is created with the new function (see above).

whats in the first argument when sending to a function is that
"PrintIntro" and "PrintEnd" thing.

so yeah, theres a reference in the first.





  Re: Passing value through function then print... GONE WRONG!!  S_Flex at 12:06 on Sunday, September 07, 2008
 

I have no idea what your trying to do with that script.
From looking at it, its most likely not the way to do what you need.

To get a IP Address you would want to use the %ENV hash which is part or Perl's Special Variables.

To make a proper OO in Perl Please read Perl objects

If you need more help try this link too Learn Perl








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Very slow inserts using SqlCommand.ExecuteNonQuery()
•  Re: Problem 1 - Email ids conversion code
•  Re: vector
•  combined 3 functions that do the same thing
•  Re: Finding data in hash
•  Re: login logout ...
•  Visual Basic
•  Re: dynamic crystal report generation
•  sorting 2 arrays


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2008