anonymous hash notation question

W

wana

what is the difference between:

{a=>b, c=>d}

and

{-a=>b, -c=>d}

?

Specifically, what is the significance of the dash?
 
G

Gunnar Hjalmarsson

wana said:
what is the difference between:

{a=>b, c=>d}

and

{-a=>b, -c=>d}

?

None of them compiles if you have strictures enabled (which you really
should have!).

Why are you asking?
Specifically, what is the significance of the dash?

Now I understand even less why you are asking. Isn't it obvious how
the string 'a' differs from the string '-a'?
 
W

wana

Gunnar Hjalmarsson said:
None of them compiles if you have strictures enabled (which you really
should have!).

Why are you asking?


Now I understand even less why you are asking. Isn't it obvious how
the string 'a' differs from the string '-a'?

Oh, that's what it is? I was wondering about it because in some
classes, the examples show their properties being set with hashes with
keys that begin with a dash as shown above. I thought it might be an
operator that only acts as an operator in that particular situation or
something like that. It didn't occur to me that it was just a
different way to write a variable name. Sorry. You have to admit
that the language sets people up for a little confusion the way that
the same symbol has different meanings in different contexts. For
example, the '?' in regular expressions.
 
G

Gunnar Hjalmarsson

wana said:
Oh, that's what it is? I was wondering about it because in some
classes, the examples show their properties being set with hashes
with keys that begin with a dash as shown above. I thought it
might be an operator that only acts as an operator in that
particular situation or something like that. It didn't occur to me
that it was just a different way to write a variable name.

I thought they were keys in a couple of anonymous hashes, not variable
names...
Sorry. You have to admit that the language sets people up for a
little confusion the way that the same symbol has different
meanings in different contexts. For example, the '?' in regular
expressions.

Yeah, there is a lot to learn. :) http://learn.perl.org/

Btw, "context", as you mentioned, has a special significance in Perl,
so that's one of the first things you should get an understanding of
if you want to learn Perl.
 
C

ChrisO

Abigail said:
wana ([email protected]) wrote on MMMMXXXV September MCMXCIII in
<URL:&& what is the difference between:
&&
&& {a=>b, c=>d}
&&
&& and
&&
&& {-a=>b, -c=>d}
&&
&& ?
&&
&& Specifically, what is the significance of the dash?


Do you understand the difference between:

{a => b, c => d}

and

{qa => b, qc => d}

?

Do you understand the significance of the q?

I don't think this at least slightly sarcastic response is a fair answer
to the question. I had been programming in Perl for quite some time
before I realized that the dashed hash arguments some module writers
expected in their method calls was just that -- no different than "qa"
and that all they were doing was inventing their own "funky" notation
(somehow, I guess "paralleling" switched arguments.)

Perl, while powerful, can be MAJORLY "quirky" at times. There is no
"standard" per se for module writing. There are WRITTEN "standards" on
CPAN, but Perl by itself doesn't enforce those standards, and anyone can
write a module to take arguments however they want. I've seen all KINDS
of ways to pass arguments to module method calls.

And with "-" in most languages, not just Perl, usually having at least
one special meaning (negation, urinary significance, etc.), someone with
experience in another language, but only superficial experience in Perl
would AUTOMATICALLY think there *has* to be something significant with
"-a" and no way would they think it's really no different than "qa" --
which it just happens to be...

I think sometimes people in this NG are too unforgiving for those new to
the language. I've just gone around and around with some folks in
another newsgroup about these little so-called "flakey things" in Perl,
and while I at first didn't believe them or feel their arguments (no pun
intended) had merit, I came to see that many of their complaints did
have at least *some* merit. I'm just used to Perl syntax so I "see" it
differently when I scan Perl code. But some people, especially new to
the language, don't "see" it immediately. So give 'em a break...

And realize that there are actually people just starting to learn Perl
for the first time. We should be happy this is the case instead of
making them feel like idiots, esp. over something like "-a" which ISN'T
SO OBVIOUS after all and ISN'T really documented very obviously (if at
all even in the modules that call for arguments that way -- even the
module writers "assume" we know "-a" is no different than "qa".)

None of this to say there aren't A TON of stupid questions posted in
c.l.p.m every day -- questions that SHOULD and COULD be researched WAY
better than they are before posting. But this question was fair; I know
of no place off hand that this "behavior" is CLEARLY documented; the
assumption is made that we should know "-a" = "qa". And THAT'S quirky.

-ceo
 
C

ChrisO

ChrisO said:
And with "-" in most languages, not just Perl, usually having at least
one special meaning (negation, urinary significance, etc.), someone with

I about died laughing when I read this over again after posting it. I
hope no one "pees" on me for spelling "unary" as "urinary" although the
OP probably felt "peed" on and I just was reflecting that impression.

Two phunny...

-ceo
 
K

Keith Keller

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You have to admit
that the language sets people up for a little confusion the way that
the same symbol has different meanings in different contexts.

You mean, like many natural languages?

- --keith

- --
(e-mail address removed)-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBS8M9hVcNCxZ5ID8RAiOIAJ4/IP06ocsI05625QrqndgleyjvYQCgl4g4
l0M0qkQCq5pY0IEcLzV5BZQ=
=TWxh
-----END PGP SIGNATURE-----
 
P

Peter Scott

what is the difference between:

{a=>b, c=>d}

and

{-a=>b, -c=>d}

?

Specifically, what is the significance of the dash?

