Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Should chomping a constant always raise an error?

by BigLug (Chaplain)
on Mar 11, 2005 at 03:20 UTC ( [id://438519]=note: print w/replies, xml ) Need Help??


in reply to Should chomping a constant always raise an error?

Personally, it makes a lot of sense. Sure if there's no need to chomp a value then it shouldn't hurt. However the function *must* be assuming the input it to be modifyied in some way otherwise you'd not pass a value to it that you expect to be modified.
sub modify { chomp( $_[0] ); # whatever else happens, you've just potentially modified $_[0] } modify('fred'); # What would you expect to happen to 'fred'? my $f ="fred\n"; modify($f); print $f; #fred
sub modify2 { # Create a copy of the input my $input = $_[0]; chomp($input); } modify('fred'); # works now because we're not modifying the input, but a copy of it. my $f ="fred\n"; modify($f); print $f; #fred\n

Cheers!
Rick
If this is a root node: Before responding, please ensure your clue bit is set.
If this is a reply: This is a discussion group, not a helpdesk ... If the discussion happens to answer a question you've asked, that's incidental.

Replies are listed 'Best First'.
Re: can this be turned into a compile-time error?
by Thilosophy (Curate) on Mar 11, 2005 at 03:56 UTC
    sub modify { chomp( $_[0] ); # whatever else happens, you've just potentially modified $_[0] } modify('fred'); # What would you expect to happen to 'fred'?
    Great example. For those who did not try it out, it causes a runtime exception:
    Modification of a read-only value attempted at /tmp/t.pl line 2.
    Can modify somehow be declared to only take a modifiable parameter, so that we can have a compile-time error (as happens with the core chomp)?
    Can't modify constant item in modify at -e line 1, at end of line Execution of -e aborted due to compilation errors.
      Can modify somehow be declared to only take a modifiable parameter, so that we can have a compile-time error (as happens with the core chomp)?
      Playing Devil's Advocate, will this work?
      chomp(foo(",",@list))
      Don't know? How about this?
      chomp(join(",",@list))
      It seems, off the top of my naive head, that the argument to chomp would have to be an l-value to be guaranteed to be modifiable.

      Is that what you want?

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-25 19:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found