Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

question about using constants

by emilford (Friar)
on Feb 02, 2005 at 20:06 UTC ( [id://427406]=perlquestion: print w/replies, xml ) Need Help??

emilford has asked for the wisdom of the Perl Monks concerning the following question:

These constants do not directly interpolate into double-quotish string +s, although you may do so indirectly. (See perlref for details about how this works.) print "The value of PI is @{[ PI ]}.\n";
This is listed in the notes for constants.pm and I'm trying to understand the @{[PI]} piece of this. Could someone check my understanding of this? The PI is just a subroutine call, correct, since the notes say it can also be written as PI() to obtain the result. The []'s are putting the return from PI() into list context and the @{} then dereferences that? Am I anywhere close and is there an easier way to use these constant values within a set of double quotes?

20050204 Edit by castaway: Changed title from 'question about using contants'

Replies are listed 'Best First'.
Re: question about using constants
by dragonchild (Archbishop) on Feb 02, 2005 at 20:13 UTC
    The question you're asking is "Is there any easier way to call a subroutine within doublequotes?". The answer, AFAIK, is no.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      So then is that all the @{[]} is doing? Beyond finding a cleaner way, I want to also understand the workings of this.
        You're creating an array reference, then dereferencing it. In the process, you're allowed to make any sort of Perl executing code, not just variable interpolation.

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        It really falls under the category of one of those things that is possible but not all that advisable.

        Just because it's possible to say, "Let's interpolate a function: @{[PI]}\n";, I'm not convinced that it's in any way clearer than: "Lets not interpolate: " . PI . "\n";

        In the first example the double-quoted string is parsed for interpolation components. The @{...} is found, and that tells Perl you want to interpolate a reference to an array. And the [....] creates that reference, based on the "...." expression.

        In the second example, you start with a string, concatenate the evaluated expression, and concatenate a newline character. Much simpler, and less work.


        Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://427406]
Approved by kutsu
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: (3)
As of 2024-04-25 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found