Reputation: 102
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()" ;
Upvotes: 0
Views: 486
Reputation: 98423
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.
Upvotes: 4
Reputation: 12503
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).
Upvotes: 3