I believe this extract from the perlop man page addresses
your question:

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 "-bareword".

I also found the usage confusing until I learned of the special
case.
 
W

wana

ChrisO said:
I about died laughing when I read this over again after posting it. I
hope no one "pees" on me for spelling "unary" as "urinary" although the
OP probably felt "peed" on and I just was reflecting that impression.

Two phunny...

-ceo

Thanks for your help. I know I am a relative beginner, but even with
my superficial understanding of the language, I have been able to
write some useful code. I can use the format for passing data to
objects without understanding it fully, but I like to understand what
I am doing. My reference books are 'Learning Perl' and 'Perl in a
Nutshell'. I am currently saving up for 'Programming Perl' (I got the
other two on sale).

I searched google and couldn't find anything on the subject, probably
because it is hard to look up stuff relating to the use of symbols. I
didn't totally get the concept that -a = qa, so I did a little
experiment with a method from CGI.pm:

input/output

textfield({-name=>'text'}) creates <input type="text" name="text" />
#with the -

textfield({name=>'text'}) creates <input type="text" name="text" />
#without the -

textfield({qname=>'text'}) creates <input type="text" name=""
qname="text" /> #replace - with q

textfield({q(name)=>'text'}) creates <input type="text" name="text"
/> # replace - with q(name)

by -a = qa, did you mean q as in quoting, like qw?

I am probably completely off on this one. Can you recommend a good
book that might address the issue? I did lots of searches regarding
OO in Perl, anonymous hashes, module construction etc...

Thanks again!
 
G

Gunnar Hjalmarsson

wana said:
I searched google and couldn't find anything on the subject, probably
because it is hard to look up stuff relating to the use of symbols. I
didn't totally get the concept that -a = qa,

It's not. As Chris pointed out, Abigails post was sarcastic.

The '-' is not a symbol in this context. '-a' is nothing but a string
that begins with a dash followed by the character 'a'. 'a', '-a' and
'qa' are simply three different strings.
I am probably completely off on this one. Can you recommend a good
book that might address the issue?

There is no issue to be addressed.

Or does Peter's quote from "perldoc perlop" help you?
 
W

wana

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 "-bareword".

I also found the usage confusing until I learned of the special
case.

Thank you, thank you!
 
S

Sherm Pendley

Gunnar said:
It's not. As Chris pointed out, Abigails post was sarcastic.

The '-' is not a symbol in this context. '-a' is nothing but a string
that begins with a dash followed by the character 'a'. 'a', '-a' and
'qa' are simply three different strings.

To put it another way: The behavior that you've observed is part of the
CGI module, which recognizes and removes a leading '-' from keys used as
method arguments. Other modules are not required to do the same, and
many in fact do not.

To the core Perl language, those are just ordinary hash keys, nothing
special at all - '-b' is no more special than 'qb'.

sherm--
 
B

Brian McCauley

Gunnar said:
The '-' is not a symbol in this context. '-a' is nothing but a string
that begins with a dash followed by the character 'a'. 'a', '-a' and
'qa' are simply three different strings.

This is incorrect.

The '-' in this case is a unary operator.

The => causes the word (thats just the 'a') to the left to be quoted so

-a => 'b'

is interpreted as

- 'a' => 'b'

The unary minus applied to a string prepends a minus (unless the string
already starts with a minus - in which case it removes it).
 
G

Gunnar Hjalmarsson

Brian said:
This is incorrect.

The '-' in this case is a unary operator.

So it seems; thanks for pointing it out. The reason why I didn't
realize it until now is probably that you need rather weird examples
to observe it, such as:

my %hash = (-'-a' => 'b');
print keys %hash, "\n";

Outputs:
+a
The => causes the word (thats just the 'a') to the left to be
quoted so

-a => 'b'

is interpreted as

- 'a' => 'b'

The unary minus applied to a string prepends a minus (unless the
string already starts with a minus - in which case it removes it).

Actually, the latter seems to also be incorrect. It doesn't remove it,
but it replaces it with a plus.

print join "\n", -'-one', -'+two';

Outputs:
+one
-two
 
B

Bo Lindbergh

I believe this extract from the perlop man page addresses
your question:

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 "-bareword".

I also found the usage confusing until I learned of the special
case.

The exact meaning of "is numeric" is still confusing. Example:

sub analyze (@)
{
foreach (@_) {
print STDERR "Negate\t",$_,"\tto get\t",-$_,"\n";
}
}

my($pos,$neg,$num,$bare)=('+1.0','-1.0','1.0','zero');

analyze($pos,$neg,$num,$bare);
print "Add them all to get\t",$pos+$neg+$num+$bare,"\n";
analyze($pos,$neg,$num,$bare);
 
P

Peter Scott

The exact meaning of "is numeric" is still confusing. Example:

sub analyze (@)
{
foreach (@_) {
print STDERR "Negate\t",$_,"\tto get\t",-$_,"\n";
}
}

my($pos,$neg,$num,$bare)=('+1.0','-1.0','1.0','zero');

analyze($pos,$neg,$num,$bare);
print "Add them all to get\t",$pos+$neg+$num+$bare,"\n";
analyze($pos,$neg,$num,$bare);

Yes, it is possible to play tricks with Perl's DWIMmer.
Fortunately, real programs that fall afoul of this
behavior accidentally are extremely rare. I tried to
imagine how that could happen and gave up.

(Your example is even stronger without $bare, because
that does at least trigger a "not numeric" warning on
the first usage.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top