tags:

views:

181

answers:

6

I have searched around the Internet and the Perl doc and can't seem to find the answer.

Help will be appreciated.

EDIT:: He asked me about -G, wrote it down on a piece of paper and when i looked stumped asked me to go read up on the basics.

+1  A: 

There's no switch -G in perl.

perl -G Unrecognized switch: -G (-h will show valid options).

Edit: OK, there's nothing with -G either - only -g.

-g File has setgid bit set.

http://perldoc.perl.org/perlfunc.html

Otherwise, it's nonsense and the question is misphrased.

Artem Russakovskii
He means in the code, not a switch: `print "Hello" if -G;`
Chris Lutz
+6  A: 

I agree with JesperE, please show us some code. However, as far as I can tell, this is what's happening:

if(-G) {

Perl sees this, doesn't recognize -G, and so treats it as a string. It becomes:

if('-G') {

Which is equivalent to:

if(1) {

So as far as I can tell, if(-G) does nothing. I've tried using it, and it always seems to return true, which supports my hypothesis. Further support is from the following code (tested on OS X with Perl 5.10.0):

use strict;
use warnings;

my $var = -G;
print "$var\n";

Displays no warnings, compiles and runs, and prints simply "-G".

EDIT: Doing a search I should have done much earlier provides the following from Perldoc's perlop page:

Unary "-" performs arithmetic negation if the operand is numeric. If the operand is an identifier, a string consisting of a minus sign concatenated with the identifier is returned. Otherwise, if the string starts with a plus or minus, a string starting with the opposite sign is returned. One effect 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 attempt to convert the string to a numeric and the arithmetic negation is performed. If the string cannot be cleanly converted to a numeric, Perl will give the warning Argument "the string" isn't numeric in negation (-) at ....

As stated in the comments, B::Deparse appears to show that Perl converts if(-G) to if(-'G'). However, the documentation (and the behavior with print()) are consistent with the documentation, which says that it should convert if(-G) to if('-G'). This doesn't change the result of the program either way.

However, subtle typing differences in the behaviors of unary operators that 99% of people will only ever use on numbers are not what I would call "basic." I don't think anyone should (or would ever need to) use the -bareword to 'bareword' conversion in any practical situation.

Chris Lutz
If that's the case the questioner should be slapped upside the head. That's not "the basics." It's a trick question and not even one that reveals any useful functionality. Even if he was referring to the -g filetest operator I wouldn't consider that "basic."
Michael Carman
I agree in both cases, but that's the only answer I can figure out (other than the -g filetest.)
Chris Lutz
B::Deparse will show what Perl thinks it is. It's not `if('-G')`, its `if(-'G')` which happens to be true. Anyhow, Michael is right. "-g" certainly isn't "basic". I don't think I've ever used it and I don't know why anyone would memorize it.
Schwern
+1  A: 

I don't know about -G but -g is described here as

-g  File has setgid bit set.
workmad3
I thought this at first, but I'm not sure that this is what he's talking about. Especially since the code `if(-G) {` produces no errors or warnings.
Chris Lutz
A: 

Sorry guys an examiner asked this question in my practicals. He said it exists and advised me to read up on the basics. I could not find it either.

Thanks for the help and your time.

qwrty
Did he specifically say -G (capital G) or could it perhaps have been -g (lowercase g)? Also, in the future, to add information to your question, it's best to edit your original question rather than post an answer. There should be a gray "edit" button under your question.
Chris Lutz
sorry mate my bad.
qwrty
Tell him to come talk to us. We'll straighten him out. ;)
Michael Carman
edit question, don't add a non-answer answer
Brad Gilbert
A: 

This is clearly confusion between [ (test) options and Perl -X file tests. -G is in the former (on my BSD system), but not the latter. -G is a non-posix extension and I guess Perl didn't include all the extensions, just some. So its either, he meant to say -g or he meant [ -G $file ]; (for some superset of POSIX [). It is also in my default shell (pdksh) and bash (the linux default shell, for the most part)

-G in test or as a shell builtin here:

-G file True if file exists and its group matches the effective group ID of this process.

MkV
A: 

One answer says: "I don't think anyone should (or would 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's Question");
$cookie2 = $q->cookie(-name=>'answers', -value=>\%answers);
print $q->header(
    -type    => 'image/gif',
    -expires => '+3d',
    -cookie  => [$cookie1,$cookie2]
);
ysth
The point of the -flag notation is that it doesn't confuse the bareword with a Perl built-in. -values doesn't confuse anything with values(%hash).
brian d foy
The leading dashes here seem like a stylistic carryover from command line switches. They aren't necessary; the fat comma quotes barewords on its left side.
Michael Carman