[Kc] square brackets as parameter holders?

David Nicol davidnicol at gmail.com
Thu Mar 24 08:22:18 PST 2005


I was trying to pull the current year out of the localtime function and
I discovered that square brackets work as parentheses:


D:\FCBC_S~1\POPclient>perl -wle "print (localtime)[5]"
print (...) interpreted as function at -e line 1.
syntax error at -e line 1, near ")["
Execution of -e aborted due to compilation errors.

D:\FCBC_S~1\POPclient>perl -wle "print ((localtime)[5])"
print (...) interpreted as function at -e line 1.
105

D:\FCBC_S~1\POPclient>perl -wle "print((localtime)[5])"
105

D:\FCBC_S~1\POPclient>perl -wle "print localtime[5]"
5257172287022641

D:\FCBC_S~1\POPclient>perl -wle "print (localtime[5])"
print (...) interpreted as function at -e line 1.
5257172287022641

D:\FCBC_S~1\POPclient>perl -wle "print localtime"
315102421054820

D:\FCBC_S~1\POPclient>perl -wle "print (localtime[5])"
print (...) interpreted as function at -e line 1.
5257172287022641

D:\FCBC_S~1\POPclient>perl -wle "print(localtime)"
1015102421054820

D:\FCBC_S~1\POPclient>perl -wle "print(~~localtime(5))"
Wed Dec 31 18:00:05 1969

D:\FCBC_S~1\POPclient>perl -wle "print(~~localtime[5])"
Tue Sep 22 17:57:52 1970


anyone have an explanation?  is this specific to localtime or can anything
get invoked with args in [] instead of ()?


D:\FCBC_S~1\POPclient>perl -wle "sub onearg{shift() + 7};print(~~onearg[5])"
22892279

D:\FCBC_S~1\POPclient>perl -wle "sub onearg{shift() + 7};print(~~onearg(5))"
12

D:\FCBC_S~1\POPclient>perl -wle "sub onearg{shift() + 7};print(~~onearg([5]))"
22892279

what is going on?  am I passing an array reference, which is getting interpreted
as a number?


-- 
David L Nicol
"You don't know how to maintain a station wagon either!"


More information about the kc mailing list