Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Perl Context - concatenation operator

by prasadbabu (Prior)
on Dec 21, 2006 at 14:05 UTC ( #591106=perlquestion: print w/ replies, xml ) Need Help??
prasadbabu has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

Today while I was discussing with my colleague about Perl context behaviour, I found some strange behaviour as described below. I went through perdoc but I could not able to figure out. Could someone correct me where am I wrong.

$a = 1.2.3; print $a; #printing junk characters ##################### $a =1; $b =2; $c =3; print $a.$b.$c; #printing 123 ############## func (1, 2, 3); sub func { my ($a, $b, $c) = @_; print $a.$b.$c; #printing 123 } ##############

Why its printing junk characters, but not in subroutine?

Thanks in advance

Prasad

Comment on Perl Context - concatenation operator
Download Code
Re: Perl Context - concatenation operator
by chargrill (Parson) on Dec 21, 2006 at 14:10 UTC

    Perl is interpreting 1.2.3 as a vstring (version string), so it's trying to print chr(1) . chr(2) . chr(3) ... which is printing ^A^B^C, which doesn't print nicely :-)



    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
Re: Perl Context - concatenation operator
by Herkum (Parson) on Dec 21, 2006 at 14:14 UTC

    You are doing a vstring assignment here. The . characters are not concatenating your digits together like you thought.

    $a = 1.2.3; print $a; #printing junk characters

    If you were using strings then it would do it but not numbers. This is what you were thinking would happen.

    $a = "1"."2"."3"; print $a;

    A better example for working with a normal number would be this,

    $a = 1.20; print $a;

    1.20 is a proper number, would you expect it to be '1.20' or 120? Vstrings are a little different however.

      $a = "1"."2"."3";

      Just putting spaces is sufficient.
      >perl -e "print 1.2.3
      ☺☻♥
      >perl -e "print 1 . 2 . 3
      123
      
Re: Perl Context - concatenation operator
by BrowserUk (Pope) on Dec 21, 2006 at 14:16 UTC

    You've fallen foul of a deprecated perl feature, namely version strings (or v-strings). See perldata under the heading "Version Strings" for more information.

    Basically, a string of digits interspersed with '.'s (and no whitespace), is different from a series of digits being concatenated.

    This 1.2.3 is a version string and is equivalent to "\1\2\3".

    This 1 . 2 . 3 is the concatenation of those three digits. Eg"123"


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Not to be pedantic, but the node you're pointing at doesn't mention version strings. The perldata node you cite is for version 5.6.1; the deprecation notice you mention seems to originate in 5.8.

      Nonetheless, a deprecated but poorly documented feature is a sure trap for the uninitiated. I'm not personally a total n00b, but I've never heard of vstrings. I think my editing habits (always quoting strings, for instance) have kept me from stumbling into this particular piece of syntactic sugar poison, but it doesn't take much imagination to see how it could happen.

        Not to be pedantic, but the node you're pointing at doesn't mention version strings. The perldata node you cite is for version 5.6.1;

        Yes, but ... the very first link on that page leads directly to the latest docs, and the second line warns that the PM local copy is out of date.

        Quite why the links don't just forward you to the appropriate place ... then a great gob of PM disk space could be recovered. Maybe it's because the "appropriate place" has a habit of changing. It was perldoc.org for a while, but that was down more often than a tart's knickers. It now seems to be perldoc.perl.org. There is probably a shortcut thingy to take you directly there, but I can never remember where to go to look them up!


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Perl Context - concatenation operator
by shmem (Canon) on Dec 21, 2006 at 14:21 UTC
    As chargrill<update>, Herkum and BrowserUK</update> says, a bunch of numbers with more than one dot in between don't evaluate to what you expected. That could also be a packed IP address (see gethostbyname):
    perl -le '$lo = 127.0.0.1; $l = (gethostbyname("localhost"))[4];print +"yup" if $lo eq $l' yup

    Anyways, it's a packed structure of bytes. To get back what you expected, use the %v format directive of sprintf:

    perl -le 'printf "%vd\n", (gethostbyname("localhost"))[4]' 127.0.0.1

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Perl Context - concatenation operator
by Anonymous Monk on Dec 22, 2006 at 09:39 UTC
    the line $a = 1.2.3; can't be interpreted as a number because of improper format, so perl uses it's definition of '.' operation - concatenates strings that are represented by your 1,2 and 3, which in hex code give you your 'junk' strings. in your other cases it just treats variables as strings, and concatenates them
Re: Perl Context - concatenation operator
by alpha (Scribe) on Dec 22, 2006 at 12:20 UTC
    This is v-strings.
Re: Perl Context - concatenation operator
by logie17 (Friar) on Dec 22, 2006 at 16:29 UTC
    Consequently the following code output is the same:
    print 84.69.83.84.10.13; # Same as print map chr($_), (84, 69, 83, 84, 10, 13);
    s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;
Re: Perl Context - concatenation operator
by druud (Sexton) on Dec 22, 2006 at 16:35 UTC
    perl -wle ' $x = 80.101.114.108; print $x '

    -- 
    Ruud

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://591106]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2014-09-18 02:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (104 votes), past polls