can i treat a subroutine call as an array?

Discussion in 'Perl Misc' started by doolittle, Nov 13, 2006.

  1. doolittle

    doolittle Guest

    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
     
    doolittle, Nov 13, 2006
    #1
    1. Advertising

  2. doolittle

    Paul Lalli Guest

    doolittle wrote:
    > 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
     
    Paul Lalli, Nov 13, 2006
    #2
    1. Advertising

  3. doolittle

    Guest

    "doolittle" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Nov 13, 2006
    #3
  4. doolittle

    doolittle Guest

    Michele Dondi wrote:
    >
    > 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)

    > y;.;,;;


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

    dan
     
    doolittle, Nov 13, 2006
    #4
  5. doolittle

    Guest

    "doolittle" <> wrote:
    > Michele Dondi wrote:
    > >
    > > 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)


    > > y;.;,;;

    >
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Nov 13, 2006
    #5
  6. doolittle

    Scott Bryce Guest

    doolittle wrote:
    > 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)


    Look under print.

    http://perldoc.perl.org/functions/print.html

    The + is one way to get around the "If it looks like a function, it is a
    function" rule.
     
    Scott Bryce, Nov 13, 2006
    #6
  7. Scott Bryce <> wrote:
    > doolittle wrote:
    >> 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)

    >
    > Look under print.
    >
    > http://perldoc.perl.org/functions/print.html
    >
    > The + is one way



    A particularly poor way, when compared to the alternative way...


    > to get around the "If it looks like a function, it is a
    > function" rule.



    The best way is to actually use parenthesis around the function's
    argument list, even if you don't normally choose to put parens
    around a function's arg list.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 14, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeevan

    treat array as a file

    Jeevan, Sep 4, 2003, in forum: C Programming
    Replies:
    10
    Views:
    607
    Glen Herrmannsfeldt
    Sep 15, 2003
  2. Chakra

    Can we treat a handheld as a webserver ?

    Chakra, Aug 23, 2004, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    117
    Chakra
    Aug 23, 2004
  3. rickcasey
    Replies:
    14
    Views:
    248
    Anno Siegel
    Jan 25, 2005
  4. Replies:
    9
    Views:
    170
  5. king
    Replies:
    5
    Views:
    202
Loading...

Share This Page