can i treat a subroutine call as an array?

D

doolittle

Hi,

The 4th line of the following code fragment is a syntax error.

my @size = getvalue('SZ');
print $size[0]."\n"; #OK
print @{$sgf->SZ}[0]."\n"; #OK
print getvalue('SZ')[0]."\n"; #sysntax error

sub getvalue {
my $propertyname = shift;
return @{$sgf->$propertyname}
}

Can i re-phrase the 4th line in some clever way, or am i stuck with the
methods on lines 1 or 2?

Dan
 
P

Paul Lalli

doolittle said:
The 4th line of the following code fragment is a syntax error.

my @size = getvalue('SZ');
print $size[0]."\n"; #OK
print @{$sgf->SZ}[0]."\n"; #OK
print getvalue('SZ')[0]."\n"; #sysntax error

sub getvalue {
my $propertyname = shift;
return @{$sgf->$propertyname}
}

Can i re-phrase the 4th line in some clever way, or am i stuck with the
methods on lines 1 or 2?

print ((getvalue('SZ'))[0] . "\n");

Paul Lalli
 
X

xhoster

doolittle said:
Hi,

The 4th line of the following code fragment is a syntax error.

my @size = getvalue('SZ');
print $size[0]."\n"; #OK
print @{$sgf->SZ}[0]."\n"; #OK
print getvalue('SZ')[0]."\n"; #sysntax error

sub getvalue {
my $propertyname = shift;
return @{$sgf->$propertyname}
}

Can i re-phrase the 4th line in some clever way, or am i stuck with the
methods on lines 1 or 2?

In addition to Michele's answer to prettisize #1, you could also
prettisize #2:

print $sgf->SZ->[0], "\n";

Xho
 
D

doolittle

Michele said:
print +(getvalue('SZ'))[0]."\n";
print +(getvalue 'SZ')[0]."\n"; # if predeclared

BTW, you can also

y;.;,;;

Thanks for that.

Sorry to be a pest but why does the +(..) work?

just point me to the relevant docs, (i couldn't find anything under
additive operators)

is this a way of saying i could replace . with , if i wanted?

dan
 
X

xhoster

doolittle said:
Michele said:
print +(getvalue('SZ'))[0]."\n";
print +(getvalue 'SZ')[0]."\n"; # if predeclared

BTW, you can also

y;.;,;;

Thanks for that.

Sorry to be a pest but why does the +(..) work?

It forces the paranthesis to not be "consumed" by the print
function being interpreted as a function
just point me to the relevant docs, (i couldn't find anything under
additive operators)

Look in perldoc perldiag for something like:
print (...) interpreted as function

(And of course, follow the references you provides)

is this a way of saying i could replace . with , if i wanted?

Yep. If you don't know why it is a Perl geekish way of saying that then:

perldoc -f y

y/// The transliteration operator. Same as "tr///".
See perlop.

Xho
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top