Showing results for 
Search instead for 
Do you mean 

perl query

SOLVED
Go to Solution
Honored Contributor Honored Contributor

perl query

Hi everyone

I am trying to pass the the following into a sub routine

&insert_audit("$hostid successfully $hostname");

the subroutine is used to pass the data into a MySQL database via perl::DBD module. I appear to have a problem though when trying to create an array from the pass to the sub routine

sub insert_audit ()

{

my (@record)=shift (@_);
print "@record\n";
}

The above is ok, ie (1 line of output below)

48 successfully rs-demo8

If I try to print an element of the array. The statement is as follows

sub insert_audit ()

{

my (@record)=(@_);
print "@record\n";
print "$record[1]";

}

Use of uninitialized value in join or string at configUpload.pl line 49

Which suggests the attempt to create the array from the parameters passed to the sub routine fails ?

Am I doing something wrong ?

TIA

Steve
take your time and think things through
5 REPLIES
Acclaimed Contributor Acclaimed Contributor

Re: perl query

Hi Steve:

Yes, one or more of the elements is the array passed is undefined. Consider:

#!/usr/bin/perl
use strict;
use warnings;

sub insert_audit
{
my (@record)=(@_);
print "@record\n";
print "$record[1]\n";
}

insert_audit ("a", "b" , "c");
print "------\n";
insert_audit ("a", undef, "c");
1;

If you run that you will see:

a b c
b
------
Use of uninitialized value in join or string at /tmp/subs line 8.
a c
Use of uninitialized value in concatenation (.) or string at /tmp/subs line 9.

Regards!

...JRF...
Honored Contributor Honored Contributor

Re: perl query

Hi

I understand your description and why I would see this error. I can't however see why in this example I get the error. When I print the array as a whole

print "@record\n";

I get each element that I have passed, ie

&insert_audit("$hostid successfully $hostname");

gives

49 successfully rs-dev

Am I being a little thick here ?

TIA

Steve
take your time and think things through
Honored Contributor Honored Contributor

Re: perl query

Interesting

my $sum=@record;
print "$sum\n";

1

Steve
take your time and think things through
Acclaimed Contributor Acclaimed Contributor

Re: perl query

Hi Steve:

OK, you passed a scalar, not an array or list when you said:

# insert_audit("$hostid successfully $hostname");

You should do:

# insert_audit (($hostid, "successfully",$hostname));

Thus:

#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;

sub insert_audit
{
my (@record)=(@_);
print "@record\n";
print "$record[1]\n";
}

my $hostid = 48;
my $hostname = "myhost";
insert_audit ( ($hostid, "successfully", $hostname) );
1;

...yields:

48 successfully myhost
successfully

Regards!

...JRF...
Honored Contributor Honored Contributor

Re: perl query

Bargain !!

I get it,

Thanks James

In the meantime I came to this

my ($hostid, $exit, $hostname);
my ($record)=(@_);
($hostid, $exit, $hostname)=split(/ /, $record);

Will do it your way as that is correct

Thanks again

Steve
take your time and think things through