Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Calling a subroutine from a scalar

by tlm (Prior)
on Jun 29, 2005 at 15:47 UTC ( #471075=note: print w/replies, xml ) Need Help??

in reply to Calling a subroutine from a scalar

Your question suggests a logical confusion. One can't store a subroutine in a scalar variable. You can store a reference to a subroutine in a scalar variable, or you can, as in your code, store the name of a subroutine in a scalar variable.

In the former case, the answer to your question is easy:

my $coderef = \&some_sub; # $coderef contains a reference to &some_su +b my $results = $coderef->();

In the latter case, you can either use eval:

my $subname = 'some_sub'; my $results = eval "$subname()"; die $@ if $@;
or you can treat it as a symbolic ref:
my $results = do { no strict 'refs'; $subname->() };

use strict; use warnings; sub some_sub { print "Hello world!\n"; return 42; } my $coderef = \&some_sub; my $subname = 'some_sub'; print $coderef->(), "\n"; print eval "$subname()", "\n"; die $@ if $@; print do { no strict 'refs'; $subname->() }, "\n"; __END__ Hello world! 42 Hello world! 42 Hello world! 42

But storing names of subroutines in variables should be a red flag to alert you that you are probably not thinking about the problem in the right way.

the lowliest monk

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://471075]
[Corion]: TCLion: Whee ;)
[TCLion]: need to put like this : 2017-02-20 09:30:53
[1nickt]: That's why I asked if you are using DateTime. It has a large number of supporting modules (the author likes the term 'eco-system') so if you are already creating a DateTime obj from your dates, this module would read in the mnoron-formatted 1s seamlessly
[TCLion]: when I put the date together it looks like : 2017-Feb-24 (month is the problem)
[1nickt]: good luck, then.
[TCLion]: looking at DateTime documentation in monastery now

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2017-03-23 14:57 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (288 votes). Check out past polls.