how do I pass user-defined values to an object's method ?

E

eatmoreoats

The general question

In general how do I pass into an object's method a variable set of
values ?


The actual scenario

There is an object that I am using that has a method, called graph(),
which can take multiple files to chart out :

...
$rrd->graph(
image => $image_file_name,
draw => {
file => "file1.rrd",
legend => "First Source"
},
draw => {
file => "file2.rrd",
legend => "Second Source"
}
);
...

The result of this, by using draw twice, is that both file1.rrd and
file2.rrd are charted by graph().

I want to build up a list of files dynamically and pass that list into
the graph method but am struggling to do this .
IE what goes in [>>> here <<<]

$rrd->graph(
image => $image_file_name,
[ >>>>> multiple draw's , one each for each of the multiple user
defined files to chart <<<<<]
);

I want the user to define the list of files to be charted and pass them
to chart() method. I'm no expert in OO perl clearly. I've tried a
number of different ways of doing this such as passing arrays of hashes
by reference, or not, etc etc but to no avail and usually ending up
with a 'Reference found where even-sized list expected' error.

Does this make any sense ?

Any help or pointers would be fantastic.

For more info : see RRDTool::OO document
http://search.cpan.org/~mschilli/RRDTool-OO-0.13/lib/RRDTool/OO.pm

Thanks very much
- Dom
 
A

A. Sinan Unur

The general question

In general how do I pass into an object's method a variable set of
values ?


The actual scenario

There is an object that I am using that has a method, called graph(),
which can take multiple files to chart out :

...
$rrd->graph(
image => $image_file_name,
draw => {
file => "file1.rrd",
legend => "First Source"
},
draw => {
file => "file2.rrd",
legend => "Second Source"
}
);
...

The result of this, by using draw twice, is that both file1.rrd and
file2.rrd are charted by graph().

....

I'm no expert in OO perl clearly.

This is not really an OO question. You might want to consult

perldoc perlreftut

You want to construct an array containing:

'draw', anonymous hash ref, 'draw', anonymous hash ref ...

my @from_user = (
{ file => 'file1.rrd', legend => 'First Source' },
{ file => 'file2.rrd', legend => 'Second Source' },
);

$rrd->graph(
image => $image_file_name,
map { draw => $_ } @from_user,
);

Sinan
 
C

Ch Lamprecht

You want to construct an array containing:

'draw', anonymous hash ref, 'draw', anonymous hash ref ...

my @from_user = (
{ file => 'file1.rrd', legend => 'First Source' },
{ file => 'file2.rrd', legend => 'Second Source' },
);

