Calling perl built-in functions

  • Thread starter Janek Schleicher
  • Start date
J

Janek Schleicher

Jon wrote at Tue, 02 Sep 2003 04:12:37 -0700:
my $function = "print";
my $args = "hello, world";

&$function $args;


It produces:
Undefined subroutine &main::print called

Is there a way to make this work?

Well, you can always do the eval workaround:

eval "$function $args";

with some more problems ...

But what's the problem behind the problem you've written to us?


Greetings,
Janek
 
J

Jon

I have a script that parses a file. In this file, I would like to be able to
put in perl build in functions. So, with that in mind, let me give you this
example that doesn't work:

my $function = "print";
my $args = "hello, world";

&$function $args;


It produces:
Undefined subroutine &main::print called

Is there a way to make this work?

Thanks in advance,

Jon
 
A

Anno Siegel

Jon said:
I have a script that parses a file. In this file, I would like to be able to
put in perl build in functions. So, with that in mind, let me give you this
example that doesn't work:

my $function = "print";
my $args = "hello, world";

&$function $args;


It produces:
Undefined subroutine &main::print called

Well, builtins are not subroutines and can't be called as such.
Is there a way to make this work?

Make it work how? Do you want to change $function and $args so that
"&$function $args" works, or do you want to keep the values in $function
and $args and write a call that works?

Both are possible. Make up your mind.

Anno
 
S

Shawn Corey

Anno said:
Well, builtins are not subroutines and can't be called as such.




Make it work how? Do you want to change $function and $args so that
"&$function $args" works, or do you want to keep the values in $function
and $args and write a call that works?

Both are possible. Make up your mind.

Anno

From my bag of dirty tricks:
#!/usr/bin/perl

$function='print';

&$function("Hello, world\n");

sub print {
print @_;
}
 
U

Uri Guttman

SC> From my bag of dirty tricks:

very dirty.

SC> #!/usr/bin/perl

SC> $function='print';

SC> &$function("Hello, world\n");

SC> sub print {
SC> print @_;
SC> }

try that under use strict.

next!!

uri
 
S

Shawn Corey

Uri said:
SC> From my bag of dirty tricks:

very dirty.

SC> #!/usr/bin/perl

SC> $function='print';

SC> &$function("Hello, world\n");

SC> sub print {
SC> print @_;
SC> }

try that under use strict.

next!!

uri
I didn't say it was good programming pratice; in fact, I called it a
dirty trick.
 
J

Jon

Sorry for the delayed response...I ended up going out of town and just
got back.

Here is the problem I'm having. Using Parse::RecDescent, I wrote a
simple language for my users to run tests without having to know Perl.
For, example, a config file would look like:

parallel {
test1
test2
test3
}

serial {
test1
test2
test3
}

This then does all the perl stuff like forking to run tests in
parallel or serial without the user not having to know anything other
than my simple language. Here is something I would like to add:

serial {
test1
print(running test2)
test2
}

Here's the kicker though...I want them to use any perl built in
function without me having to know ahead of time. I don't want to
have a function called print that runs the built-in print function
when it sees it in my file. I would like to parse the file and get
back $function($args) and then run it. $function can be any built in
function, but I won't know what it is.

Does that make sense?

Thanks
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top