Single quotes in a variable name in Perl? - perl

I was writing some Perl code in vim and accidentally typed a single quote character in a variable name and noticed that it highlighted it in a different color than normal single quoted strings.
I thought that was odd, so I wrote a small test program (shown above) and tried to run it to see how Perl would handle it and I got this error:
"my" variable $var::with::apostrophes can't be in a package
What exactly is going on here? Are there situations where single quotes in variable names are actually valid? If so, what meaning do single quotes have when used in this context?

The single quote is the namespace separator used in Perl 4, replaced by the double colon :: in Perl 5. Because Perl is mostly backwards compatible, this still works. It's great for golfing, but not much else.
Here's an article about it on perl.com that doesn't explain it.

Related

How to match \n\n using perl one liner?

My Sample file:
As I Come Into Your Presence
Key: F
1 As I come into Your presence
Past the gates of praise
Into Your sanctuary
Till we are standing face to face
And look upon Your countenance
I see the fullness of Your glory
And I can only bow down and say
Chorus:
Your awesome in this place
Mighty God
You are awesome in this place
Abba Father
You are worthy of all praise
To You our lives we raise
You are awesome in this place
Mighty God
<--- Empty line here
<--- Empty line here
I wrote this perl one-liner to get <i></i> tags around the entire chorus block:
perl -p0e "s#Chorus:(.*?)\n\n#<i>Chorus:$1</i>#gsm" file
The result:
As I Come Into Your Presence
Key: F
1 As I come into Your presence
Past the gates of praise
Into Your sanctuary
Till we are standing face to face
And look upon Your countenance
I see the fullness of Your glory
And I can only bow down and say
<i>Chorus:</i>%
I can't get the desired result where the </i> tag would be printed after the entire chorus after the Mighty God.
Where is the error? How can I achieve this?
Your solution would work if you just put it in single quotes instead of double quotes. You should pretty much always use single quotes for one-liners from the shell, no matter what language/interpreter you're running, to keep shell interpolation from messing things up.
In your code:
perl -p0e "s#Chorus:(.*?)\n\n#<i>Chorus:$1</i>#gsm" file
The $1 is being expanded by the shell before it ever gets to Perl, so Perl sees this:
perl -p0e "s#Chorus:(.*?)\n\n#<i>Chorus:</i>#gsm" file
and happily deletes your chorus. If you use single quotes instead:
perl -p0e 's#Chorus:(.*?)\n\n#<i>Chorus:$1</i>#gsm' file
it will work as intended.
Note, however, that the -0 means any NUL characters that creep into the input will still cause Perl to split it into multiple records at that point. A more correct solution would be to use -0777 instead, which tells Perl that no value should split the input; it is treated as a single record no matter what data it contains.
perl -p0777e 's#Chorus:(.*?)\n\n#<i>Chorus:$1</i>#gsm' file
escape the $
perl -p0777e "s#Chorus:(.*?)\n\n#<i>Chorus:\$1</i>#gsm" file.
also as #Kenney mention in the comment:
Use single quotes on the commandline to wrap perl expressions otherwise the shell expansion will kick in.

How to understand this perl multi-line writing command

I am trying to understand the perl commands below:
$my = << EOU;
This is an example.
Example too.
EOU
What is the name of this way? Could somebody can explain more about this "multi-line writing" command?
Essentially the syntax is allowing you to put anything unique as a marker so that it won't conflict with your contents. You can do this:
$my = <<ABCDEFG;
This is an example.
Example too.
BLAH
ABCDEFG
Everything between "This.." and "BLAH" will be assigned to the variable. Note that you shouldn't have a space after the << symbols otherwise you will get a syntax error. It helps avoid adding CR characters, or append (.) everywhere, and useful when passing data into another application (eg. ftp session). Here Documents is the correct term for this.
Everything between <<EOU and EOU is a multi-line, non-escapable, string. It's nothing fancy, think of them as start and end quote marks with nothing inside requiring escapes to be literally what you typed...

Why is the apostrophe sign a valid path separator in Perl

In Perl, you call modules using :: as path separator. So, if you have a module on site/lib/GD/Image.pm, you call use GD::Image.
However, long time ago I found out that you can also call use GD'Image and things like my $img = new GD'Image;, and there are also modules on CPAN using that syntax on ther names/documentation.
What is the purpose or logic behind that? Is it maybe, as many things in Perl, just a feature intended to humanize sentences and allow you to create and use modules like Acme::Don't?
Does it have any other intention different to ::?
See perlmod for explanation:
The old package delimiter was a single quote, but double colon is now the preferred delimiter
So, the reason is history.
The single quote is an old ADA separator. However, it didn't play well with Emacs, so the double colon became used.
Good God! ADA? Emacs? I am old.

How does Getopt::Std handle spaces in arguments on the command line?

I've been playing around with the Getopt::Std module and was wondering about arguments taking spaces.
I have this code atm: getopts('dp:h', \%options);
The problem is, that if the argument following the p flag contains a space, getopts stops processing the list right when it hits the space. Is there a way I can allow spaces in the arguments without having to wrap the arguments following the flag in quotes (-p "something something")?
I'm fine with quotes. I'm just curious. Thanks guys!
Take a look here, someone did a lot of experimenting ..
and this page indicates that Double Quotes " char(34) will work, if you have spaces embedded, thus implying that there is no other way ..

What is a double underscore in Perl?

I'm trying to understand someone else's Perl code without knowing much Perl myself. I would appreciate your help.
I've encountered a Perl function along these lines:
MyFunction($arg1,$arg2__size,$arg3)
Is there a meaning to the double-underscore syntax in $arg2, or is it just part of the name of the second argument?
There is no specific meaning to the use of a __ inside of a perl variable name. It's likely programmer preference, especially in the case that you've cited in your question. You can see more information about perl variable naming here.
As in most languages underscore is just part of an identifier; no special meaning.
But are you sure it's Perl? There aren't any sigils on the variables. Can you post more context?
As far as the interpreter is concerned, an underscore is just another character allowed in identifiers. It can be used as an alternative to concatenation or camel case to form multi-word identifiers.
A leading underscore is often used to mean an identifier is for local use only, e.g. for non-exported parts of a module. It's merely a convention; the interpreter doesn't care.
In the context of your question, the double underscore doesn't have any programmatic meaning. Double underscores does mean something special for a limited number of values in Perl, most notably __FILE__ & __LINE__. These are special literals that aren't prefixed with a sigil ($, % or #) and are only interpolated outside of quotes. They contain the full path & name of the currently executing file and the line that is being executed. See the section on 'Special Literals' in perldata or this post on Perl Monks
I'm fairly certain arg2__size is just the name of a variable.
Mark's answer is of course correct, it has no special meaning.
But I want to note that your example doesn't look like Perl at all. Perl variables aren't barewords. They have the sigils, as you will see from the links above. And Perl doesn't have "functions", it has subroutines.
So there may be some confusion about which language we're talking about.
You will need to tell the interpreter that "$arg2" is the name of a variable. and not "$arg2__size". For this you will need to use the parenthesis. (This usage is similar to that seen in shell).
This should work
MyFunction($arg1,${arg2}__size,$arg3)
--Binu