why not print $A[0]

M

Mark Galecki

Hello,

I am new to Perl. The Perl FAQ, referenced in this newsgroup, says
"only simple scalar variables, not expressions or subscripts into
hashes or arrays, can be used with built-ins like print"

Where is this explained in the Camel book? Or (equivalent question)
Why not?

(background: I have been trying to do
print $A[0] "foo";
and that would not even compile, where
$a = $A[0]; print $a "foo";
works fine, and also
close $A[0];
works. So I just don't get it, why not, and I want to see this
explained in the Camel book - can't find it there so far...).

thank you

Mark Galeck
 
G

Gunnar Hjalmarsson

Mark said:
The Perl FAQ, referenced in this newsgroup, says "only simple scalar
variables, not expressions or subscripts into hashes or arrays, can
be used with built-ins like print"

Where in the FAQ did you read that?
Where is this explained in the Camel book? Or (equivalent question)
Why not?

How about giving some context...
(background: I have been trying to do
print $A[0] "foo";
and that would not even compile,

This works fine:

my @A;
open $A[0], '> file';
print { $A[0] } "foo\n";
close $A[0];

You just need to take care of the ambiguity.
 
S

Stuart Moore

Gunnar said:
This works fine:

my @A;
open $A[0], '> file';
print { $A[0] } "foo\n";
close $A[0];

You just need to take care of the ambiguity.

OOI, how is this any more ambiguous than print $a "foo\n";
 
S

Stuart Moore

Gunnar said:
This works fine:

my @A;
open $A[0], '> file';
print { $A[0] } "foo\n";
close $A[0];

You just need to take care of the ambiguity.

OOI, how is

print $a[0] "foo\n";

any more ambiguous than

print $a "foo\n";
 
G

Gunnar Hjalmarsson

Stuart said:
Gunnar said:
This works fine:

my @A;
open $A[0], '> file';
print { $A[0] } "foo\n";
close $A[0];

You just need to take care of the ambiguity.

OOI, how is

print $a[0] "foo\n";

any more ambiguous than

print $a "foo\n";

Guess you'd better check the sorce code of the print() function to find
out. ;-)
 
B

Brian McCauley

Stuart said:
Gunnar said:
This works fine:

my @A;
open $A[0], '> file';
print { $A[0] } "foo\n";
close $A[0];

You just need to take care of the ambiguity.

OOI, how is

print $a[0] "foo\n";

any more ambiguous than

It isn't really. It's an artifact of the parser.
 
M

Mark Galecki

OK, I am sorry about the apparent lack of context. I purposely
omitted some details which I thought were irrelevant, to not waste
your time. I guess I overdid it.

So what I can understand from the responses, is that I have to do

print {$A[0]} "foo\n";

or
$a = $A[0]; print $a "foo\n";

but not
print $A[0] "foo\n";

because of the way the Perl parser works. OK I can accept that.


But please, can someone point me to where this is written down in the
Camel book? (I really tried to find it there ). Or is it?

Thank you

Mark
 
C

ctcgag

Stuart Moore said:
Gunnar said:
This works fine:

my @A;
open $A[0], '> file';
print { $A[0] } "foo\n";
close $A[0];

You just need to take care of the ambiguity.

OOI, how is

print $a[0] "foo\n";

any more ambiguous than

print $a "foo\n";

In your case it is obvious what is intended, but it may not always
be:
print $a [0],{1}

Does this print two stringified references to filehandle $a, or one
stringified reference to file handle $a[0]?

I guess parser programmers decided there was too much of a limit to their
ability to decern what you meant in this context, so required explicitness.

Xho
 
P

Paul Lalli

Stuart Moore said:
OOI, how is

print $a[0] "foo\n";

any more ambiguous than

print $a "foo\n";

In your case it is obvious what is intended, but it may not always
be:
print $a [0],{1}

Does this print two stringified references to filehandle $a, or one
stringified reference to file handle $a[0]?

It can't be the second one, since a comma is illegal after the
filehandle in a print function. The ambiguity is whether it prints the
two refs to $a or the ref {1} and the element $a[0] to STDOUT.

(fwiw, perl resolves this to the second option I listed).

Paul Lalli
 
G

Gunnar Hjalmarsson

Mark said:
So what I can understand from the responses, is that I have to do

print {$A[0]} "foo\n";

or
$a = $A[0]; print $a "foo\n";

but not
print $A[0] "foo\n";

because of the way the Perl parser works. OK I can accept that.

But please, can someone point me to where this is written down in the
Camel book? (I really tried to find it there ).

Why are you so anxious to find it in the Camel, considering that it's
very clearly explained in the latter versions of

perldoc -f print
Or is it?

I didn't find it in my copy. But, again, the Camel isn't the first place
to look for the syntax of a specific built-in function.
 
B

Ben Morrow

Quoth (e-mail address removed) (Mark Galecki):
OK, I am sorry about the apparent lack of context. I purposely
omitted some details which I thought were irrelevant, to not waste
your time. I guess I overdid it.

So what I can understand from the responses, is that I have to do

print {$A[0]} "foo\n";

or
$a = $A[0]; print $a "foo\n";

but not
print $A[0] "foo\n";

because of the way the Perl parser works. OK I can accept that.

But please, can someone point me to where this is written down in the
Camel book? (I really tried to find it there ). Or is it?

Where expected, under print on p766; the general discussion of 'indirect
object syntax' is in the objects section on p314 (3rd ed.).

Ben
 
M

Mark Galecki

Why are you so anxious to find it in the Camel, considering that it's
very clearly explained in the latter versions of

perldoc -f print

Well because ..., the way my brain works, it likes to have all things
completely explained in some sort of a reference, and everybody says
the Camel book were such. I downloaded Perl from Cygwin (I know I
know, Windows, well, that's what my employer uses), and they don't
even have "perldoc".
 
G

Gunnar Hjalmarsson

Mark said:
Well because ..., the way my brain works, it likes to have all things
completely explained in some sort of a reference, and everybody says
the Camel book were such.

Well, it is ... or was. This is a case where the docs have been changed
since the latest edition of the Camel.
 
P

Paul Lalli

Mark Galecki said:
I downloaded Perl from Cygwin (I know I
know, Windows, well, that's what my employer uses), and they don't
even have "perldoc".

Nonsense. perldoc is a standard tool that comes with Perl, including
the Cygwin version of Perl. I don't recall offhand if it's listed as a
seperate package in Cygwin's setup.exe utility, but it's definately on
my installation.

What happens when you type (for example)
perldoc perl
in your cygwin shell?

Paul Lalli
 
M

Mark Galecki

But please, can someone point me to where this is written down in the
Where expected, under print on p766; the general discussion of 'indirect
object syntax' is in the objects section on p314 (3rd ed.).

Indeed. Right you are. Perhaps it's a case of temporary blindness?
 
M

Mark Galecki

Paul Lalli said:
Nonsense. perldoc is a standard tool that comes with Perl, including
the Cygwin version of Perl. I don't recall offhand if it's listed as a
seperate package in Cygwin's setup.exe utility, but it's definately on
my installation.

What happens when you type (for example)
perldoc perl
in your cygwin shell?

Paul Lalli

Well, I see, what happens is that Cygwin installs perl as perl.exe,
but perldoc and perldoc (no exe) and DOS shell does not recognize
that. Now in Cygwin shell, the output of
$perldoc perl

is a bit mangled - starts with :


PERL(1) User Contributed Perl Documentation
PERL(1)



ESC[1mNAMEESC[0m
perl - Practical Extraction and Report Language

ESC[1mSYNOPSISESC[0m
ESC[1mperl ESC[22m[ ESC[1m-sTuU ESC[22m] [ ESC[1m-hv ESC[22m] [
ESC[1m-V

(...)

But that is OK, I already found my answer in the Camel book. Thank
you.
 

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
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top