Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print @ in quotes in perl

I want to print the line:

my email is "[email protected]" 

using a perl script.

Below is a snippet of what I am trying to do:

print "my email is \"[email protected]\";

However when I run the program is does not recognize the @ symbol. How do I tweak the code snippet so that I get the @ to display?

like image 397
jms1980 Avatar asked Jul 25 '18 23:07

jms1980


People also ask

How do you print quotation marks in Perl?

The simplest change you could make is to just put a backslash ( \ ) in front of the @ symbol to 'escape' it - i.e.: \@ tells Perl that you just want an @ character and this it is not the start of an array identifier. print "my email is \"abc\@gmail.com\"\n"; This does look a little messy.

How do I insert a quote in Perl?

The String is defined by the user within a single quote (') or double quote (“). In Perl, strings can be put in between double-quotes (” “) or in between single-quotes (' ').

How do I print special characters in Perl?

my $var = "\\n"; $var =~s/\\//g; print "$'"; Else If you want print the \n . Just use the single quotes instead of double quotes. or use forward slashes within double quotes.

How do I print in Perl?

print() operator – print operator in Perl is used to print the values of the expressions in a List passed to it as an argument. Print operator prints whatever is passed to it as an argument whether it be a string, a number, a variable or anything. Double-quotes(“”) is used as a delimiter to this operator.


1 Answers

One of the mottos of the Perl community is "there's more than one way to do it". In this case there are many ways to achieve what you're trying to do, each will have advantages in different situations. I've illustrated some answers below.

Firstly, you didn't mention what output you were getting. Assuming you have some sensible default boilerplate like this at the top of your script ...

#!/usr/bin/perl

use 5.012;
use strict;
use warnings;
use autodie;

then this line of code ...

print "my email is \"[email protected]\"\n";

should give you an error message similar to this:

Possible unintended interpolation of @gmail in string at ./ptst.pl line 10.
Global symbol "@gmail" requires explicit package name at ./ptst.pl line 10.
Execution of ./ptst.pl aborted due to compilation errors.

Which is telling you that Perl is interpreting the @ symbol to be the start of an array name. So it takes the 'word' which follows and uses that as an identifier name and thinks you're trying to refer to the array variable @gmail. Since the script does not declare that array with my @gmail anywhere, use strict will throw a fatal exception. (Note: I tweaked your code a little to include \n to add a newline at the end).

The simplest change you could make is to just put a backslash (\) in front of the @ symbol to 'escape' it - i.e.: \@ tells Perl that you just want an @ character and this it is not the start of an array identifier.

print "my email is \"abc\@gmail.com\"\n";

This does look a little messy. One reason is that you have a double quoted string with double quotes in it, so they are also escaped with \". Another way to represent a double-quoted string is to use the qq operator and then choose your own string delimiters. Here I used curly braces as the delimiters, so the embedded double quotes no longer need to be escaped:

print qq{my email is "abc\@gmail.com"\n};

However the main reason to use double quotes is to allow variables in you string to be 'interpolated' (i.e.: the variable name will be replaced with the contents of the va riable). But your string doesn't include any variables. So a better fix might be to just use single quotes, which don't do variable interpolation so symbols like @ and $ are not special and don't need to be escaped:

print 'my email is "[email protected]"', "\n";

Unfortunately the other thing that double quotes do is allow you to use special sequences like \n for a newline or \t for a tab. In my example I wanted a newline, so I took advantage of the fact that the print function accepts a list of arguments and passed it two strings: the first used single quotes to avoid escaping and the second used double quotes to allow escape sequences. In another context where you had to use a single string, instead of the comma separator, you could use the . operator to concatenate the two strings into one:

my $message = 'my email is "[email protected]"' . "\n";

But if the only reason for using double quotes is to add the newline on the end of the print, then you could instead use the say function which is exactly the same as print but it adds a newline on the end:

say 'my email is "[email protected]"';

If you try this and get an error like (Do you need to predeclare say?) then it's because you're missing the boilerplate lines I mentioned at the start.

Your original script didn't use variables, but adding in a variable would be another way to solve your problem:

my $email = '[email protected]';
say qq{my email is "$email"};

or:

my $email = '[email protected]';
say "my email is '$email'";

Finally, another approach that is sometimes useful is to use printf or sprintf to plug variables into a 'template' string. This can be handy for formatting numbers with decimal places, adding leading zeros, padding strings with spaces etc:

printf(qq{my email is "%s"\n}, $email);

Once again this became a little more complicated due to the newline. If you don't need it then it's easier to use single quotes around the format string:

printf('my email is "%s"', $email);
like image 101
Grant McLean Avatar answered Sep 18 '22 16:09

Grant McLean