Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how do I pass user-defined values to an object's method ?

Reply
Thread Tools

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

 
 
eatmoreoats
Guest
Posts: n/a
 
      02-21-2006
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/RRD.../RRDTool/OO.pm

Thanks very much
- Dom

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-22-2006
"eatmoreoats" <> wrote in
news: oups.com:

> 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

--
A. Sinan Unur <>
(reverse each component and
remove .invalid for email address)

comp.lang.perl.misc guidelines on
the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
Ch Lamprecht
Guest
Posts: n/a
 
      02-22-2006
A. Sinan Unur wrote:
> "eatmoreoats" <> wrote in
> news: oups.com:
>


> 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

> );
>
> Sinan
>



--

perl -e "print scalar reverse q//"
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-22-2006
Ch Lamprecht <> wrote in news:dtgbgj$j4p
$:

> A. Sinan Unur wrote:

....
>> map { draw => $_ } @from_user,

>
> 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
--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
eatmoreoats
Guest
Posts: n/a
 
      02-22-2006
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

 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      02-22-2006
On 21 Feb 2006 16:52:15 -0800, "eatmoreoats" <> wrote:

>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 !!!
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-22-2006
"eatmoreoats" <> wrote in
news: oups.com:

> 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

--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      02-22-2006
On 21 Feb 2006 16:52:15 -0800, "eatmoreoats" <> wrote:

>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 ....
>);
>...
>
>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


 
Reply With Quote
 
eatmoreoats
Guest
Posts: n/a
 
      02-22-2006
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(0x89c71eIllegal 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

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-22-2006
"eatmoreoats" <> wrote in
news: ups.com:

> 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?

> Rob


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
--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
define_method to define a method to pass a block to another method John J. Franey Ruby 3 06-15-2007 04:33 AM
Pass by reference / pass by value Jerry Java 20 09-09-2005 06:08 PM
Previous return values from a BE method are shown when refreshing an ASP calling that method Shekhar ASP General 0 07-16-2004 03:59 AM



Advertisments