|
-
Feb 21st, 2013, 02:01 PM
#1
Perl Print Question
In Perl if I do this:
print "$variable\n";
I get the contents of $variable
How do I print $variable as a text string and not it's contents?
For example, in Visual Basic, I can do this
Dim Name As String
Name = "James Rickland"
Print "Name = " & Name
and it appears like this: Name = James Rickland
This is what I want to do in Perl
print "$variable = $variable\n";
How to show $variable = as text
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Feb 21st, 2013, 06:15 PM
#2
Re: Perl Print Question
You can use PadWalker to achieve that, but I have to ask why you would want to? It's not a very common thing to do, and I've yet to come across a valid reason for needing it. If you are wanting to use it for debugging, then Data::Dumper and related modules are a better choice.
EDIT: On second thoughts, after reading your post again, I see it differently. You want to use single quotes around your string. Double-quoted strings do variable interpolation inside them while single-quoted strings do not. If it's for debugging, then I still would recommend using Data::Dumper and printing the output of Dumper($var) to stderr.
perl Code:
#!/usr/bin/perl -T use warnings; use strict; my $foo = "bar"; print '$foo = ' . $foo;
Last edited by tr333; Feb 21st, 2013 at 06:21 PM.
-
Feb 21st, 2013, 06:32 PM
#3
Re: Perl Print Question
I'm not good at Perl so as I am trying to learn it I write very simple Perl scripts. I am a VB programmer and as I write in Perl I think VB on certain things so I think to myself, if this is how I do it in VB then maybe I can do similar in Perl.
Thanks, for your reply. That help a lot.
BTW:
What's with this my I see in your code. I have never used my so what does it do compared to not using my
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Feb 24th, 2013, 06:07 PM
#4
Re: Perl Print Question
my is for lexical scoping: http://perl.plover.com/FAQs/Namespaces.html
You should always write your perl scripts with "use strict;" and "use warnings;". Assigning values to variables that haven't been defined with "my" will throw an error under "use strict;", which is a good thing
-
Feb 24th, 2013, 06:31 PM
#5
Re: Perl Print Question
OK, good to know that. Thanks again
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Feb 26th, 2013, 02:07 AM
#6
Re: Perl Print Question
And one more thing...
If you're doing web-related stuff with Perl, it's recommended to run your scripts under Taint mode.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|