Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

chomp it chomp it good

by Anonymous Monk
on Jun 03, 2009 at 04:51 UTC ( #767884=perlquestion: print w/replies, xml ) Need Help??

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

Simple question. How does chomp change the contents of the string without the need to pass it in by reference?


ex:
$/="!"; $chomper = "chomp! chomp! chomp!"; chomp($chomper); print $chomper; #outputs chomp! chomp! chomp

....but notice I didn't need to pass in \$chomper for a reference to the scalar and its value changed anyway. How is this done?

Replies are listed 'Best First'.
Re: chomp it chomp it good
by ikegami (Pope) on Jun 03, 2009 at 05:21 UTC

    How does chomp change the contents of the string without the need to pass it in by reference?

    Aside from the fact that chomp is an operator (so special rules could apply), arguments are always passed by reference in Perl.

    sub foo { $_[0] = 'def'; } my $x = 'abc'; foo($x); print("$x\n"); # def

    You seem to be confusing passing by reference (the term you used and the reason it works) and passing references to variables (something unrelated).

Re: chomp it chomp it good
by AnomalousMonk (Bishop) on Jun 03, 2009 at 05:25 UTC
    All parameters passed via  @_ are passed by reference; it's Perl's normal way of doing business. Consider:
    >perl -wMstrict -le "my $i = 42; inc($i); print $i; sub inc { ++$_[0] } " 43
    See the third paragraph in the DESCRIPTION section of perlsub.
Re: chomp it chomp it good
by sflitman (Hermit) on Jun 03, 2009 at 05:26 UTC
    It uses Magic.

    If you check the perl source, you'll see that it is implemented as Perl_do_chomp in doop.c and it takes a pointer to an SV (which is fed to it from the top of the stack down since chomp can take multiple arguments) and that pointer is the C equivalent of a reference.

    St. Larry be praised, there is much magic to make our lives easier in Perl.

    HTH,
    SSF

      It will be great if you can post the code snippet of the c code..
        Here's an analogy:
        my $bar = 41; foo($bar); print $bar; use Inline C => <<'EOC'; void foo(SV* bar) { int b = (int)SvIV(bar); b++; sv_setiv(bar,b); } EOC
Re: chomp it chomp it good
by vinoth.ree (Monsignor) on Jun 03, 2009 at 05:12 UTC

    The chomp function works in combination with the $/ variable when reading from filehandles.

    The $/ variable is the input record separator that is attached to the records you read from a filehandle and it is by default set to the newline character "\n".

    The chomp function works by removing the last character from a string only if it matches the value of $/.

    See Chomp

    Vinoth,G
      The newline character is \n, note the \.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2020-01-21 04:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?