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

Discussion in 'Perl Misc' started by eatmoreoats, Feb 21, 2006.

  1. eatmoreoats

    eatmoreoats Guest

    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
     
    eatmoreoats, Feb 21, 2006
    #1
    1. Advertising

  2. "eatmoreoats" <> wrote in
    news::

    > 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/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Feb 22, 2006
    #2
    1. Advertising

  3. eatmoreoats

    Ch Lamprecht Guest

    A. Sinan Unur wrote:
    > "eatmoreoats" <> wrote in
    > news::
    >


    > 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//"
     
    Ch Lamprecht, Feb 22, 2006
    #3
  4. 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/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Feb 22, 2006
    #4
  5. eatmoreoats

    eatmoreoats Guest

    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
     
    eatmoreoats, Feb 22, 2006
    #5
  6. eatmoreoats

    robic0 Guest

    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 !!!
     
    robic0, Feb 22, 2006
    #6
  7. "eatmoreoats" <> wrote in
    news::

    > 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/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Feb 22, 2006
    #7
  8. eatmoreoats

    robic0 Guest

    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
     
    robic0, Feb 22, 2006
    #8
  9. eatmoreoats

    eatmoreoats Guest

    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
     
    eatmoreoats, Feb 22, 2006
    #9
  10. "eatmoreoats" <> wrote in
    news::

    > 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/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Feb 22, 2006
    #10
  11. eatmoreoats

    robic0 Guest

    On 21 Feb 2006 17:25:27 -0800, "eatmoreoats" <> wrote:

    >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...
    >
    >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
     
    robic0, Feb 22, 2006
    #11
  12. eatmoreoats

    robic0 Guest

    On Wed, 22 Feb 2006 01:30:19 GMT, "A. Sinan Unur" <> wrote:

    >"eatmoreoats" <> wrote in
    >news::
    >
    >> 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.

    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...
    > On the other hand, after you wasted my time like
    >this, I feel like you guys deserve each other.
    >
    >Bye.
    >
    >Sinan
     
    robic0, Feb 22, 2006
    #12
  13. eatmoreoats

    eatmoreoats Guest

    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
     
    eatmoreoats, Feb 22, 2006
    #13
  14. eatmoreoats

    robic0 Guest

    On 21 Feb 2006 18:25:00 -0800, "eatmoreoats" <> wrote:

    >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!
     
    robic0, Feb 22, 2006
    #14
  15. eatmoreoats <> wrote:

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 22, 2006
    #15
  16. eatmoreoats

    eatmoreoats Guest

    Tad McClellan wrote:
    > eatmoreoats <> wrote:
    >
    > > 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).
    >
    >
    > --
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas



    Like I said above, it was a mistake.
    Thanks
     
    eatmoreoats, Feb 23, 2006
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oscar
    Replies:
    0
    Views:
    319
    Oscar
    Jan 13, 2006
  2. Oodini
    Replies:
    1
    Views:
    1,796
    Keith Thompson
    Sep 27, 2005
  3. mike
    Replies:
    5
    Views:
    343
    Ben Pope
    Feb 22, 2006
  4. Replies:
    1
    Views:
    517
    Sion Arrowsmith
    Jul 10, 2008
  5. aidy
    Replies:
    5
    Views:
    241
    Marcin Mielżyński
    Jun 4, 2006
Loading...

Share This Page