function calls in double quoted strings

D

david

HI all,

This is more a question out of curiosity. Is it possible to call a
function from a double quoted string. For example
print "The absolute value of -2 is abs(-2). This value is bigger than
0"

I know i can use sprintf or write the string like this

print 'The absolute value of -2 is '.abs(-2).'. This value is bigger
than 0'

I am just curious.

Thanks,
David
 
J

Josef Moellers

david said:
HI all,

This is more a question out of curiosity. Is it possible to call a
function from a double quoted string. For example
print "The absolute value of -2 is abs(-2). This value is bigger than
0"

Have you tried it? What happened? The earth collapsed? Bush got another
term in office?
 
D

david

Have you tried it? What happened? The earth collapsed? Bush got another
term in office?

--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
        If failure had no penalty success would not be a prize (T..  Pratchett)
Company Details:http://www.fujitsu-siemens.com/imprint.html

something much more dramatic:
This is the print text:
The absolute value of -2 is abs(-2). This value is bigger than 0
 
R

RedGrittyBrick

david said:
something much more dramatic:
This is the print text:
The absolute value of -2 is abs(-2). This value is bigger than 0

Most people think it a mistake to confuse data and code.

If you were determined, you could do so. You might parse the string and
use eval on bits that look like functions. I'd reconsider.
 
D

david

Most people think it a mistake to confuse data and code.

If you were determined, you could do so. You might parse the string and
use eval on bits that look like functions. I'd reconsider.

Interesting, but how do i know how a function does look like
what is if i have a string
"blah (a,b,c)"
Is this a function ?
Is it a normal list ?
 
R

RedGrittyBrick

david said:
Interesting, but how do i know how a function does look like

Good question. How do *you* know? (only *you* can answer that). Once
*you* have decided how, *you* can write Perl code to implement *your*
method. After that, I expect this newsgroup will be of more help.
what is if i have a string
"blah (a,b,c)"
Is this a function ?
Is it a normal list ?

I'd say it can be either, both or neither. Written languages are often
ambiguous. This is one reason why I think writing a compiler must be
non-trivial.

Where did your double quoted strings originate? Did you write them? Did
someone else write them? Did they follow some convention? Could you
rewrite them to follow an unambiguous convention?

IME hard problems rarely have easy solutions. Easy solutions to hard
problems might sometimes just underestimate their difficulty.
 
T

Tad J McClellan

david said:
On Jan 20, 6:23 pm, RedGrittyBrick <[email protected]>
wrote:

Not snipped, because it appears to have been skipped... :-(

Interesting, but how do i know how a function does look like
what is if i have a string ^^^^^^^^^^^^^^^
"blah (a,b,c)"
Is this a function ?
Is it a normal list ?


You said yourself that it is a string.

A string is not a function.

A string is not a list.

So ""blah (a,b,c)" is ... a string!
 
T

Ted Zlatanov

d> This is more a question out of curiosity. Is it possible to call a
d> function from a double quoted string. For example
d> print "The absolute value of -2 is abs(-2). This value is bigger than
d> 0"

d> I know i can use sprintf or write the string like this

d> print 'The absolute value of -2 is '.abs(-2).'. This value is bigger
d> than 0'

Yes:

print "The absolute value of -2 is @{[ abs(-2) ]}. This value is bigger than 0\n";

You can put any Perl code in there, but I wouldn't.

Ted
 
E

Eric Pozharski

If "abs(-2)" was automagically replaced with "2", then you
would not be able to print that 7-character string.

ie. If "data" was "code", then:

print "use abs(-2) to find the absolute value of -2\n"

would output

use 2 to find the absolute value of -2

What would be definition of "code" that would prohibit requiring Perl v2
or later in this case?
 
C

cartercc

Most people think it a mistake to confuse data and code.

One of my New Years resolutions is to become competent in Lisp. So
far, I'm making good progress.

One of the Big Deals about Lisp is that there's no distinction between
code and data -- code IS data and data IS code!

I haven't wrapped my mind around this idea, yet. When I do, I'll share
it with you. However, Lisp is a fifty year old language that some very
smart people advocate and that's used in some very complex
applications, so maybe there's something to it.

One of my Christmas presents to myself was MJD's 'Higher Order Perl'.
I haven't read past the first chapter (due to time constraints) but it
addresses some big ideas, and I'm looking forward to reading it
sometime this year.

CC
 

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,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top