Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

i don't know rather take a look below

by nehlwyn (Acolyte)
on Aug 10, 2001 at 18:44 UTC ( #103883=perlquestion: print w/replies, xml ) Need Help??

nehlwyn has asked for the wisdom of the Perl Monks concerning the following question:

Greetings fellow monks ,

i have this simple question :

let's say i have a call to a subroutine , but i don't know how many arguments will be sent . Then inside the subroutine , there is a call to another function that needs a certain syntax , like an object method , so that i need some of the arguments to be wrtitten indivdually .

How do i do this ?

# bad example ... way (@args); #and let's say that in the course of the programm #@args is made to equal ($a,$b,$c) ... sub way { my ($aa,@bb)=@_; #because i don't know how many scalar are following $a/$aa print "$aa is the first argument\n"; $aa->MethodCall(@_); #but here i'd like to have the elements of @bb , but sorted #individually to have what would be, in this example, the equivalent #of : #$aa->MethodCall($main::a,$main::c);
Is this clear ;-) ??? It shouldn't be , but as well it shouldn't baffle your wisdom ... so ...

The little one.

Replies are listed 'Best First'.
Re: i don't know rather take a look below
by japhy (Canon) on Aug 10, 2001 at 18:51 UTC
    You don't have the problem you think you do.
    sub foo { my $obj = shift; $obj->method(@_); }
    works just fine. No problems. Or you're just not being specific enough.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: i don't know rather take a look below
by stefan k (Curate) on Aug 10, 2001 at 18:50 UTC
    Hi,
    maybe I don't get the point, but you say:
    $aa->MethodCall(@_); #but here i'd like to have the elements of @bb , but sorted
    ... well, just do it: $aa->MethodCall(sort(@bb)); or am I missing something?

    BTW: it's easy to get really confused with @'s when you're coding in Ruby just on the other desktop *g*

    Regards... Stefan

Use a hashref for named parameters...
by dragonchild (Archbishop) on Aug 10, 2001 at 19:01 UTC
    Give us a concrete example. My first reaction is that you're having problems with the naming of parameters. So, what I would do is pass your parameters in as a hashref. Something like:
    sub first_sub { my ($args) = @_; # ... Do stuff here with $args->{FOO} ... $object->method(@{$args}{qw(ARG1 ARG2 ARG3)}); # ... Do more stuff here, this time with $args->{ETC} ... } first_sub( { ARG1 => 0, ARG2 => 1, ARG3 => 2, FOO => 'bar', ETC => 'etc', }, );
    That way, you're using Perl to name your arguments for you. The @{$args}{qw(ARG1 ARG2 ARG3)} syntax is a hashslice of a hashref.
    my @temp = @{$args}{qw(ARG1 ARG2 ARG3)};
    is equivalent to
    my @temp; push @temp, $args->{ARG1}; push @temp, $args->{ARG2}; push @temp, $args->{ARG3};

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

Re: i don't know rather take a look below
by Hofmator (Curate) on Aug 10, 2001 at 19:02 UTC

    Not sure if I'm getting you correctly ... I think you should write your subroutine like this

    sub way { my $aa = shift; $aa->MethodCall(@_); }
    The shift takes the first argument out of @_ and puts it into $aa, the rest of the arguments are passed to MethodCall (and they are still an alias to the original variables, not a copy like in the call $aa->MethodCall(@bb)). I suppose there's a typo in your code
    #$aa->MethodCall($main::a,$main::c); # should be #$aa->MethodCall($main::b,$main::c); # ^^^

    As a side remark, don't use the variables $a and $b as normal variables in a program, they are special to perl (see sort).

    -- Hofmator

Re: i don't know rather take a look below
by bikeNomad (Priest) on Aug 10, 2001 at 18:52 UTC
    In your example, $aa would get the value of $a, and @bb would get ($b, $c). I don't know what you mean by "elements of @bb, but sorted individually", because $a isn't a member of @bb.

    Could you describe what you're trying to do a little better?

      Sure ... let's take an example where $abutton is a gtk button , $a its label , $bbutton if it exists a button from the same button group . ... #depending on the circumstances @args is either equal to #($a) or ($a,$bbutton) Make_Button($abutton,@args); ... sub Make_Button { my ($aa,@bb)=@_; $aa = new Gtk::RadioButton(here must come either $a or $a,$bbutton); } #end of script The syntax is very rigid , how do i manage to have the equivalent of $button = new Gtk::RadioButton($a, $bbutton); within the sub , and starting from @bb Sorry for the confusion , maybe it's confusing because i see a problem where there's none ( but a mere $aa = new Gtk::RadioButton(@bb) within the sub doesn't work so ... P.S.: i pretty new , can you tell me where this message is visible ? i have to pass through my home node to see it ...
Re: i don't know rather take a look below
by nehlwyn (Acolyte) on Aug 10, 2001 at 20:12 UTC
    <HTML> <BODY>

    Greetings again and many thanks , fellow monks, i've taken a little of all your answers and along with a little cleaning of my code (;-) , it works fine now ... The Little One.

    </HTML> </BODY>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://103883]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2022-01-23 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (63 votes). Check out past polls.

    Notices?