Assigning virtual filename to eval block

T

tobias.grimm

Hi!

Assuming I have something like this:

my $str = "sub foo { print 1 / 0; }";
eval($str);
foo();

.... then I get:

Illegal division by zero at (eval 1) line 1.

Can I somehow give the eval block a more meaningful filename than
"(eval N)"?

Tobias
 
J

Joost Diepenmaat

Hi!

Assuming I have something like this:

my $str = "sub foo { print 1 / 0; }";
eval($str);
foo();

... then I get:

Illegal division by zero at (eval 1) line 1.

Can I somehow give the eval block a more meaningful filename than
"(eval N)"?

my $str = <<CODE;
#line 1 /my/fake/file.txt
sub foo { print 1 / 0; }
CODE

See also the end of perlsyn.
 
B

Ben Morrow

Quoth (e-mail address removed):
Assuming I have something like this:

my $str = "sub foo { print 1 / 0; }";
eval($str);
foo();

... then I get:

Illegal division by zero at (eval 1) line 1.

Can I somehow give the eval block a more meaningful filename than
"(eval N)"?

Use #line:

my $str = <<'PERL'
#line 1 eval_for_foo
sub foo { print 1/0 }
PERL

eval $str or die $@;
foo();

(at least in my version of perl it's necessary to check the eval
succeeded, as otherwise the division is done at compile time, as part of
constant folding, and the sub never gets created at all).

Ben
 
A

A. Sinan Unur

(e-mail address removed) wrote in (e-mail address removed):
Assuming I have something like this:

I would to have to first ask why you are using a string eval for what is
just an anonymous sub invocation. You might have a good reason, but most
posters don't, so I ask.

Now, given that you are calling foo by name after the eval, I really
cannot think of any reason to use a string eval rather than an anonymous
subroutine.
my $str = "sub foo { print 1 / 0; }";
eval($str);
foo();

Compare it to

my $sub = sub { print 1/0 };
$sub->();
... then I get:

Illegal division by zero at (eval 1) line 1.

Can I somehow give the eval block a more meaningful filename than
"(eval N)"?

I don't know. Frankly, I don't think you should waste time on this.

If you use an anonymous sub you will get both compile time error
checking and the ability to apply hack #57 in the excellent "Perl
Hacks" book.

The code is available at <URL:http://examples.oreilly.com/perlhks/>.
Download the archive at that location and look in

perl_hacks_examples\debugging\name_your_anonymous_subroutines

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
A

A. Sinan Unur

Quoth (e-mail address removed):

Use #line:

my $str = <<'PERL'
#line 1 eval_for_foo
sub foo { print 1/0 }
PERL

eval $str or die $@;
foo();

Well, that's clever. I am afraid I forgot about #line as I have never
had a reason to use it.

On the other hand, am I the only one who thinks it smells bad to use
string eval for a function invoked by name?

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
T

tobias.grimm

Sinan,

this was just as small sample. I have a perl module written in C++
which generates perl module code at runtime and evals it, so it
becomes available to the calling perl application.

Tobias
 
A

A. Sinan Unur

(e-mail address removed) wrote in (e-mail address removed):
this was just as small sample. I have a perl module written in C++
which generates perl module code at runtime and evals it, so it
becomes available to the calling perl application.

Well, thank you for the explanation. You know, I really did not think
"print 1/0" was the actual code you were eval'ing ;-)

Apologies for the misunderstanding.

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top