What does -G do in Perl?
I agree with JesperE, please show us some code. However, as far as I can tell, this is what is happening:
if(-G) {
Perl sees this, does not recognize it, -G
and therefore treats it as a string. This will:
if('-G') {
Which is equivalent:
if(1) {
So, as far as I can tell, if(-G)
does nothing. I tried to use it and it always returns true which confirms my hypothesis. Further support comes from the following code (tested on OS X with Perl 5.10.0):
use strict;
use warnings;
my $var = -G;
print "$var\n";
Displays warnings, compiles and runs, and prints just "-G".
EDIT: Doing a search that I should have done much earlier suggests the following from the Perldoc perlop page:
Unary "-" performs arithmetic negation if the operand is numeric. If the operand is an identifier, a string consisting of the minus sign concatenated with the identifier is returned. Otherwise, if the string starts with a plus or minus sign, a string starting with the opposite sign is returned. One of the consequences of these rules is that -bareword is equivalent to the string "-bareword". If, however, the string begins with a non-alphabetic character (excluding "+" or "-"), Perl will try to convert the string to a numeric value and perform arithmetic negation. If a string cannot be cleanly converted to numeric, Perl will issue a warning . String argument is not numeric in negation (-) at address.
As pointed out in the comments B::Deparse
shows what Perl converts if(-G)
to if(-'G')
. However, the documentation (and behavior from print()
) is consistent with the documentation, which says it should convert if(-G)
to if('-G')
. This does not change the output of the program in any way.
However, the subtle typing differences in the behavior of unary operators, which 99% of people will only use by numbers, are not what I would call "basic." I do not think that someone should (or will ever need) to use a conversion -bareword
to 'bareword'
any practical situation.
a source to share
There is no -G switch in perl.
perl -G Unrecognized switch: -G (-h will show valid options).
Edit: OK, there's nothing with -G in there - just -g.
-g The setgid bit is set in the file.
http://perldoc.perl.org/perlfunc.html
Otherwise, it is nonsense and the question is wrong.
a source to share
This is clearly a confusion between the [(test) options and the Perl -X file checks. -G is in the former (on my BSD system) but not the latter. -G is a non-positional extension, and I guess Perl doesn't include all extensions, just a few. So he either wanted to say -g, or he meant [-G $ file]; (for some superset of POSIX [). It's also in my default shell (pdksh) and bash (the default linux shell, for the most part)
-G in test or as a wrapper built in here:
-G file True if file exists and its group matches the effective group ID of this process.
a source to share
One answer says, "I don't think anyone should (or will ever need to) use the -bareword to -'bareword conversion in any practical situation."
This is widely used in one style of named parameters. See the venerable CGI for one:
$cookie1 = $q->cookie(-name=>'riddle_name', -value=>"The Sphynx Question");
$cookie2 = $q->cookie(-name=>'answers', -value=>\%answers);
print $q->header(
-type => 'image/gif',
-expires => '+3d',
-cookie => [$cookie1,$cookie2]
);
a source to share