Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Math::BigInt new

by QM (Vicar)
on Sep 27, 2012 at 15:46 UTC ( #996036=note: print w/ replies, xml ) Need Help??


in reply to Re: Math::BigInt new
in thread Math::BigInt new

Agreed. However, here is the first part of new:

sub new { # create a new BigInt object from a string or another BigInt object. + # see hash keys documented at top # the argument could be an object, so avoid ||, && etc on it, this w +ould # cause costly overloaded code to be called. The only allowed ops ar +e # ref() and defined. my ($class,$wanted,$a,$p,$r) = @_; # avoid numify-calls by not using || on $wanted! return $class->bzero($a,$p) if !defined $wanted; # default to 0 return $class->copy($wanted,$a,$p,$r) if ref($wanted) && $wanted->isa($class); # MBI or subclass $class->import() if $IMPORT == 0; # make require work my $self = bless {}, $class;
If it gets to the last line, and $class == 27, then $class must have been changed somewhere?

And generally speaking, if I want a new object from an existing object, shouldn't the existing object be able to create one for me? Or do I have to go around doing this?

my $y = {ref $x}->new();

(or whatever the syntax is for that)

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


Comment on Re^2: Math::BigInt new
Select or Download Code
Re^3: Math::BigInt new
by tobyink (Abbot) on Sep 27, 2012 at 16:13 UTC

    $class is the same as $x (i.e. the bigint 27) the whole way through new, and bless implicitly stringifies its second argument (like print implicitly stringifies its arguments).

    ref($x)->new(48) should do what you want. Another reasonably common idiom is:

    my $new = (ref($existing) // $existing)->new(...)

    This allows $existing to be either an existing object or a class name.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^3: Math::BigInt new
by remiah (Hermit) on Sep 27, 2012 at 23:12 UTC

    I also think this is strange. There is no checking for ref($class). I guess something like ...

    if ( ref($class) ){ my $t = $wanted; $t =~ s/^[+-]//; return bless { sign => $class->{sign}, value => $CALC->_new($t), }, ref($class); }
    should be in new constructor, doesn't it?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-09-22 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (205 votes), past polls