The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   get the value of a variable in a previous command, single line (http://hintsforums.macworld.com/showthread.php?t=106596)

eitchbar 10-27-2009 04:04 AM

Hmm… that doesn't fix it. In the example
Code:

open -a Preview
historical open -o a

replacing '$saughtOption=s' with 'a=s' leaves $historicalValue unitialized when it's referred to on the next line. Thought first it was the trailing newline on @ARGV, but chomping $historicalValue doesn't help the GetOptions.
Code:

my $historicalValue;

chomp($historicalUse);

if($onlyOption && $historicalUse) {
        @ARGV = $historicalUse;
#die "a\n@ARGV\nb\n"; #uncommenting confirms @ARGV is 'open -a Preview'
        GetOptions(
                'a=s'        => \$historicalValue
        );
        die "val: $historicalValue\n"; #error! $historicalValue unitialized!
}

Whatever, I'm done. It was fun, but now I'm just wasting time. Thanks to everyone for the ideas, especially Hayne- really nice of you take the time be my advisor

hayne 10-27-2009 05:07 AM

Quote:

Originally Posted by eitchbar (Post 559028)
Code:

#die "a\n@ARGV\nb\n"; #uncommenting confirms @ARGV is 'open -a Preview'
        GetOptions(
                'a=s'        => \$historicalValue
        );


Note that Perl's @ARGV does not (unlike argv in C++) have the name of the program in the zeroth position ($ARGV[0]).
So that "open" is misplaced in @ARGV.

But I don't see why that would prevent GetOptions from working.

Note also that it is (IMHO) slightly strange to be assigning a scalar to an array with a statement like:
@ARGV = $historicalUse;
I would either use parentheses: @ARGV = ($historicalUse);
or start with an empty array @ARGV=(); and then add the scalar with 'push': push(@ARGV, $historicalUse);
But I guess it works, so it's just a matter of style.

ganbustein 10-27-2009 07:35 AM

Quote:

Originally Posted by hayne (Post 559033)
Note also that it is (IMHO) slightly strange to be assigning a scalar to an array with a statement like:
@ARGV = $historicalUse;
I would either use parentheses: @ARGV = ($historicalUse);
or start with an empty array @ARGV=(); and then add the scalar with 'push': push(@ARGV, $historicalUse);
But I guess it works, so it's just a matter of style.

It works because it's the same thing. It's a common misconception that parentheses create lists in Perl. In most cases, they're only for overriding operator precedence. For example, because comma has a lower precedence than assignment, the statement
@a = $x, $y
is parsed as
(@a = $x), $y;
which is probably not what you want (and will give you a warning if you use warnings;). The warning is that $y is a useless evaluation in a void context, because it has no side-effects. Change it to have a side-effect, and the warning disappears:
@a = $x, ++$y;
parses as:
(@a = $x), (++$y);
Adding parentheses, as in:
@a = ($x, $y);
serves to change the grouping, but does not change the scalar-or-list nature of the assignment. What makes this a list assignment is the presence of a list on the left hand side of the assignment.

These statements:
@a = $x;
@a = ($x);
@a = (($x));
@a = ((($x)));
are all parsed identically except for the varying numbers of superfluous parentheses. In all these cases, the right hand side is evaluated in list context because the left hand side is a list.

A comma has different meanings in different contexts, so that in:
($x, $y) = @a;
it's the comma on the left hand side of an assignment that creates the list. The parentheses are there only to make the comma bind tighter than the assignment.

Parentheses are useful for delimiting an empty list, but they don't create one. You have to already be in list context for some other reason:
@a = ; # syntax error
@a = (); # assign an empty list
$x = @a; # assign 0, the length of the right hand side
$x = (); # assign nothing, not even an empty list, leaving $x uninitialized
@a = (7,8); # assign 2-element list
$x = @a; # assign 2, the length of the right hand side
$x = (7,8); # discard 7 (and warn), assign 8, nary a list in sight
In Perl, context is everything. The very meaning of an expression like "(7,8)" changes depending on its context. Learn to "follow the context", and Perl becomes less cryptic.


All times are GMT -5. The time now is 05:52 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.