Results 1 to 6 of 6

Thread: Perl Print Question

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  2. #2
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    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:
    1. #!/usr/bin/perl -T
    2.  
    3. use warnings;
    4. use strict;
    5.  
    6. my $foo = "bar";
    7. print '$foo = ' . $foo;
    Last edited by tr333; Feb 21st, 2013 at 06:21 PM.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  4. #4
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    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
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  6. #6
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    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.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width