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

how to access the value of subroutine using its name.I tested with passing hash and accessing the values using keys.Is there any way to achieve it.

Replies are listed 'Best First'.
Re: named_arguments in subroutine
by Utilitarian (Vicar) on Aug 13, 2009 at 10:09 UTC
    Could you include a snippet of code that can be run which reproduces the issue and your expected output, it is somewhat unclear what you mean

    When you say you passed a hash, do you mean you called the routine with a hash as a parameter? and if so did you call it like
    my $value=mySub(%hash); or my $value=mySub(\%hash;)
    Example call below

    #! /usr/bin/perl use strict; use warnings; my %hash=qw(fruit apple dairy cheese cereal oats); sub whatis{ my $hash=shift; for my $category (keys %{$hash}){ print "$hash->{$category} is a $category\n"; } } whatis(\%hash);

    Or do you mean that the subroutine returns a hash and you wish to access a value of this hash by it's key? Horrible example below

    #! /usr/bin/perl use strict; use warnings; sub foodCategories{ my %hash=qw(fruit apple dairy cheese cereal oats); return \%hash; } sub whatis{ for my $category (keys %{foodCategories()}){ print &foodCategories->{$category}," is a $category\n"; } } whatis();
    Update: added code examples
Re: named_arguments in subroutine
by Anonymous Monk on Aug 13, 2009 at 08:46 UTC
Re: named_arguments in subroutine
by Bloodnok (Vicar) on Aug 13, 2009 at 10:49 UTC
    Earlier replies from Utilitarian & AM aside, it strikes me that you're asking 2 entirely unconnected questions -
    1. Appears (to me) to be asking how to convert a sub name to the corresponding code ref .oO(is that the only value of a subroutine ?).
    2. IMO, the 2nd bears no relation to the 1st [question]
    Additionally, you've not provided any clue(s) as to what you've done nor have you indicated where you found the problem(s) to which you allude.

    In short, I suggest you read How do I post a question effectively? ...closely.

    A user level that continues to overstate my experience :-))
Re: named_arguments in subroutine
by dsheroh (Monsignor) on Aug 13, 2009 at 11:20 UTC
    The most common way is by passing a hash and accessing its values using keys, which you say you've already tested. Did this not work as you expected?

    We could probably give you detailed answers if you provided the code you tested with along with an explanation of what you wanted it to do and how the actual results differed from your intended results. Without that information, though, we'd just be guessing at what you want.