$rrd->graph(
image => $image_file_name,
map { draw => $_ } @from_user,

map {+ draw => $_} @from_user

Hi,
this line didn't compile without the '+'. (Makes it a block ... )
Thanks anyway - I didn't know this before.

Christoph
 
A

A. Sinan Unur

A. Sinan Unur wrote: ....

map {+ draw => $_} @from_user

Hi,
this line didn't compile without the '+'. (Makes it a block ... )

Sorry. I was typing straight into the newsreader. Thanks for catching
that.

Sinan
 
E

eatmoreoats

Thanks for the response.

I tried the method you described above, including the correction to
map. Here is the code now :

....
my @from_user = (
{ file => 'file1.rrd', legend => 'First Source' },
{ file => 'file2.rrd', legend => 'Second Source' },
);

$rrd->graph(
image => $image_file_name,
map {+ draw => $_ } @from_user,
);
....

When running, I get this problem :

draw => HASH(0x96afcf0)draw => HASH(0x99104dc)Illegal parameter '1' in
graph() at ./grapher.pl line (some line #)

Any ideas why ?

Also - dumb question but how do I push another anonymous hash into
@from_user, for say, file3.rrd ?

Thanks again for helping - really appreciate it.
-dom
 
R

robic0

Thanks for the response.

I tried the method you described above, including the correction to
map. Here is the code now :

...
my @from_user = (
{ file => 'file1.rrd', legend => 'First Source' },
{ file => 'file2.rrd', legend => 'Second Source' },
);

$rrd->graph(
image => $image_file_name,
map {+ draw => $_ } @from_user,
);
...

When running, I get this problem :

draw => HASH(0x96afcf0)draw => HASH(0x99104dc)Illegal parameter '1' in
graph() at ./grapher.pl line (some line #)

Any ideas why ?

Also - dumb question but how do I push another anonymous hash into
@from_user, for say, file3.rrd ?

Thanks again for helping - really appreciate it.
-dom

Hey eatmorecrap, please quote in your reply. Its almost impossible to
discern meaning from an apparent conversation. This aint VOIP !!!
 
A

A. Sinan Unur

Thanks for the response.

I tried the method you described above, including the correction to
map. Here is the code now :

...
my @from_user = (
{ file => 'file1.rrd', legend => 'First Source' },
{ file => 'file2.rrd', legend => 'Second Source' },
);

$rrd->graph(
image => $image_file_name,
map {+ draw => $_ } @from_user,
);
...

When running, I get this problem :

draw => HASH(0x96afcf0)draw => HASH(0x99104dc)Illegal parameter '1' in
graph() at ./grapher.pl line (some line #)

Any ideas why ?

I am a little puzzled by that. I do not see any code in the source code
for that module that would have emitted draw => HASH(0x96afcf0)draw =>
HASH(0x99104dc). It would be useful if you posted a short but complete
script instead of snippets.
Also - dumb question but how do I push another anonymous hash into
@from_user, for say, file3.rrd ?

That sounds like another SAQ (self-answering question):

perldoc -f push

Sinan
 
R

robic0

Thanks for the response.

I tried the method you described above, including the correction to
map. Here is the code now :

...
my @from_user = (
{ file => 'file1.rrd', legend => 'First Source' },
{ file => 'file2.rrd', legend => 'Second Source' },
);

$rrd->graph(
image => $image_file_name,
map {+ draw => $_ } @from_user,
Why do you play such a dangerous game in your function call with
map? You need to "pull" all those shortcuts "out" into the open
ahead of time and prove them there, ahead of time, before you can do them inside
calls. Then move them one by one inside/and or make shortcuts.
Don't pile drive the Perl stack and expect it to work righ away.
Its ludicruis to even attempt such a thing. Utterly absurd!!!
There is not time saving involved. Use Benchmark if your not sure.
Bizzar ....
 
E

eatmoreoats

Here is the complete source code :

#!/usr/bin/perl -w
$|=1;

use RRDTool::OO;
my $rrd = RRDTool::OO->new( file =>
"/usr/local/nagios/rrds/mon-1100.ram.rrd" );

$image_file_name = "./m.png";

my @from_user = (
{ file => '/usr/local/nagios/rrds/mon-1100.ram.rrd', legend =>
'First Source' },
{ file => '/usr/local/nagios/rrds/utl-1100.ram.rrd', legend =>
'Second Source' }
);

$rrd->graph(
image => $image_file_name,
map {+ print "draw => $_" } @from_user
);

Here is the output when run :

draw => HASH(0x87afcf0)draw => HASH(0x89c71e8)Illegal parameter '1' in
graph() at ./grapher.pl line 16

Rob if you can suggest a correct way of coding this, please post a
constructive reply.
Thanks again

-d
 
A

A. Sinan Unur

Here is the complete source code :

#!/usr/bin/perl -w
$|=1;

use strict;

missing.
use RRDTool::OO;
my $rrd = RRDTool::OO->new( file =>
"/usr/local/nagios/rrds/mon-1100.ram.rrd" );

$image_file_name = "./m.png";

my @from_user = (
{ file => '/usr/local/nagios/rrds/mon-1100.ram.rrd', legend =>
'First Source' },
{ file => '/usr/local/nagios/rrds/utl-1100.ram.rrd', legend =>
'Second Source' }
);

$rrd->graph(
image => $image_file_name,
map {+ print "draw => $_" } @from_user

What is that print doing in there?

Why are there quotation marks surrounding draw => $_? You have just
created a string instead of the key => anonymous hash ref that you need
to pass. The '1' in the error message is the return value of the print.

I am really baffled. Do you have to introduce random crap into the
suggestion I posted, and then lie about it in your follow-up post?

If you are referring to robic0, well, you might want to stay away from
that unbalanced person. On the other hand, after you wasted my time like
this, I feel like you guys deserve each other.

Bye.

Sinan
 
R

robic0

Here is the complete source code :

#!/usr/bin/perl -w
$|=1;

use RRDTool::OO;
my $rrd = RRDTool::OO->new( file =>
"/usr/local/nagios/rrds/mon-1100.ram.rrd" );

$image_file_name = "./m.png";

my @from_user = (
{ file => '/usr/local/nagios/rrds/mon-1100.ram.rrd', legend =>
'First Source' },
{ file => '/usr/local/nagios/rrds/utl-1100.ram.rrd', legend =>
'Second Source' }
);

$rrd->graph(
image => $image_file_name,
map {+ print "draw => $_" } @from_user
);
emo (good for your heart!!) --
@from_user is an array of anon references. Is that what is intended in the map?
Pull the map out of that function call and debug that all by itself.
Make sure its doing what you want. Your the immediate expert here on the map syntax.
Verify its usage. Otherwise I have to get out my sliderule and I'm oh so tired...
 
R

robic0

use strict;

missing.


What is that print doing in there?

Why are there quotation marks surrounding draw => $_? You have just
created a string instead of the key => anonymous hash ref that you need
to pass. The '1' in the error message is the return value of the print.

I am really baffled. Do you have to introduce random crap into the
suggestion I posted, and then lie about it in your follow-up post?


If you are referring to robic0, well, you might want to stay away from
that unbalanced person.
Yes he's right, my name is Rob. He might know me. And if he doesen't,
your telling him that your bullshit help hinges his "reference" to me
as a respondent? Hahahaaaaa, I've done my job in this group...
 
E

eatmoreoats

Sinan, Chris,

opps that was my mistake - that was from earlier
testing/experimentation. doh!

yup - all seems to work - thanks very much for helping everyone.

-d
 
R

robic0

Sinan, Chris,

opps that was my mistake - that was from earlier
testing/experimentation. doh!

yup - all seems to work - thanks very much for helping everyone.

-d
Sinan is the apparent map expert. Don't worry they won't they won't you,
you don't know me and have never sold me drugs right?

Formally admit that now, and never ever, acknowledge any follow up I may
reply to your posts, even with a Rob. Because, something is wrong with this
forum. See, you don't know me, but I replied with a sensible aproach.
Since you had'nt figured out your dilema (haden't u?) you would cry for help
in that regard. I replied, you responded to multiple posts. I think you tried
Sinans method, but it didn't work (some mistake you made?) so you grabbed for
anything you could (to save your job?). Now you try to make restitution.

I wish you lick (I mean luck). All you have to do is state you will put me
on your exclusion list and all is well with Sinan. Either that or publicall state
to me personally I am something that will separate you from me in the mind of Sinan.

To do that you will have to publically state, in short form, you wish not to
get responses on any of your questions, by myself. It might help if you also state
that you are putting me on your exclusion list in your reader. Any posts by me do
not show up on your reader (whats that called?)

Then you will be the "one" and only who has done that. I mean its been done,
but undone faster than a bear shits in the woods.

Sinan is a regular responder. And you shouldn't get on his bad side. I am a regular
developer and couldn't give a rats ass about such a puny mind.

Thems the facts ma'm, whish you luck.
Oh and btw, thanks for the lunch last Thurs!
 
T

Tad McClellan

eatmoreoats said:
map {+ print "draw => $_" } @from_user
^^^^^ ^ ^
^^^^^ ^ ^

Why did you add the print() there?

If you modify the code you were given, it is likely to
execute differently.

You are passing a bunch of ones (print's return value) to
your function (method?).

Here is the output when run :

draw => HASH(0x87afcf0)draw => HASH(0x89c71e8)


That does exactly what you told it to do.

Try telling it to do something else (like using the code you were given).
 
E

eatmoreoats

Tad said:
^^^^^ ^ ^
^^^^^ ^ ^

Why did you add the print() there?

If you modify the code you were given, it is likely to
execute differently.

You are passing a bunch of ones (print's return value) to
your function (method?).




That does exactly what you told it to do.

Try telling it to do something else (like using the code you were given).


Like I said above, it was a mistake.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top