Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Should chomping a constant always raise an error?

by BrowserUk (Patriarch)
on Mar 11, 2005 at 05:23 UTC ( #438538=note: print w/replies, xml ) Need Help??


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

That would require anyone calling the sub to tie the constants--which makes no sense at all.

It just took me by surprise that chomp died when asked to inspect a variable that was readonly. The work-arounds are myriad:

  1. The XS version of Scalar::Util has a readonly function.
  2. I could also use the eval block technique to catch the death. (Which is how the pure Perl version of Scalar::Util::readonly() does it.)
  3. The Internals package has a IsWriteProtected() function.
  4. I could use Inline::C to use the svREADONLY macro.
  5. etc.

But, as chomp has to determine if there is anything to chomp, before attempting to modify it's argument, I just expected it to make that determination before checking for readonlyness. It doesn't do it that way, so I have to do extra tests or copies myself.

I've gotten spoiled by Perl's DWIMming things like negative offsets don't die, they work backwards from the end. Same with negative subscripts; ranges that produce no values; empty lists that act correctly regardless of the context etc.

On this occasion it didn't DWIM for me.


Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco.

Replies are listed 'Best First'.
Re^3: Should chomping a constant always raise an error?
by Anonymous Monk on Mar 14, 2005 at 11:53 UTC
    Oddly enough, if you use the Readonly module to mark your variable readonly, chomping will only fail if the variable is actually tried to be modified:
    #!/usr/bin/perl use strict; use warnings; use Readonly; Readonly my $msg1 => "Hello, world"; Readonly my $msg2 => "Hello, world\n"; chomp $msg1; # line 11 chomp $msg2; # line 12 __END__ Modification of a read-only value attempted at /tmp/ro line 12
Re^3: Should chomping a constant always raise an error?
by ysth (Canon) on Mar 14, 2005 at 08:56 UTC
    4. I could use Inline::C to use the svREADONLY macro.
    Done for you, in 5.8.0 and higher: Internals::SvREADONLY($sv)

      See number 3). :)


      Examine what is said, not who speaks.
      Silence betokens consent.
      Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco.
        The function I was referring to is built in, not from the CPAN module.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2022-05-20 23:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?