user10261
user10261

Reputation: 53

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?

Upvotes: 5

Views: 3262

Answers (7)

Binu
Binu

Reputation:

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

Upvotes: -1

AmbroseChapel
AmbroseChapel

Reputation: 12087

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.

Upvotes: 1

Drew Stephens
Drew Stephens

Reputation: 17827

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

Upvotes: 2

JB.
JB.

Reputation: 42094

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.

Upvotes: 2

hexten
hexten

Reputation: 1187

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?

Upvotes: 11

Mark
Mark

Reputation: 10162

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.

Upvotes: 11

Haabda
Haabda

Reputation: 1433

I'm fairly certain arg2__size is just the name of a variable.

Upvotes: 1

Related Questions