Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

What does $, = " " do in this code?

by alwynpan (Acolyte)
on Oct 31, 2016 at 06:46 UTC ( [id://1174995]=perlquestion: print w/replies, xml ) Need Help??

alwynpan has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Today is my first day in the Perl world and have tons of questions. My questions could be silly, please help :)
package DUMPVAR; sub dumpvar { my ($packageName) = @_; local (*alias); # a local typeglob # We want to get access to the stash corresponding to the package # name *stash = *{"${packageName}::"}; # Now %stash is the symbol table $, = " "; # Output separator for print # Iterate through the symbol table, which contains glob values # indexed by symbol names. while (($varName, $globValue) = each %stash) { print "$varName ============================= \n"; *alias = $globValue; if (defined ($alias)) { print "\t \$$varName $alias \n"; } if (defined (@alias)) { print "\t \@$varName @alias \n"; } if (defined (%alias)) { print "\t \%$varName ",%alias," \n"; } } }
I am not quite understand what does $, = " "; do in this package. Thank you.

Replies are listed 'Best First'.
Re: What does $, = " " do in this code?
by haukex (Archbishop) on Oct 31, 2016 at 07:34 UTC

    Hi alwynpan,

    The special variable $, controls what print outputs between its arguments. See also $" and $\.

    local $" = "a"; # list separator local $, = "c"; # output field separator local $\ = "l"; # output record separator my @x = ('F','O'); print @x, "@x", "x"; # prints "FcOcFaOcxl"

    So $" controls what is inserted between elements when the array @x is interpolated into a string. Normally, it'd turn out to be "F O", but since we've redefined $", we get "FaO".

    Next, $, controls what gets output between the arguments of print. Since the argument list is "flattened" and the elements of the array @x each become an argument to the function, the above is as if we had written print "F", "O", "FaO", "x" (this is a little bit of a simplification but good enough in this example). So the value of $, is inserted in between every argument.

    Lastly, $\ is simply what print outputs at the end of the argument list. It can be useful to set it to something like "\n", but note that this affects every print statement, which is why doing so is not always a good idea in larger programs. That's also why I've used local above, because that'll limit the effect of the assignment to the current dynamic scope only.

    As you can see variable names can be special characters, but Perl already uses a lot of them for special meanings, so you shouldn't try and make your own! Just use the ones Perl provides, when you need to. See perlvar. Update: Also, shmem makes an excellent point!

    By the way, the code you posted shows some fairly advanced techniques, I would not recommend it as something to study for your "first day"!

    Hope this helps,
    -- Hauke D

      By the way, the code you posted shows some fairly advanced techniques, I would not recommend it as something to study for your "first day"!
      I could not agree more (++), that was exactly what I was going to say if you hadn't.
      Thank you so much for your detailed explanation. Cheers.
Re: What does $, = " " do in this code?
by shmem (Chancellor) on Oct 31, 2016 at 18:16 UTC

    What others have said, yes. What they didn't address is the fact that $, - like any other special variable - is a global variable.

    So, this code is badly behaved because it changes this variable's content for the caller also, from the first invocation of this subroutine until the end of it's runtime. What it should do would be at least:

    sub dumpvar { ... my $saved_sep = $,; $, = " "; # Iterate through the symbol table, which contains glob values ... $, = $saved_sep; # restore original value }

    or, more concise

    local $, = " "; # localize the the change to this scope

    See local.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: What does $, = " " do in this code?
by Phenomanan (Monk) on Oct 31, 2016 at 14:58 UTC

    Incidentally, # Output separator for print is actually exactly what it is. Good commenting makes me feel all warm and fuzzy inside. :)

Re: What does $, = " " do in this code?
by Anonymous Monk on Oct 31, 2016 at 06:49 UTC

    I am not quite understand what does $, = " "; do in this package. Thank you.

    Any guesses?

      If @alias = qw(a b c);, without  $, = " ";, it will print as # abc rather than # a b c. Correct?

        If @alias = qw(a b c);, without $, = " ";, it will print as # abc rather than # a b c. Correct?

        What happens when you try it?

      Not really. First thought was it is a variable name, but obviously it is not, since the variable name cannot be "," from my memory. Anything to do with regular expression?

        Not really. First thought was it is a variable name, but obviously it is not, since the variable name cannot be "," from my memory. Anything to do with regular expression?

        What is this "from my memory"? Is that perldoc://?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1174995]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-19 05:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found