Strange Perl line : Return the result of a function to a function

Discussion in 'Perl Misc' started by AlexHWGUY, Oct 24, 2006.

  1. AlexHWGUY

    AlexHWGUY Guest

    I never done a Perl code before but I done some C, PHP, JAVA, etc...
    Peel looked very similar before I failed to understand those lines :

    substr($y, 5, 2) = substr($y, 4, 2);
    substr($y, 4, 1) = ".";

    How that possible ? It is replacing the 2 char at pos 5 with 2 char at
    pos 4 ?

    Thanks to help me put my eyes back in their hole.
    AlexHWGUY, Oct 24, 2006
    #1
    1. Advertising

  2. AlexHWGUY wrote:
    > I never done a Perl code before but I done some C, PHP, JAVA, etc...
    > Peel looked very similar before I failed to understand those lines :
    >
    > substr($y, 5, 2) = substr($y, 4, 2);
    > substr($y, 4, 1) = ".";
    >
    > How that possible ? It is replacing the 2 char at pos 5 with 2 char at
    > pos 4 ?
    >
    > Thanks to help me put my eyes back in their hole.
    >


    It does indeed look strange, but (from "perldoc -f substr"):

    "You can use the substr() function as an lvalue, in which case EXPR must
    itself be an lvalue."

    So it works as designed.

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Oct 24, 2006
    #2
    1. Advertising

  3. Josef Moellers <> wrote:
    > AlexHWGUY wrote:
    >> I never done a Perl code before but I done some C, PHP, JAVA, etc...
    >> Peel looked very similar before I failed to understand those lines :
    >>
    >> substr($y, 5, 2) = substr($y, 4, 2);
    >> substr($y, 4, 1) = ".";
    >>
    >> How that possible ? It is replacing the 2 char at pos 5 with 2 char at
    >> pos 4 ?
    >>
    >> Thanks to help me put my eyes back in their hole.
    >>

    >
    > It does indeed look strange, but (from "perldoc -f substr"):
    >
    > "You can use the substr() function as an lvalue, in which case EXPR must
    > itself be an lvalue."
    >
    > So it works as designed.



    And if you use the 4-argument form of substr(), then your eyeballs will
    stay in their sockets. :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 24, 2006
    #3
  4. On 2006-10-24 13:19, AlexHWGUY <> wrote:
    > I never done a Perl code before but I done some C, PHP, JAVA, etc...
    > Peel looked very similar before I failed to understand those lines :
    >
    > substr($y, 5, 2) = substr($y, 4, 2);
    > substr($y, 4, 1) = ".";
    >
    > How that possible ? It is replacing the 2 char at pos 5 with 2 char at
    > pos 4 ?


    Yes.

    That form is now by many considered deprecated, but I wonder whether the
    alternative

    substr($y, 5, 2, substr($y, 4, 2));
    substr($y, 4, 1, ".");

    is really more readable. At least the assignment makes it clear what is
    replaced (you obviously found the right answer without checking the
    manual), while for the 4-argument form you have to look up the manual to
    find out that there is a replacement at all.

    hp

    PS: For replacing parts of a string I prefer the s/// operator, even if
    it is somewhat slower.

    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
    Peter J. Holzer, Oct 24, 2006
    #4
  5. AlexHWGUY

    Ingo Menger Guest

    Peter J. Holzer wrote:
    > On 2006-10-24 13:19, AlexHWGUY <> wrote:
    > > I never done a Perl code before but I done some C, PHP, JAVA, etc...
    > > Peel looked very similar before I failed to understand those lines :
    > >
    > > substr($y, 5, 2) = substr($y, 4, 2);
    > > substr($y, 4, 1) = ".";
    > >
    > > How that possible ? It is replacing the 2 char at pos 5 with 2 char at
    > > pos 4 ?

    >
    > Yes.
    >
    > That form is now by many considered deprecated, but I wonder whether the
    > alternative
    >
    > substr($y, 5, 2, substr($y, 4, 2));
    > substr($y, 4, 1, ".");
    >
    > is really more readable.


    Full ACK. After all, people are used to read and write things like

    $a[2] = 42;

    without ever noticing that the construct on the left hand side means
    something totally different when it is used on the right hand side.
    Ingo Menger, Oct 24, 2006
    #5
  6. AlexHWGUY wrote:
    > I never done a Perl code before but I done some C, PHP, JAVA, etc...
    > Peel looked very similar before I failed to understand those lines :
    >
    > substr($y, 5, 2) = substr($y, 4, 2);
    > substr($y, 4, 1) = ".";
    >
    > How that possible ? It is replacing the 2 char at pos 5 with 2 char at
    > pos 4 ?


    Yes.

    That could also be written as:

    substr $y, 4, 3, '.' . substr $y, 4, 2;


    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Oct 24, 2006
    #6
    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. Replies:
    4
    Views:
    532
    James Kanze
    Jul 22, 2008
  2. J.Ram
    Replies:
    7
    Views:
    642
  3. Pavel
    Replies:
    7
    Views:
    515
    Pavel
    Sep 19, 2010
  4. Lakshmi Sreekanth

    i = 10; result = ++i - --i; How result become ZERO

    Lakshmi Sreekanth, Sep 21, 2010, in forum: C Programming
    Replies:
    52
    Views:
    1,151
    Nick Keighley
    Sep 23, 2010
  5. Michael Tan
    Replies:
    32
    Views:
    940
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page