Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Accessing a scalar value in another subroutine?

by Anonymous Monk
on Sep 23, 2012 at 19:36 UTC ( [id://995239]=note: print w/replies, xml ) Need Help??


in reply to Accessing a scalar value in another subroutine?

Hi.

For example:

sub method1 { my $self = shift; my $docstring = "Prints information about X, Y, and Z"; # ... } sub help { my $self = shift; my $method = shift; # Note: The following approach does not work. # I tried inserting the package name before the method name, i.e., + $package_name::method::docstring, but that too was unsuccessful. + # print $method::docstring, "\n"; } <br/> <br/> Thanks.

Replies are listed 'Best First'.
Re^2: Accessing a scalar value in another subroutine?
by LanX (Saint) on Sep 23, 2012 at 20:03 UTC
    I doubt this is a good idea $docstring is only initialized after method1 is called for the first time.

    So you won't be able to inspect the docstring in advance.

    But if that's OK for you you might wanna try PadWalker::peek_sub.

    UPDATE:

    Nope doesn't help, lexical are reset to undef after the function-scope is left.

    DB<135> sub tst { my $doc="bla" } => 0 DB<136> PadWalker::peek_sub(\&tst) => { "\$doc" => \undef } DB<137> tst() => "bla" DB<138> PadWalker::peek_sub(\&tst) => { "\$doc" => \undef }

    Cheers Rolf

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found