0

I am new to perl , Is it any way to write nested perl commands in single line like below (shell)

echo "I am  `uname -n` and today is `date`"

I tried like below ; but not working

my $i=0 ;
print "$i++\n" ;
print "localtime()" ;
2
  • 1
    It's unclear what you're asking. What is the expected output? Commented Sep 8, 2015 at 16:14
  • See Quote and Quote-like Operators for details on what interpolates and what doesn't. Commented Sep 8, 2015 at 18:13

2 Answers 2

4

You can only interpolate variables into double-quoted strings. But those variables can be anonymous, allowing you to provide an expression to generate them. This looks like this:

my $i = 0;
print "Number ${\($i++)} date ${\scalar localtime}";

You can also use @{[ some-expression ]} instead of ${\ some-expression }. In either case, you have an expression and you create a reference to its value (or in the former case, to an anonymous array containing it), and then dereference it in the string. The expression will be in list context, so you may need to add scalar, as above.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ysth; this is what I expected.
3

Is it any way to write nested perl commands in single line like below (shell)

Of course you can.

print "I am " . `uname -n` . "and today is " . `date`;

Perl's backtick operator (`) doesn't work inside double quotes (""). So, the string "I am ", the output of uname -n, the string "and today is " and the output of date are joined by dot operator (.).

I don't understand what the latter part of your question means in relation to the former part (and I think ysth has already answered to it).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.