Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Perl one-liner Quotes help

by Anonymous Monk
on Jul 15, 2010 at 18:06 UTC ( [id://849818]=perlquestion: print w/replies, xml ) Need Help??

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

I want my Perl one-liner to print quotes. See my attempts below.
$ perl -lane "print 'hello '' ' " $ perl -lane 'print "hello '' " -- this seems to be waiting for som +ething. $ perl -lane 'print "hello \'\' " -- this also seems to be waiting fo +r something.
TIA.

Replies are listed 'Best First'.
Re: Perl one-liner Quotes help
by ikegami (Patriarch) on Jul 15, 2010 at 19:06 UTC

    It helps to mention which shell you're using if you us to help with its quoting mechanism. Looks like bash or similar.

    You want bash to do

    system( q{perl}, q{-lane}, q{print "John's";}, );

    The first two parts are no-brainers since they contain no characters that are special to bash. Let's focus on the third.

    • You could escape every special character
      print\ \"John\'s\";
    • You could use double quotes and escape the characters that are special in double quotes.
      "print \"John's\";"
    • You can't use single quotes, at least not alone. There's no way to include single quotes in a single-quoted bash literal. What you can do is use different mechanisms for different parts of the string (as in q{print "John}.q{'}.q{s";})
      'print "John'"'"'s";' 'print "John'\''s";'
    So
    perl -lane 'print "John'\''s";'

    Note that the following is also acceptable to Perl:

    system( q{perl}, q{-lane}.q{print "John's";}, );

    That could be written as

    perl '-laneprint "John'\''s";'
    but it looks better as
    perl -lane'print "John'\''s";'
Re: Perl one-liner Quotes help
by johngg (Canon) on Jul 15, 2010 at 20:59 UTC

    You can use hex escapes inside a double-quote quoting construct (qq{...}). Works on *nix and Windows.

    $ perl -le 'print qq{hello \x27\x22 \x22\x27};' hello '" "' $
    C:\>perl -le "print qq{hello \x27\x22 \x22\x27};" hello '" "' C:\>

    I hope this is useful.

    Cheers,

    JohnGG

Re: Perl one-liner Quotes help
by alexbio (Monk) on Jul 15, 2010 at 18:36 UTC

    In the second and third examples, you forgot a final '

    If you want ' print working try

    perl -e "print 'hello \'\'  ' "

    or

    perl -e "print \"hello \'\'  \" "

    (Like the first example but with \')

    The second and third, wait for something because they (with -lane) mean

    BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = <ARGV>)) { chomp $_; our(@F) = split(' ', $_, 0); print 'hello '; }

    Update: Just inverted order of things and corrected some errors.

    Alex's Log - http://alexlog.co.cc
Re: Perl one-liner Quotes help
by mikeraz (Friar) on Jul 15, 2010 at 18:20 UTC

    consider

        system("fortune"); 

    wait, not those kinds of quotes

    Does

        perl -e 'print "hello \"quoted name\"\n";'
    
    do something like you want?

    Generally though if you use " and then want to print a " you need to escape it by preceding it with a \ see example above. Generalize to any type of quote character.


    Be Appropriate && Follow Your Curiosity
Re: Perl one-liner Quotes help
by haoess (Curate) on Jul 15, 2010 at 19:57 UTC
    % perl -E $'say q{"\'}' "' % echo $SHELL /bin/zsh

    -- Frank

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://849818]
Approved by ahmad
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found