Perl Programming Page 2 - Perl Text: Quoting Without Quote Marks |
Another way that we can quote text without using actual quotes is using the here document, which is great for long strings or paragraphs of text. You specify a here document by using the << followed by a delimiter encased in either single quotes or double quotes (single quotes if there is no variable to be interpreted, double if there is). Here is a sample in code: #!/usr/bin/perl print <<'MOOSE'; We the people of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America. MOOSE Note that the word MOOSE can be any word you choose. This results in: We the people of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America. Now try this code: #!/usr/bin/perl $country="United States of America"; print <<'MOOSE'; We the people of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the $country. MOOSE If you run this program, you will see the following print-out: We the people of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the $country. This is because we used the single quote around our delimiter instead of the double quote. The fix is simple: #!/usr/bin/perl $country="United States of America"; print <<"MOOSE"; We the people of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the $country. MOOSE Now if we run the code it will display: We the people of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America. We can also store a here document in a variable: #!/usr/bin/perl $country="United States of America"; $con= <<"MOOSE"; We the people of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the $country. MOOSE print $con; Which results in the same as above: We the people of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.
blog comments powered by Disqus |
|
|
|
|
|
|
|