Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Perl one-liner Quotes help

by Anonymous Monk
on Jul 15, 2010 at 18:06 UTC ( #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.

Replies are listed 'Best First'.
Re: Perl one-liner Quotes help
by ikegami (Pope) 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";'
    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.



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 \'\'  ' "


    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 -
Re: Perl one-liner Quotes help
by mikeraz (Friar) on Jul 15, 2010 at 18:20 UTC



    wait, not those kinds of quotes


        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?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2021-11-28 00:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found